Wiki

Changes between Version 2 and Version 3 of ForExpression

Show
Ignore:
Timestamp:
05/31/13 13:09:08 (11 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ForExpression

    v2 v3  
    66for <var> in <ienumerable> [where <condition>] get <expr> 
    77for <var> in <ienumerable> where <condition> 
     8for <var> in <start-expr> : stop-expr [: <step-expr> [where <condition>] get <expr>  
    89}}} 
    910 
    10 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. 
     11The '''var''' may be declared in the expression or it can be a preexisting variable. The '''ienumerable''' is any expression that implements the '''IEnumerable''' interface (Collections, arrays, streams,...). [[BR]] 
     12The '''condition''' is optional and is used to filter the list. [[BR]] 
     13Finally, the expr is evaluated for each individual item resulting in the elements of the final list. 
    1114 
    12 The '''get <expr>''' part can be excluded in the presence of '''where''' in which case it is assumed to be '''get <var>'''. 
     15The '''get <expr>''' part can be excluded in the presence of '''where''' in which case it is assumed to be '''get <var>'''.[[BR]] 
     16i.e return the item from the ienumerable. 
    1317 
    1418Both '''ienumerable''' and the resulting list may be empty. 
     
    1620The 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. 
    1721 
     22The last variant specifying start and stop steps is to generate a list containing a range of numbers similarly to a numeric for loop. All the expressions here must evaluate to an IEnumerable range (integers usually). Unlike a for-loop the '''start-expr''' must be given. '''The stop-expr''' is the value at which generation stops, this value is not returned to the list (so its a one-past the end/sentinel ). The '''step-expr''' is the step between items the sequence progresses in - if not given it defaults to 1. 
     23 
    1824If 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. 
    1925 
    20 There is also a statement version of the for loop. 
     26There is also a statement version of the for loop. (EnumerableForStatement and NumericFor) 
    2127 
    2228{{{ 
    2329#!cobra 
    24 # Example 1 t = for x in [1, 2, 3] get x*x  
     30# Example 1  
     31t = for x in [1, 2, 3] get x*x  
    2532# type of `x` is int, type of `t` is List<of int> 
    2633assert t.count==3  
     
    4249# Example 4: `get x` is implied  
    4350t = for x in numbers where x > 0  
     51 
     52#Example 5: numeric ranged list  
     53range = for i in 0 : 10 get i*i 
     54assert range.count == 10 
     55assert range[0] ==0 
     56assert range[9] == 81 
    4457}}} 
     58 
     59Python calls this construct a 'List comprehension'.