Skip to content

Instantly share code, notes, and snippets.

@greggirwin
Last active August 15, 2017 15:33
Show Gist options
  • Save greggirwin/c0fe722deffb7b448286b5a819e0ca2b to your computer and use it in GitHub Desktop.
Save greggirwin/c0fe722deffb7b448286b5a819e0ca2b to your computer and use it in GitHub Desktop.
A different approach to a cycle generator, using a closure.
Red []
closure: func [
vars [block!] "Values to close over, in spec block format"
spec [block!] "Function spec for closure func"
body [block!] "Body of closure func; vars will be available"
][
func spec bind body context vars
]
cycler: func [block [block!]][
closure compose/only [block: (block)] [/reset] [
if any [reset tail? block] [block: head block]
also first block block: next block
]
]
test-cyc: func [cyc-fn [function!]][loop 7 [probe cyc-fn]]
cyc: cycler [1 2 3 4 5]
test-cyc :cyc
cyc: cycler [a #test 5x5 8 "key" %file 10%]
test-cyc :cyc
cyc: cycler [[a #test] [5x5 8] ["key" %file 10%]]
test-cyc :cyc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment