2010-04-03 10:39:32 -04:00
|
|
|
# Simple variable swapping.
|
2010-07-25 01:23:37 -04:00
|
|
|
a = -1
|
|
|
|
b = -2
|
2010-01-11 22:04:25 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
[a, b] = [b, a]
|
2010-01-11 22:04:25 -05:00
|
|
|
|
2010-09-28 16:47:12 -04:00
|
|
|
eq a, -2
|
|
|
|
eq b, -1
|
2010-01-11 22:04:25 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
func = ->
|
|
|
|
[a, b] = [b, a]
|
2010-02-25 00:43:02 -05:00
|
|
|
|
2010-09-28 16:47:12 -04:00
|
|
|
eq func().join(' '), '-1 -2'
|
|
|
|
eq a, -1
|
|
|
|
eq b, -2
|
2010-02-25 00:43:02 -05:00
|
|
|
|
2010-09-28 16:47:12 -04:00
|
|
|
#713
|
|
|
|
eq (onetwo = [1, 2]), [a, b] = [c, d] = onetwo
|
|
|
|
ok a is c is 1 and b is d is 2
|
2010-02-25 07:31:33 -05:00
|
|
|
|
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
# Array destructuring, including splats.
|
2010-07-25 01:23:37 -04:00
|
|
|
[x,y...,z] = [1,2,3,4,5]
|
2010-03-22 01:52:47 -04:00
|
|
|
|
|
|
|
ok x is 1
|
|
|
|
ok y.length is 3
|
|
|
|
ok z is 5
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
[x, [y, mids..., last], z..., end] = [1, [10, 20, 30, 40], 2,3,4, 5]
|
2010-03-22 01:52:47 -04:00
|
|
|
|
|
|
|
ok x is 1
|
|
|
|
ok y is 10
|
|
|
|
ok mids.length is 2 and mids[1] is 30
|
|
|
|
ok last is 40
|
|
|
|
ok z.length is 3 and z[2] is 4
|
|
|
|
ok end is 5
|
2010-01-11 22:04:25 -05:00
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
# Object destructuring.
|
2010-07-25 01:23:37 -04:00
|
|
|
obj = {x: 10, y: 20, z: 30}
|
2010-01-11 22:04:25 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
{x: a, y: b, z: c} = obj
|
2010-01-11 22:04:25 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok a is 10
|
|
|
|
ok b is 20
|
|
|
|
ok c is 30
|
2010-01-11 22:04:25 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
person = {
|
2010-04-03 10:39:32 -04:00
|
|
|
name: "Moe"
|
2010-01-11 22:04:25 -05:00
|
|
|
family: {
|
2010-04-25 11:07:09 -04:00
|
|
|
'elder-brother': {
|
2010-01-11 22:04:25 -05:00
|
|
|
addresses: [
|
|
|
|
"first"
|
|
|
|
{
|
|
|
|
street: "101 Deercreek Ln."
|
|
|
|
city: "Moquasset NY, 10021"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
{name: a, family: {'elder-brother': {addresses: [one, {city: b}]}}} = person
|
2010-01-11 22:04:25 -05:00
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
ok a is "Moe"
|
2010-02-16 19:45:25 -05:00
|
|
|
ok b is "Moquasset NY, 10021"
|
2010-01-13 22:25:58 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
test = {
|
2010-01-13 22:25:58 -05:00
|
|
|
person: {
|
|
|
|
address: [
|
|
|
|
"------"
|
|
|
|
"Street 101"
|
|
|
|
"Apt 101"
|
|
|
|
"City 101"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
{person: {address: [ignore, addr...]}} = test
|
2010-01-13 22:25:58 -05:00
|
|
|
|
2010-03-19 22:48:55 -04:00
|
|
|
ok addr.join(', ') is "Street 101, Apt 101, City 101"
|
|
|
|
|
|
|
|
|
2010-04-25 22:21:53 -04:00
|
|
|
# Pattern matching against an expression.
|
2010-07-25 01:23:37 -04:00
|
|
|
[a, b] = if true then [2, 1] else [1, 2]
|
2010-03-19 22:48:55 -04:00
|
|
|
|
|
|
|
ok a is 2
|
|
|
|
ok b is 1
|
2010-04-25 22:21:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Pattern matching with object shorthand.
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
person = {
|
2010-04-25 22:21:53 -04:00
|
|
|
name: "Bob"
|
|
|
|
age: 26
|
|
|
|
dogs: ["Prince", "Bowie"]
|
|
|
|
}
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
{name, age, dogs: [first, second]} = person
|
2010-04-25 22:21:53 -04:00
|
|
|
|
|
|
|
ok name is "Bob"
|
|
|
|
ok age is 26
|
|
|
|
ok first is "Prince"
|
|
|
|
ok second is "Bowie"
|
2010-04-26 14:35:35 -04:00
|
|
|
|
|
|
|
# Pattern matching within for..loops
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
persons = {
|
2010-04-26 14:35:35 -04:00
|
|
|
George: { name: "Bob" },
|
|
|
|
Bob: { name: "Alice" }
|
|
|
|
Christopher: { name: "Stan" }
|
|
|
|
}
|
|
|
|
|
2010-08-07 08:02:16 -04:00
|
|
|
join1 = "#{key}: #{name}" for key, { name } of persons
|
2010-04-26 14:35:35 -04:00
|
|
|
|
|
|
|
deepEqual join1, ["George: Bob", "Bob: Alice", "Christopher: Stan"]
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
persons = [
|
2010-04-26 14:35:35 -04:00
|
|
|
{ name: "Bob", parent: { name: "George" } },
|
|
|
|
{ name: "Alice", parent: { name: "Bob" } },
|
|
|
|
{ name: "Stan", parent: { name: "Christopher" } }
|
|
|
|
]
|
|
|
|
|
2010-08-07 08:02:16 -04:00
|
|
|
join2 = "#{parent}: #{name}" for { name, parent: { name: parent } } in persons
|
2010-04-26 14:35:35 -04:00
|
|
|
|
|
|
|
deepEqual join1, join2
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
persons = [['Bob', ['George']], ['Alice', ['Bob']], ['Stan', ['Christopher']]]
|
2010-08-07 08:02:16 -04:00
|
|
|
join3 = "#{parent}: #{name}" for [name, [parent]] in persons
|
2010-04-26 14:35:35 -04:00
|
|
|
|
|
|
|
deepEqual join2, join3
|
2010-09-19 09:04:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Pattern matching doesn't clash with implicit block objects.
|
|
|
|
obj = a: 101
|
2010-09-27 10:16:34 -04:00
|
|
|
func = -> true
|
2010-09-19 09:04:38 -04:00
|
|
|
|
|
|
|
if func func
|
|
|
|
{a} = obj
|
|
|
|
|
|
|
|
ok a is 101
|
2010-10-02 08:49:21 -04:00
|
|
|
|
|
|
|
[x] = {0: y} = {'0': z} = [Math.random()]
|
|
|
|
ok x is y is z, 'destructuring in multiple'
|
2010-10-04 22:10:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Destructuring into an object.
|
|
|
|
obj =
|
|
|
|
func: (list, object) ->
|
|
|
|
[@one, @two] = list
|
|
|
|
{@a, @b} = object
|
2010-10-04 23:03:13 -04:00
|
|
|
{@a} = object # must not unroll this
|
|
|
|
null
|
2010-10-04 22:10:10 -04:00
|
|
|
|
|
|
|
obj.func [1, 2], a: 'a', b: 'b'
|
|
|
|
|
|
|
|
eq obj.one, 1
|
|
|
|
eq obj.two, 2
|
|
|
|
eq obj.a, 'a'
|
|
|
|
eq obj.b, 'b'
|