Changes between Version 2 and Version 3 of ForExpression
- Timestamp:
- 05/31/13 13:09:08 (11 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ForExpression
v2 v3 6 6 for <var> in <ienumerable> [where <condition>] get <expr> 7 7 for <var> in <ienumerable> where <condition> 8 for <var> in <start-expr> : stop-expr [: <step-expr> [where <condition>] get <expr> 8 9 }}} 9 10 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. 11 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 (Collections, arrays, streams,...). [[BR]] 12 The '''condition''' is optional and is used to filter the list. [[BR]] 13 Finally, the expr is evaluated for each individual item resulting in the elements of the final list. 11 14 12 The '''get <expr>''' part can be excluded in the presence of '''where''' in which case it is assumed to be '''get <var>'''. 15 The '''get <expr>''' part can be excluded in the presence of '''where''' in which case it is assumed to be '''get <var>'''.[[BR]] 16 i.e return the item from the ienumerable. 13 17 14 18 Both '''ienumerable''' and the resulting list may be empty. … … 16 20 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. 17 21 22 The 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 18 24 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. 19 25 20 There is also a statement version of the for loop. 26 There is also a statement version of the for loop. (EnumerableForStatement and NumericFor) 21 27 22 28 {{{ 23 29 #!cobra 24 # Example 1 t = for x in [1, 2, 3] get x*x 30 # Example 1 31 t = for x in [1, 2, 3] get x*x 25 32 # type of `x` is int, type of `t` is List<of int> 26 33 assert t.count==3 … … 42 49 # Example 4: `get x` is implied 43 50 t = for x in numbers where x > 0 51 52 #Example 5: numeric ranged list 53 range = for i in 0 : 10 get i*i 54 assert range.count == 10 55 assert range[0] ==0 56 assert range[9] == 81 44 57 }}} 58 59 Python calls this construct a 'List comprehension'.