Created
September 30, 2013 19:35
-
-
Save llasram/6768951 to your computer and use it in GitHub Desktop.
multimethod groups
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 defmulti-group | |
"Define a group of related multimethods." | |
[& forms] `(do ~@(map #(cons 'defmulti %) forms))) | |
(defmacro defmethod-group | |
"Implement a group of related multimethods sharing common dispatch values." | |
[& specs] | |
(letfn [(parse-impls [specs] | |
(lazy-seq | |
(when (seq specs) | |
(let [dval (first specs), | |
[fns specs] (split-with list? (rest specs))] | |
(cons (cons dval fns) (parse-impls specs))))))] | |
`(do ~@(mapcat (fn [[dval & fns]] | |
(map #(list* 'defmethod (first %) dval (rest %)) fns)) | |
(parse-impls specs))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment