Added section on python list comprehensions
This commit is contained in:
parent
80157de011
commit
766979cd9b
|
@ -168,6 +168,19 @@ Use [Nim sequences](http://nim-lang.org/tut1.html#sequences)
|
||||||
|
|
||||||
Nim arrays and sequences can hold only items of the same type.
|
Nim arrays and sequences can hold only items of the same type.
|
||||||
|
|
||||||
|
#### List Comprehensions
|
||||||
|
```
|
||||||
|
import future # required for the list comprehension syntactical sugar
|
||||||
|
|
||||||
|
let x = @[1, 2, 3, 4, 5, 8, 8, 8, 10] # create a Nim sequence
|
||||||
|
|
||||||
|
# syntax: lc[ item | ( item <- seq, (optional condition) ), type ]
|
||||||
|
# equivalent to python [ y for y in x if y > 5 ]
|
||||||
|
echo lc[ y | ( y <- x, y > 5 ), int ]
|
||||||
|
# Can also perform operations on item, e.g. y*2 as shown here
|
||||||
|
echo lc[ y*2 | ( y <- x ), int ]
|
||||||
|
```
|
||||||
|
|
||||||
### Python sets
|
### Python sets
|
||||||
|
|
||||||
Python sets are not like [Nim set type](http://nim-lang.org/manual.html#set-type).
|
Python sets are not like [Nim set type](http://nim-lang.org/manual.html#set-type).
|
||||||
|
|
Loading…
Reference in New Issue