List Comprehension
List Comprehensions From http://www.frogatto.com/?p=1060
Frogatto supports a convenient way to build lists, known as list comprehensions. Let’s start with an example:
[n^2 | n <- range(10)] = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
This is equivalent to saying “for each n in range(10), generate a list with n^2″. A list comprehension can also contain filtering conditions on the input list. For instance, if we wanted to square only even numbers, we could do this:
[n^2 | n <- range(10), not n%2] = [0, 4, 16, 36, 64]
A list comprehension can take multiple input lists, in which all the combinations of elements in each list are used. For instance:
[a + ' ' + b | a <- ['small', 'big', 'huge'], b <- ['ant', 'bird', 'bat']] = ['small ant', 'big ant', 'huge ant', 'small bird', 'big bird', 'huge bird', 'small bat', 'big bat', huge bat']
List comprehensions are a convenient alternative to using map and filter. This formula from earlier can be re-written using list comprehensions:
map(range(10), spawn('coin_silver', (x - 200) + value*30, y - 80, 1))
As a list comprehension:
[spawn('coin_silver', (x - 200) + n*30, y - 80, 1) | n <- range(10)]
More help can be found via chat in Frogatto's Discord server, or by posting on the forums. This wiki is not a complete reference. Thank you for reading! You're the best. 🙂