Created
September 27, 2010 23:18
-
-
Save vito/600055 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Generator = Object clone | |
(g: Generator) yield: v := g yielder = { cc | | |
g control-state =! cc | |
g yielder yield: v | |
} call/cc | |
(b: Block) generator := | |
Generator clone do: { | |
next := control-state _? call/cc | |
control-state = Parameter new: { return | | |
yielder = return | |
join: b | |
} in-context | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- generator for the fibonacci sequence | |
fib = { | |
[i, j] = [0, 1] | |
{ | |
yield: i | |
[i, j] = [j, i + j] | |
} repeat | |
} generator | |
-- generator for 0, 1, 2, 3, ... | |
foob = { | |
i = 0 | |
{ | |
yield: i | |
i = i + 1 | |
} repeat | |
} generator | |
10 times: { | |
(foob next show .. ": " .. fib next show) print | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
atomo $ atomo examples/yield.atomo | |
0: 0 | |
1: 1 | |
2: 1 | |
3: 2 | |
4: 3 | |
5: 5 | |
6: 8 | |
7: 13 | |
8: 21 | |
9: 34 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment