Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Multimethods with variants
; it's a bit cumbersome to set up and there's the unfortunate need to ignore the tag
; in the individual methods, but this allows you to leave the interpretation of open
; variants, well, *open* for extension by multimethod.
; dispatch off the first argument, which will be the tag
(defmethod command-multi (fn [tag & data] tag))
; the first argument to the *method* is still the tag
(defmulti command-multi :print [_ val] (println val))
(defmulti command-multi :read [_ fname] (slurp fname))
; partially applying `apply` so that the variant vectors
; get flattened - it's a bit odd but gets the job done
(def command (partial apply command-multi))
(command [:print "hello world"]) ; prints "hello world"
(command [:read "./project.clj"]) ; returns the contents of project.clj
Copy link

clojj commented Feb 17, 2015

any inspirations about leveraging clojure hierarchies for these variants ...?
see here:

So that :foo-hound isa ::foo-species
...and 'species-commands' get evaluated as well ...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment