Skip to content
AbePralle edited this page May 8, 2017 · 9 revisions

Syntax

Multi-line

forEach (<control>)
  <statements>
endForEach

Single Line

forEach (<control>) <statement>; <statement>; ...

Control Variations

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>)

Implicit forEach

(forEach in <collection> [from <from-range> | step <step>])
(forEach of <collection> [from <from-range> | step <step>])

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.

from Clause

Any forEach range based on a random-access collection (using the count/get interface described below) can have an optional from clause after the collection:

from <first index>
from <range>

Examples:

forEach (n in list from 1)
forEach (n in list from 0..list.count-2)

Range Syntax

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, ...)

Examples

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 (n in names from 1) println n     # Prints: Beta / Gamma
forEach (n in names from 0..1) println n  # Prints: Alpha / Beta
forEach (n in names step 2) println n     # Prints: Alpha / 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

Collections

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

Example forEach Collection Implementation

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
Clone this wiki locally