for - pannous/warp GitHub Wiki
for
The keyword for denotes iteration over lists or iterators
One common example would be to iterate over number ranges:
for 1..10 : print it // excluding 10
for 1...10 : print it // including 10
for 1 to 10 : print it // including 10
for 1 upto 10 : print it // excluding 10
Following swift, and the possibile confusion of developers, we might is_declared warnings or ask if they know what they are doing.
for … in
While the in operator may return lists compatible with for, it may also return other objects. In this case in
binds with for to iterate over all such items:
3 in [1,2,3,4] // yes; the position comes from [1,2,3,4].index_of(3) == 3
for x in [1,2,3,4] : print x // different situation
Same with of from
Ruby's form works too, with a hint naming the braces: for x in 0..3; print x; end is for x in 0..3 { print x }.
for TYPE in …
Interesting: structurally type-matching for condition
for friend in [foe1, friend1, foe2, friend2]: print it
ONLY iterates over those elements of specified type (here:friend)!
This idea can be extended:
for LAMBDA CONDITION in …
for (it>2) in [1,2,3,4] : print it
for PATTERN in …
for (even number) in [1,2,3,4] : print it
Runs today (warp)
The end of a range is read once, before the first round, as in Python: n = 3; for i in 0..n { n = 10 } runs three
rounds.
In for x in xs the variable is also it; a block or lambda inside the body keeps its own it.
print name in text prints each item, as for name in text: print it:
for word in ["a", "b"]: print it
print chars in "hey"