-
-
Notifications
You must be signed in to change notification settings - Fork 4
for
Pannous edited this page Dec 21, 2022
·
5 revisions
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.
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] // true == (3 at postion 3)
for x in [1,2,3,4] : print x // different situation
Same with of
from
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 (it>2) in [1,2,3,4] : print it
for PATTERN in …
for (even number) in [1,2,3,4] : print it