Skip to content

Instantly share code, notes, and snippets.

@drlivingston
Created April 17, 2012 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drlivingston/2409793 to your computer and use it in GitHub Desktop.
Save drlivingston/2409793 to your computer and use it in GitHub Desktop.
deterministic gentemp
(def ^:dynamic *gentemp-counters*)
(defn new-gentemp-counters [] {})
(defn next-name [name]
(let [c (get *gentemp-counters* name 0)]
(set! *gentemp-counters* (assoc *gentemp-counters* name (inc c)))
(str name "-" c)))
(defmacro with-reset-ids [& body]
`(binding [*gentemp-counters* (new-gentemp-counters)]
~@body)
@drlivingston
Copy link
Author

e.g.

> (with-reset-ids (next-name "foo")
                        (next-name "foo"))
"foo-1"
> (with-reset-ids (next-name "foo")
                        (next-name "foo")
                        (next-name "foo"))
"foo-2"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment