for loop - What is the difference between :while and :when in clojure? -
i'm studying clojure not quite clear on difference between :while
, :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 elaborating on them ?
:when
iterates on bindings, evaluates body of loop when condition true. :while
iterates on bindings , evaluates body until condition 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)
Comments
Post a Comment