Skip to content

Instantly share code, notes, and snippets.

@bahmanm
Created April 15, 2012 12:48
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 bahmanm/2392610 to your computer and use it in GitHub Desktop.
Save bahmanm/2392610 to your computer and use it in GitHub Desktop.
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