Created
January 17, 2021 17:23
-
-
Save oantolin/331cf3887f0abaff33cca9bb38196d96 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(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