The following shows that the modifier “while” means the iterate will stop once an element match the check:
=> (for [x [3 2 3 1] :while (< x 3)] x)
()
However why the following not stop iterating ? it should return an empty list in my (wrong) understanding.
=> (for [x [3 2 3 1] y [:a :b] :while (< x 3)] [x y])
([2 :a] [2 :b] [1 :a] [1 :b])
Actually it turns out that there is no difference between the “when” and the “while” modifier in this case.
=> (for [x [3 2 3 1] y [:a :b] :when (< x 3)] [x y])
([2 :a] [2 :b] [1 :a] [1 :b])
How that happen ?
The
:whileand:whenmodifiers are always checked after the binding immediately preceding them, and only apply to the iteration of that loop. If you want to stop binding newxs, you need to put the:whileafter thexclause, not theyclause.