有什么区别:while和:when in clojure? [英] What is the difference between :while and :when in clojure?

查看:156
本文介绍了有什么区别:while和:when in clojure?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究clojure,但不太清楚:while 之间的区别:

I'm studying clojure but not quite clear on the difference between the :while and :when test:

=> (for [x [1 2 3] y [1 2 3] :while (= (mod x y) 0)] [x y])
([1 1] [2 1] [2 2] [3 1])
=> (for [x [1 2 3] y [1 2 3] :when (= (mod x y) 0)] [x y])
([1 1] [2 1] [2 2] [3 1] [3 3])

任何人都可以通过阐述来帮助他们?

Can anybody help by elaborating on them ?

推荐答案

:当迭代绑定时,当条件为真时。 :while 遍历绑定并评估正文,直到条件为假:

:when iterates over the bindings, but only evaluates the body of the loop when the condition is true. :while iterates over the bindings and evaluates the body until the condition is false:

(for [x (range 20) :when (not= x 10)] x)
; =>(0 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19)

(for [x (range 20) :while (not= x 10)] x)
; => (0 1 2 3 4 5 6 7 8 9)

这篇关于有什么区别:while和:when in clojure?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆