Last active
August 15, 2017 15:33
-
-
Save greggirwin/c0fe722deffb7b448286b5a819e0ca2b to your computer and use it in GitHub Desktop.
A different approach to a cycle generator, using a closure.
This file contains 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
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