Skip to content

Instantly share code, notes, and snippets.

@amalloy
Forked from anonymous/defratios
Created March 31, 2011 23:01
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 amalloy/897441 to your computer and use it in GitHub Desktop.
Save amalloy/897441 to your computer and use it in GitHub Desktop.
(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 (every? #{clojure.lang.Symbol}
(map type [a-symb b-symb]))
(let [ab-symb (symbol (arr a-symb b-symb))
ba-symb (symbol (arr b-symb a-symb))
num (gensym "num-")]
`(let [~num ~num-expr]
~@(for [[op arg name] [['* 'a ab-symb]
['/ 'b ba-symb]]]
`(defn ~name [~arg] (~op ~arg ~num)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment