-
-
Save amalloy/897441 to your computer and use it in GitHub Desktop.
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
(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