-
Notifications
You must be signed in to change notification settings - Fork 8
forEach
AbePralle edited this page Sep 15, 2016
·
9 revisions
forEach (<control>)
<statements>
endForEach
forEach (<control>) <statement>; <statement>; ...
forEach (<range>)
forEach (<value id> in <range>)
forEach (<element id> in <collection>)
forEach (<index id> of <collection>)
forEach (<element id> at <index id> in <collection>)
forEach (... in <collection id>=<collection>)
forEach (... of <collection id>=<collection>)
(forEach in <collection>)
(forEach of <collection>)
An implicit forEach should be part of a larger expression. When the compiler encounters an implicit forEach, the current statement is wrapped in a forEach (<id> in/of <collection>)
and the implicit forEach term is replaced with with . See examples below.
Range Syntax | Description |
---|---|
<low>..<high> |
Iterates from low to high, inclusive |
<low>..<high> step <step> |
Iterates from low to high with the given step size |
<low> ..< <limit> |
Iterates from low to limit, including low but not including limit |
<low> ..< <limit> step <step> |
Iterates from low to limit with the given step size |
<high> downTo <low> |
Iterates from high down to low, inclusive, in steps of -1 |
<high> downTo <low> step <step> |
Iterates from high down to low with the given step size (e.g. -2, -3, ...) |
local names = ["Alpha", "Beta", "Gamma"]
forEach (n in names) println n # Prints: Alpha / Beta / Gamma
println (forEach in names) # Prints: Alpha / Beta Gamma
forEach (i of names) println i # Prints: 0 / 1 / 2
println (forEach of names) # Prints: 0 / 1 / 2
forEach (n at i in names) println "$:$"(i,n) # Prints: 0:Alpha / 1:Beta / 2:Gamma
forEach (1..3) println "Check" # Prints: Check / Check / Check
forEach (n in 1..5) println n # Prints: 1 / 2 / 3 / 4 / 5
println (forEach in 1..5) # Prints: 1 / 2 / 3 / 4 / 5
forEach (n in 1..5 step 2) println n # Prints: 1 / 3 / 5
forEach (n in 1..<5) println n # Prints: 1 / 2 / 3 / 4
forEach (n in 1..<5 step 2) println n # Prints: 1 / 3
forEach (n in 5 downTo 1) println n # Prints: 5 / 4 / 3 / 2 / 1
forEach (n in 5 downTo 1 step -2) println n # Prints: 5 / 3 / 1
A collection in Rogue forEach
terms is any object that provides one of the following pairs of properties and/or methods, listed here in the order of precedence:
forEach Collection Protocol | Interface |
---|---|
Random Access at
|
count:Int32 or count()->Int32 , at(index:Int32)->DataType
|
Random Access get
|
count:Int32 or count()->Int32 , get(index:Int32)->DataType
|
Sequential Access read
|
has_another:Logical or has_another()->Logical , read()->DataType
|
forEach (n at i in FibonacciGen(10)) println "f($) = $" (i,n)
class FibonacciGen( remaining_terms:Int32 )
PROPERTIES
current = 0
next = 1
METHODS
method has_another->Logical
return remaining_terms?
method read->Int32
--remaining_terms
local result = current
local sum = current + next
current = next
next = sum
return result
endClass
# Output
f(0) = 0
f(1) = 1
f(2) = 1
f(3) = 2
f(4) = 3
f(5) = 5
f(6) = 8
f(7) = 13
f(8) = 21
f(9) = 34