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