Skip to content

Instantly share code, notes, and snippets.

@robinkraft
Created December 19, 2012 23:16
Show Gist options
  • Select an option

  • Save robinkraft/4341518 to your computer and use it in GitHub Desktop.

Select an option

Save robinkraft/4341518 to your computer and use it in GitHub Desktop.
Simple example of using a Clojure macro to dispatch to functions by passing in a string of the function name.
(defn plus [& n] (apply + n))
(defn minus [& n] (apply - n))
(defn mult [& n] (apply * n))
(defmacro do-math
[math & nums]
`(~(read-string math) ~@nums))
(do-math "plus" 1 2 3 4)
;=> 10
(do-math "minus" 1 2 3 4)
;=> -8
(do-math "mult" 1 2 3 4)
;=> 24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment