Skip to content

Instantly share code, notes, and snippets.

Created March 31, 2011 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/897401 to your computer and use it in GitHub Desktop.
Save anonymous/897401 to your computer and use it in GitHub Desktop.
Clojure macro for creating ratio converter functions.
(defmacro defratios
"Defines two converter functions with num-expr being how many of a make up b.
For example, there are 100 centimeters in a meter. To tell the program to make
(centimeter->meter) and (meter->centimeter), use (defratios meter centimeters 100)"
[a-symb b-symb num-expr]
(and (= (type a-symb) clojure.lang.Symbol) (= (type b-symb) clojure.lang.Symbol)
(let [ab-symb (symbol (arr a-symb b-symb))
ba-symb (symbol (arr b-symb a-symb))]
`(let [num# ~num-expr]
(defn ~ab-symb [a#] (* a# num#))
(defn ~ba-symb [b#] (/ b# num#))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment