Skip to content

Instantly share code, notes, and snippets.

@dockimbel
Forked from maximvl/lc.r
Created August 6, 2016 11:11
Show Gist options
  • Save dockimbel/8c89b446cfc34aeddec73f1176e549e6 to your computer and use it in GitHub Desktop.
Save dockimbel/8c89b446cfc34aeddec73f1176e549e6 to your computer and use it in GitHub Desktop.
List comprehension dialect for Rebol
REBOL []
print []
lc: function [block] [lc-state lc-rule input-rule filter-rule i e] [
lc-state: make object! [
do-block: copy []
inputs: copy []
filter-block: none
input-state: copy []
res: copy []
]
input-rule: bind [set input-var word! 'in set input-block [block! | paren! | word!] (append/only inputs reduce [input-var input-block])] lc-state
filter-rule: bind ['if set filter-block block!] lc-state
lc-rule: bind [set do-block block! '| some input-rule opt filter-rule] lc-state
parse block lc-rule
foreach i reverse lc-state/inputs [
e: either block? i/2 [i/2] [
either paren? i/2 [do i/2] [get i/2]
]
either empty? lc-state/input-state [
append lc-state/input-state compose/deep [
foreach (i/1) [(e)] [if do [(lc-state/filter-block)] [append lc-state/res do [(lc-state/do-block)]]]
]
] [
lc-state/input-state: compose/deep [foreach (i/1) [(e)] [(lc-state/input-state)]]
]
]
;print mold lc-state
;print mold reduce [lc-state/input-state]
do lc-state/input-state
lc-state/res
]
x-set: [1 2 3]
r: lc [[x + y ] | x in x-set y in (copy [10 100 1000]) if [ x != 2]]
print mold r
;; => [11 101 1001 13 103 1003]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment