Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(fn comprehend [init update ...]
(let [clauses [...] n (length clauses) result (gensym)]
(tset clauses n (update result (. clauses n)))
(for [i (- n 1) 1 -1] (table.insert (. clauses i) (. clauses (+ i 1))))
`(do (var ,result ,init) ,(. clauses 1) ,result)))
(fn tbl [...]
(comprehend {}
(fn [result expr] `(match ,expr (k# v#) (tset ,result k# v#)))
...))
(fn seq [...]
(comprehend []
(fn [result expr] `(tset ,result (+ (length ,result) 1) ,expr))
...))
(fn sum [...]
(comprehend 0
(fn [result expr] `(set ,result (+ ,result ,expr)))
...))
{:tbl tbl :sum sum :seq seq}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment