Skip to content

Instantly share code, notes, and snippets.

@vito
Created September 27, 2010 23:18
Show Gist options
  • Save vito/600055 to your computer and use it in GitHub Desktop.
Save vito/600055 to your computer and use it in GitHub Desktop.
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
}
-- 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
}
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