Wiki
Version 1 (modified by todd.a, 14 years ago)

--

The for expression enables looping in expressions and results in a list with the results.

for <var> in <ienumerable> [where <condition>] get <expr>
for <var> in <ienumerable> where <condition>

The var may be declared in the expression or it can be a preexisting variable. The ienumerable is any expression that implements the IEnumerable interface. The condition is optional and is used to filter the list. Finally, the expr is evaluated for each individual item resulting in the elements of the final list.

The get <expr> part can be excluded in the presence of where in which case it is assumed to be get <var>.

Both ienumerable and the resulting list may be empty.

The resulting list is always newly created. The type of the list is List<of T> where T is the type of expr. There is no requirement that the type of expr be the same as the type of what the ienumerable contains.

If the for expression is part of a more complex expression—especially if it is the beginning of one—then put parenthesis around it to prevent the get <expr> portion of the grammar from consuming the rest of the complex expression. See below for an example.

There is also a statement version of the for loop.

# Example 1 t = for x in [1, 2, 3] get x*x 
# type of `x` is int, type of `t` is List<of int>
assert t.count==3 
assert t[0]==1 
assert t[1]==4 
assert t[2]==9 

# Example 2: Explicit variable typing 
names = for name as String in provider.names get name.toLower 

# Example 3: Grouping with parens 
assert (for s in ['aa', 'bbbb', 'cccccc'] get s.length) == [2, 4, 6] 
# not needed in this case: 
assert [2, 4, 6] == for s in ['aa', 'bbbb', 'cccccc'] get s.length 

# Example 3: with `where` 
names = for name in names where name.trim.length get name.trim 

# Example 4: `get x` is implied 
t = for x in numbers where x > 0