2013-11-29 23:58:26 -05:00
|
|
|
# Generators
|
|
|
|
# -----------------
|
2013-12-19 17:21:14 -05:00
|
|
|
#
|
2013-11-29 23:58:26 -05:00
|
|
|
# * Generator Definition
|
|
|
|
|
2013-12-19 21:08:25 -05:00
|
|
|
test "generator as argument", ->
|
|
|
|
ok ->* 1
|
2013-11-29 23:58:26 -05:00
|
|
|
|
2013-12-19 21:08:25 -05:00
|
|
|
test "generator definition", ->
|
2013-12-19 17:21:14 -05:00
|
|
|
x = ->*
|
2013-11-30 15:26:32 -05:00
|
|
|
yield 0
|
|
|
|
yield 1
|
|
|
|
yield 2
|
2013-12-23 22:32:25 -05:00
|
|
|
y = do ->*
|
|
|
|
yield* x()
|
2013-11-30 15:26:32 -05:00
|
|
|
z = y.next()
|
|
|
|
eq z.value, 0
|
|
|
|
eq z.done, false
|
|
|
|
z = y.next()
|
|
|
|
eq z.value, 1
|
|
|
|
eq z.done, false
|
|
|
|
z = y.next()
|
|
|
|
eq z.value, 2
|
|
|
|
eq z.done, false
|
|
|
|
z = y.next()
|
|
|
|
eq z.value, undefined
|
|
|
|
eq z.done, true
|
2013-12-19 17:21:14 -05:00
|
|
|
|
|
|
|
test "bound generator", ->
|
|
|
|
obj =
|
|
|
|
bound: ->
|
|
|
|
do =>*
|
|
|
|
this
|
|
|
|
unbound: ->
|
|
|
|
do ->*
|
|
|
|
this
|
|
|
|
nested: ->
|
|
|
|
do =>*
|
|
|
|
do =>*
|
|
|
|
do =>*
|
|
|
|
this
|
|
|
|
|
2013-12-19 21:08:25 -05:00
|
|
|
eq obj, obj.bound().next().value
|
|
|
|
ok obj isnt obj.unbound().next().value
|
|
|
|
eq obj, obj.nested().next().value.next().value.next().value
|