2010-07-25 05:23:37 +00:00
|
|
|
func = (first, second, rest...) ->
|
2010-01-25 04:40:45 +00:00
|
|
|
rest.join ' '
|
2009-12-31 22:50:12 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
result = func 1, 2, 3, 4, 5
|
2009-12-31 22:50:12 +00:00
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok result is "3 4 5"
|
2010-01-01 00:52:13 +00:00
|
|
|
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
gold = silver = bronze = theField = last = null
|
2010-01-01 00:52:13 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
medalists = (first, second, third, rest..., unlucky) ->
|
|
|
|
gold = first
|
|
|
|
silver = second
|
|
|
|
bronze = third
|
|
|
|
theField = rest.concat([last])
|
|
|
|
last = unlucky
|
2010-01-01 00:52:13 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
contenders = [
|
2010-01-01 00:52:13 +00:00
|
|
|
"Michael Phelps"
|
|
|
|
"Liu Xiang"
|
|
|
|
"Yao Ming"
|
|
|
|
"Allyson Felix"
|
|
|
|
"Shawn Johnson"
|
|
|
|
"Roman Sebrle"
|
|
|
|
"Guo Jingjing"
|
|
|
|
"Tyson Gay"
|
|
|
|
"Asafa Powell"
|
|
|
|
"Usain Bolt"
|
|
|
|
]
|
|
|
|
|
2010-01-25 04:40:45 +00:00
|
|
|
medalists "Mighty Mouse", contenders...
|
2010-01-01 00:52:13 +00:00
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok gold is "Mighty Mouse"
|
|
|
|
ok silver is "Michael Phelps"
|
|
|
|
ok bronze is "Liu Xiang"
|
2010-03-17 18:41:09 +00:00
|
|
|
ok last is "Usain Bolt"
|
2010-06-12 23:05:13 +00:00
|
|
|
ok theField.length is 8
|
2010-02-20 23:25:36 +00:00
|
|
|
|
2010-03-17 16:25:04 +00:00
|
|
|
contenders.reverse()
|
|
|
|
medalists contenders[0...2]..., "Mighty Mouse", contenders[2...contenders.length]...
|
|
|
|
|
|
|
|
ok gold is "Usain Bolt"
|
|
|
|
ok silver is "Asafa Powell"
|
|
|
|
ok bronze is "Mighty Mouse"
|
2010-03-17 18:41:09 +00:00
|
|
|
ok last is "Michael Phelps"
|
2010-06-12 23:05:13 +00:00
|
|
|
ok theField.length is 8
|
2010-02-20 23:25:36 +00:00
|
|
|
|
2010-04-03 14:39:32 +00:00
|
|
|
medalists contenders..., 'Tim', 'Moe', 'Jim'
|
2010-03-18 02:07:11 +00:00
|
|
|
ok last is 'Jim'
|
|
|
|
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
obj = {
|
2010-04-03 14:39:32 +00:00
|
|
|
name: 'moe'
|
2010-02-20 23:25:36 +00:00
|
|
|
accessor: (args...) ->
|
|
|
|
[@name].concat(args).join(' ')
|
|
|
|
getNames: ->
|
2010-07-25 05:23:37 +00:00
|
|
|
args = ['jane', 'ted']
|
2010-02-20 23:25:36 +00:00
|
|
|
@accessor(args...)
|
|
|
|
}
|
|
|
|
|
2010-04-03 14:39:32 +00:00
|
|
|
ok obj.getNames() is 'moe jane ted'
|
2010-03-18 13:27:13 +00:00
|
|
|
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
crowd = [
|
2010-03-18 13:27:13 +00:00
|
|
|
contenders...
|
|
|
|
"Mighty Mouse"
|
|
|
|
]
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
bests = [
|
2010-03-18 13:27:13 +00:00
|
|
|
"Mighty Mouse"
|
|
|
|
contenders[0..3]...
|
|
|
|
]
|
|
|
|
|
|
|
|
ok crowd[0] is contenders[0]
|
|
|
|
ok crowd[10] is "Mighty Mouse"
|
|
|
|
|
|
|
|
ok bests[1] is contenders[0]
|
2010-04-24 19:57:15 +00:00
|
|
|
ok bests[4] is contenders[3]
|
|
|
|
|
|
|
|
|
|
|
|
# Finally, splats with super() within classes.
|
|
|
|
|
|
|
|
class Parent
|
|
|
|
meth: (args...) ->
|
|
|
|
args
|
|
|
|
|
|
|
|
class Child extends Parent
|
|
|
|
meth: ->
|
2010-07-25 05:23:37 +00:00
|
|
|
nums = [3, 2, 1]
|
2010-04-24 19:57:15 +00:00
|
|
|
super nums...
|
|
|
|
|
2010-06-30 01:03:50 +00:00
|
|
|
ok (new Child).meth().join(' ') is '3 2 1'
|
2010-05-08 16:44:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Functions with splats being called with too few arguments.
|
2010-07-25 05:23:37 +00:00
|
|
|
pen = null
|
|
|
|
method = (first, variable..., penultimate, ultimate) ->
|
|
|
|
pen = penultimate
|
2010-05-08 16:44:54 +00:00
|
|
|
|
|
|
|
method 1, 2, 3, 4, 5, 6, 7, 8, 9
|
|
|
|
ok pen is 8
|
|
|
|
|
|
|
|
method 1, 2, 3
|
|
|
|
ok pen is 2
|
|
|
|
|
|
|
|
method 1, 2
|
|
|
|
ok pen is 2
|
2010-07-14 02:16:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Array splat expansions with assigns.
|
2010-07-25 05:23:37 +00:00
|
|
|
nums = [1, 2, 3]
|
|
|
|
list = [a = 0, nums..., b = 4]
|
2010-07-14 02:16:19 +00:00
|
|
|
ok a is 0
|
|
|
|
ok b is 4
|
|
|
|
ok list.join(' ') is '0 1 2 3 4'
|