Added section on python list comprehensions

This commit is contained in:
John Scillieri 2015-08-11 06:20:27 -04:00
parent 80157de011
commit 766979cd9b
1 changed files with 13 additions and 0 deletions

View File

@ -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.
#### 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 are not like [Nim set type](http://nim-lang.org/manual.html#set-type).