Created
December 19, 2012 23:16
-
-
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.
This file contains hidden or 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 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