Created
April 10, 2012 01:53
-
-
Save bnyeggen/2347883 to your computer and use it in GitHub Desktop.
More interning with Clojure
This file contains 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
(defmacro with-interns | |
"interns => [intern1 intern2...] | |
Evaluates body while making available many functions, bound to the | |
symbols in interns. Each fn, when called, returns a deduped reference to | |
its argument. The deduplication is with respect to any previously-called | |
arguments to that fn. | |
(with-interns [intern] | |
(into {} | |
(for [k (range 100000)] | |
[k (intern (vec (repeatedly 3 #(rand-int 10))))])))" | |
[interns & body] | |
(cond (= (count interns) 0) | |
`(do ~@body) | |
(symbol? (interns 0)) | |
`(let [intern# (atom #{}) | |
~(interns 0) #(get @intern# % (get (swap! intern# conj %) %))] | |
(with-interns ~(subvec interns 1) ~@body)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment