2010-01-13 20:59:57 -05:00
|
|
|
x: 1
|
|
|
|
y: {}
|
2010-01-26 10:52:05 -05:00
|
|
|
y.x: -> 3
|
2010-01-13 20:59:57 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok x is 1
|
|
|
|
ok typeof(y.x) is 'function'
|
|
|
|
ok y.x instanceof Function
|
|
|
|
ok y.x() is 3
|
|
|
|
ok y.x.name is 'x'
|
2010-01-13 20:59:57 -05:00
|
|
|
|
|
|
|
|
2010-01-16 15:02:04 -05:00
|
|
|
# The empty function should not cause a syntax error.
|
2010-01-26 10:52:05 -05:00
|
|
|
->
|
2010-02-15 18:03:00 -05:00
|
|
|
() ->
|
2010-01-16 15:02:04 -05:00
|
|
|
|
|
|
|
|
2010-03-02 00:43:01 -05:00
|
|
|
# Multiple nested function declarations mixed with implicit calls should not
|
|
|
|
# cause a syntax error.
|
|
|
|
(one) -> (two) -> three four, (five) -> six seven, eight, (nine) ->
|
|
|
|
|
|
|
|
|
2010-01-13 20:59:57 -05:00
|
|
|
obj: {
|
|
|
|
name: "Fred"
|
|
|
|
|
2010-01-26 10:52:05 -05:00
|
|
|
bound: ->
|
2010-02-16 19:45:25 -05:00
|
|
|
(=> ok(this.name is "Fred"))()
|
2010-01-13 20:59:57 -05:00
|
|
|
|
2010-01-26 10:52:05 -05:00
|
|
|
unbound: ->
|
2010-02-16 19:45:25 -05:00
|
|
|
(-> ok(!this.name?))()
|
2010-01-13 20:59:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
obj.unbound()
|
2010-01-16 11:24:10 -05:00
|
|
|
obj.bound()
|
2010-01-16 15:44:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Python decorator style wrapper that memoizes any function
|
2010-01-26 10:52:05 -05:00
|
|
|
memoize: (fn) ->
|
2010-01-16 15:44:07 -05:00
|
|
|
cache: {}
|
|
|
|
self: this
|
2010-01-26 10:52:05 -05:00
|
|
|
(args...) ->
|
2010-01-16 15:44:07 -05:00
|
|
|
key: args.toString()
|
|
|
|
return cache[key] if cache[key]
|
|
|
|
cache[key] = fn.apply(self, args)
|
|
|
|
|
|
|
|
Math: {
|
2010-01-26 10:52:05 -05:00
|
|
|
Add: (a, b) -> a + b
|
|
|
|
AnonymousAdd: ((a, b) -> a + b)
|
|
|
|
FastAdd: memoize (a, b) -> a + b
|
2010-01-16 15:44:07 -05:00
|
|
|
}
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok Math.Add(5, 5) is 10
|
|
|
|
ok Math.AnonymousAdd(10, 10) is 20
|
|
|
|
ok Math.FastAdd(20, 20) is 40
|
2010-01-24 23:56:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Parens are optional on simple function calls.
|
2010-02-16 19:45:25 -05:00
|
|
|
ok 100 > 1 if 1 > 0
|
|
|
|
ok true unless false
|
|
|
|
ok true for i in [1..3]
|
2010-01-26 02:27:19 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok_func: (f) -> ok(f())
|
|
|
|
ok_func -> true
|
2010-01-26 14:49:33 -05:00
|
|
|
|
|
|
|
# Optional parens can be used in a nested fashion.
|
|
|
|
call: (func) -> func()
|
|
|
|
|
|
|
|
result: call ->
|
|
|
|
inner: call ->
|
|
|
|
Math.Add(5, 5)
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok result is 10
|
2010-01-26 21:15:56 -05:00
|
|
|
|
|
|
|
|
2010-03-09 21:54:44 -05:00
|
|
|
# More fun with optional parens.
|
|
|
|
fn: (arg) -> arg
|
|
|
|
|
|
|
|
ok fn(fn {prop: 101}).prop is 101
|
|
|
|
|
|
|
|
|
2010-03-10 09:28:00 -05:00
|
|
|
# Multi-blocks with optional parens.
|
|
|
|
result: fn( ->
|
|
|
|
fn ->
|
|
|
|
"Wrapped"
|
|
|
|
)
|
|
|
|
|
|
|
|
ok result()() is 'Wrapped'
|
|
|
|
|
|
|
|
|
2010-01-26 21:15:56 -05:00
|
|
|
# And even with strange things like this:
|
2010-03-02 00:43:01 -05:00
|
|
|
funcs: [((x) -> x), ((x) -> x * x)]
|
2010-01-26 21:15:56 -05:00
|
|
|
result: funcs[1] 5
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok result is 25
|
2010-01-26 21:15:56 -05:00
|
|
|
|
|
|
|
result: ("hello".slice) 3
|
|
|
|
|
2010-02-27 11:03:43 -05:00
|
|
|
ok result is 'lo'
|
|
|
|
|
|
|
|
|
|
|
|
# And with multiple single-line functions on the same line.
|
|
|
|
func: (x) -> (x) -> (x) -> x
|
|
|
|
ok func(1)(2)(3) is 3
|
2010-03-14 17:48:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Ensure that functions with the same name don't clash with helper functions.
|
|
|
|
del: -> 5
|
|
|
|
ok del() is 5
|
2010-03-26 11:44:25 -04:00
|
|
|
|
|
|
|
# Ensure that functions can have a trailing comma in their argument list
|
|
|
|
mult: (x, mids..., y) ->
|
|
|
|
x: * n for n in mids
|
|
|
|
x: * y
|
|
|
|
|
|
|
|
ok mult(1, 2,) is 2
|
|
|
|
ok mult(1, 2, 3,) is 6
|
2010-03-28 16:44:41 -04:00
|
|
|
ok mult(10,[1..6]...,) is 7200
|
|
|
|
|
|
|
|
|
|
|
|
# Test for inline functions with parentheses and implicit calls.
|
|
|
|
combine: (func, num) -> func() * num
|
|
|
|
result: combine (-> 1 + 2), 3
|
|
|
|
|
|
|
|
ok result is 9
|
2010-04-01 23:38:20 -04:00
|
|
|
|
2010-04-03 09:58:45 -04:00
|
|
|
|
2010-04-01 23:38:20 -04:00
|
|
|
# Test for calls/parens/multiline-chains.
|
|
|
|
f: (x) -> x
|
|
|
|
result: (f 1).toString()
|
|
|
|
.length
|
|
|
|
|
|
|
|
ok result is 1
|