diff --git a/Nim-for-Python-Programmers.md b/Nim-for-Python-Programmers.md index 384538b..1fc0689 100644 --- a/Nim-for-Python-Programmers.md +++ b/Nim-for-Python-Programmers.md @@ -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).