Skip to content

Instantly share code, notes, and snippets.

@dander
Created April 26, 2017 06:04
Show Gist options
  • Save dander/7e9118778acda64f18ee4fa6e7c9e1e8 to your computer and use it in GitHub Desktop.
Save dander/7e9118778acda64f18ee4fa6e7c9e1e8 to your computer and use it in GitHub Desktop.
lazy range enumeration
Red [
Title: "range generator attempt (4?)"
]
; @JacobGood's closure func
closure: func [
vars [block!]
spec [block!]
body [block!]
][
; Can't use `function` here, because it will collect set-words
; in the body, which may be closure vars.
func spec compose [(bind body context vars)]
]
range: function [start finish][
c: context [
cur: start - 1
f: finish
iter: func [][
either cur < f [
cur: cur + 1
][ none ]
]
]
:c/iter
]
range2: function [start finish][
closure compose [i: (start) finish: (finish)] [] [
either i <= finish [
also i
i: i + 1
][none]
]
]
..: make op! :range2
for-gen: func [
'word [word!]
gen [function!]
body [block!]
][
while reduce [to-set-word :word 'gen] body
]
for-gen i 5 .. 9 [print i]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment