Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Clojure tricks and useful functions/macros
(defn getfn [fname]
"Gets a function by its fully qualified name."
(let [fsym (symbol fname)
nsym (symbol (namespace fsym))]
(require nsym)
(resolve fsym)))
(defn interpolate [s vals]
"Returns the interpolation of string 's' with a map of key/values."
;Keys in 's' are marked with #{key-name} syntax.
;So (interpolate "Hi #{you}, this is #{me}." {:you "Clojure Hacker" :me "REPL"})
;yeilds "Hi Clojure Hacker, this is REPL."
(let [-re #"#\{(.*?)\}"
m (re-matcher -re s)
k (fn []
(keyword (.substring s (+ (.start m) 2) (dec (.end m)))))]
(loop [result "" rest s i 0]
(if (.find m)
(recur (str result (.substring s i (.start m)) ((k) vals))
(.substring s (.end m))
(.end m))
(str result (.substring s i))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment