2010-04-03 10:39:32 -04:00
|
|
|
# Basic array comprehensions.
|
2010-07-25 01:23:37 -04: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-25 01:23:37 -04: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-07-25 01:23:37 -04:00
|
|
|
nums = i * 3 for i in [1..3]
|
2010-04-03 10:39:32 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
negs = x for x in [-20..-5*2]
|
|
|
|
negs = negs[0..2]
|
2010-04-03 10:39:32 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
result = nums.concat(negs).join(', ')
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
ok result is '3, 6, 9, -20, -19, -18'
|
|
|
|
|
|
|
|
|
|
|
|
# With range comprehensions, you can loop in steps.
|
2010-07-27 23:00:26 -04:00
|
|
|
results = x for x in [0...15] by 5
|
|
|
|
ok results.join(' ') is '0 5 10'
|
2010-04-03 10:39:32 -04:00
|
|
|
|
2010-07-27 23:00:26 -04:00
|
|
|
results = x for x in [0..100] by 10
|
|
|
|
ok results.join(' ') is '0 10 20 30 40 50 60 70 80 90 100'
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
|
2010-05-31 19:41:02 -04:00
|
|
|
# And can loop downwards, with a negative step.
|
2010-07-25 01:23:37 -04:00
|
|
|
results = x for x in [5..1]
|
2010-05-31 19:41:02 -04:00
|
|
|
|
|
|
|
ok results.join(' ') is '5 4 3 2 1'
|
2010-05-31 19:43:30 -04:00
|
|
|
ok results.join(' ') is [(10-5)..(-2+3)].join(' ')
|
2010-05-31 19:41:02 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
results = x for x in [10..1]
|
2010-07-17 18:45:29 -04:00
|
|
|
ok results.join(' ') is [10..1].join(' ')
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
results = x for x in [10...0] by -2
|
2010-07-17 18:45:29 -04:00
|
|
|
ok results.join(' ') is [10, 8, 6, 4, 2].join(' ')
|
|
|
|
|
2010-05-31 19:41:02 -04:00
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
# Multiline array comprehension with filter.
|
2010-07-25 01:23:37 -04: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
|
|
|
|
|
|
|
|
ok evens.join(', ') is '4, 6, 8'
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
|
# Ensure that the closure wrapper preserves local variables.
|
2010-07-25 01:23:37 -04:00
|
|
|
obj = {}
|
2010-04-03 10:39:32 -04:00
|
|
|
|
2010-06-13 21:21:30 -04:00
|
|
|
for method in ['one', 'two', 'three']
|
2010-07-25 01:23:37 -04:00
|
|
|
obj[method] = ->
|
2010-06-13 21:21:30 -04:00
|
|
|
"I'm " + method
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
ok obj.one() is "I'm one"
|
|
|
|
ok obj.two() is "I'm two"
|
|
|
|
ok obj.three() is "I'm three"
|
|
|
|
|
|
|
|
|
2010-08-08 10:52:59 -04:00
|
|
|
# Ensure that local variables are closed over for range comprehensions.
|
|
|
|
funcs = for i in [1..3]
|
|
|
|
-> -i
|
|
|
|
|
|
|
|
ok (func() for func in funcs).join(' ') is '-1 -2 -3'
|
|
|
|
|
|
|
|
|
2010-06-13 21:21:30 -04:00
|
|
|
# Even when referenced in the filter.
|
2010-07-25 01:23:37 -04:00
|
|
|
list = ['one', 'two', 'three']
|
2010-06-13 21:21:30 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
methods = for num, i in list when num isnt 'two' and i isnt 1
|
2010-06-13 21:43:04 -04:00
|
|
|
-> num + ' ' + i
|
2010-06-13 21:21:30 -04:00
|
|
|
|
|
|
|
ok methods.length is 2
|
2010-06-13 21:43:04 -04:00
|
|
|
ok methods[0]() is 'one 0'
|
|
|
|
ok methods[1]() is 'three 2'
|
2010-06-13 21:21:30 -04:00
|
|
|
|
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
# Naked ranges are expanded into arrays.
|
2010-07-25 01:23:37 -04:00
|
|
|
array = [0..10]
|
2010-04-03 10:39:32 -04:00
|
|
|
ok(num % 2 is 0 for num in array by 2)
|
|
|
|
|
|
|
|
|
|
|
|
# Nested comprehensions.
|
2010-07-25 01:23:37 -04:00
|
|
|
multiLiner =
|
2010-04-03 10:39:32 -04:00
|
|
|
for x in [3..5]
|
|
|
|
for y in [3..5]
|
|
|
|
[x, y]
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
singleLiner =
|
2010-04-03 10:39:32 -04:00
|
|
|
[x, y] for y in [3..5] for x in [3..5]
|
|
|
|
|
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-25 01:23:37 -04: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-25 01:23:37 -04: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-25 01:23:37 -04:00
|
|
|
constructor: -> @name = 'Whiskers'
|
2010-07-15 21:18:35 -04:00
|
|
|
breed: 'tabby'
|
|
|
|
hair: 'cream'
|
|
|
|
|
2010-07-25 01:23:37 -04: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
|
|
|
|
|
|
|
|
|
|
|
# Optimized range comprehensions.
|
|
|
|
exxes = 'x' for [0...10]
|
|
|
|
ok exxes.join(' ') is 'x x x x x x x x x x'
|
|
|
|
|