Skip to content

Instantly share code, notes, and snippets.

@oantolin
Created January 17, 2021 17:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oantolin/331cf3887f0abaff33cca9bb38196d96 to your computer and use it in GitHub Desktop.
Save oantolin/331cf3887f0abaff33cca9bb38196d96 to your computer and use it in GitHub Desktop.
(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