Skip to content

Instantly share code, notes, and snippets.

@llasram
Created September 30, 2013 19:35
Show Gist options
  • Save llasram/6768951 to your computer and use it in GitHub Desktop.
Save llasram/6768951 to your computer and use it in GitHub Desktop.
multimethod groups
(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