Skip to content

Instantly share code, notes, and snippets.

@dyba
Last active August 29, 2015 14:18
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 dyba/1d9a3551e2bb9f75111c to your computer and use it in GitHub Desktop.
Save dyba/1d9a3551e2bb9f75111c to your computer and use it in GitHub Desktop.
;; WIP...
(defn quote-units
[units]
(let [syms (filter symbol? units)
vals (filter number? units)]
(interleave (map (fn [u] `'~u) syms) vals)))
(defmacro defunits
[quantity base-unit & units]
`(defmacro ~(symbol (str "unit-of-" quantity))
[val# un#]
(* val# (case un# ~@(quote-units units)))))
;; above should create this...
(defmacro unit-of-time
[value unit]
`(* ~value
~(case unit
s 1
m 60
h 3600
d 86400
ms 1/1000
us 1/1000000)))
;; Goal
(defunits time s
m 60
h 3600
d 86400
ms 1/1000
us 1/100000)
;; sample test
(unit-of-time 1 d)
;; => 86400
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment