2010-04-03 10:39:32 -04:00
|
|
|
# Basic array comprehensions.
|
2010-07-24 22:23:37 -07:00
|
|
|
nums = n * n for n in [1, 2, 3] when n % 2 isnt 0
|
|
|
|
results = n * 2 for n in nums
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
ok results.join(',') is '2,18'
|
|
|
|
|
|
|
|
|
|
|
|
# Basic object comprehensions.
|
2010-07-24 22:23:37 -07:00
|
|
|
obj = {one: 1, two: 2, three: 3}
|
|
|
|
names = prop + '!' for prop of obj
|
|
|
|
odds = prop + '!' for prop, value of obj when value % 2 isnt 0
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
ok names.join(' ') is "one! two! three!"
|
|
|
|
ok odds.join(' ') is "one! three!"
|
|
|
|
|
|
|
|
|
|
|
|
# Basic range comprehensions.
|
2010-10-13 13:53:56 +09:00
|
|
|
nums = i * 3 for i from 1 to 3
|
|
|
|
negs = x for x from -20 to -5*2
|
|
|
|
eq nums.concat(negs.slice 0, 3).join(' '), '3 6 9 -20 -19 -18'
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
# With range comprehensions, you can loop in steps.
|
2010-10-13 13:53:56 +09:00
|
|
|
eq "#{ x for x from 0 to 9 by 3 }", '0,3,6,9'
|
|
|
|
eq "#{ x for x from 9 to 0 by -3 }", '9,6,3,0'
|
|
|
|
eq "#{ x for x from 3*3 to 0*0 by 0-3 }", '9,6,3,0'
|
2010-07-17 18:45:29 -04:00
|
|
|
|
2010-05-31 19:41:02 -04:00
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
# Multiline array comprehension with filter.
|
2010-07-24 22:23:37 -07:00
|
|
|
evens = for num in [1, 2, 3, 4, 5, 6] when num % 2 is 0
|
2010-04-03 10:39:32 -04:00
|
|
|
num *= -1
|
|
|
|
num -= 2
|
|
|
|
num * -1
|
2010-10-13 13:53:56 +09:00
|
|
|
eq evens + '', '4,6,8'
|
2010-04-03 10:39:32 -04:00
|
|
|
|
2010-10-13 13:53:56 +09:00
|
|
|
|
|
|
|
# Backward traversing.
|
|
|
|
odds = num for num in [0, 1, 2, 3, 4, 5] by -2
|
|
|
|
eq odds + '', '5,3,1'
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
# The in operator still works, standalone.
|
2010-06-21 23:51:12 -04:00
|
|
|
ok 2 of evens
|
2010-04-03 10:39:32 -04:00
|
|
|
|
2010-10-13 13:53:56 +09:00
|
|
|
# all/from/to aren't reserved.
|
|
|
|
all = from = to = 1
|
2010-04-03 10:39:32 -04:00
|
|
|
|
2010-06-13 21:21:30 -04:00
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
# Nested comprehensions.
|
2010-07-24 22:23:37 -07:00
|
|
|
multiLiner =
|
2010-10-13 13:53:56 +09:00
|
|
|
for x from 3 to 5
|
|
|
|
for y from 3 to 5
|
2010-04-03 10:39:32 -04:00
|
|
|
[x, y]
|
|
|
|
|
2010-07-24 22:23:37 -07:00
|
|
|
singleLiner =
|
2010-10-13 13:53:56 +09:00
|
|
|
[x, y] for y from 3 to 5 for x from 3 to 5
|
2010-04-03 10:39:32 -04:00
|
|
|
|
2010-06-12 19:05:13 -04:00
|
|
|
ok multiLiner.length is singleLiner.length
|
|
|
|
ok 5 is multiLiner[2][2][1]
|
|
|
|
ok 5 is singleLiner[2][2][1]
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Comprehensions within parentheses.
|
2010-07-24 22:23:37 -07:00
|
|
|
result = null
|
|
|
|
store = (obj) -> result = obj
|
2010-04-03 10:39:32 -04:00
|
|
|
store (x * 2 for x in [3, 2, 1])
|
|
|
|
|
|
|
|
ok result.join(' ') is '6 4 2'
|
2010-04-10 14:20:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Closure-wrapped comprehensions that refer to the "arguments" object.
|
2010-07-24 22:23:37 -07:00
|
|
|
expr = ->
|
|
|
|
result = item * item for item in arguments
|
2010-04-10 14:20:32 -04:00
|
|
|
|
|
|
|
ok expr(2, 4, 8).join(' ') is '4 16 64'
|
2010-07-15 21:18:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Fast object comprehensions over all properties, including prototypal ones.
|
|
|
|
class Cat
|
2010-07-24 22:23:37 -07:00
|
|
|
constructor: -> @name = 'Whiskers'
|
2010-07-15 21:18:35 -04:00
|
|
|
breed: 'tabby'
|
|
|
|
hair: 'cream'
|
|
|
|
|
2010-07-24 22:23:37 -07:00
|
|
|
whiskers = new Cat
|
|
|
|
own = value for key, value of whiskers
|
|
|
|
all = value for all key, value of whiskers
|
2010-07-15 21:18:35 -04:00
|
|
|
|
|
|
|
ok own.join(' ') is 'Whiskers'
|
|
|
|
ok all.sort().join(' ') is 'Whiskers cream tabby'
|
2010-07-27 22:38:38 -04:00
|
|
|
|
|
|
|
|
2010-08-24 22:19:53 -04:00
|
|
|
# Comprehensions safely redeclare parameters if they're not present in closest
|
|
|
|
# scope.
|
|
|
|
rule = (x) -> x
|
|
|
|
|
|
|
|
learn = ->
|
|
|
|
rule for rule in [1, 2, 3]
|
|
|
|
|
|
|
|
ok learn().join(' ') is '1 2 3'
|
|
|
|
|
2010-10-13 13:53:56 +09:00
|
|
|
ok rule(101) is 101
|