O'Reilly logo

Clojure for Domain-specific Languages by Kelker Ryan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Multimethod polymorphism

The defmulti form will create a multifunction that returns a value, which is then used to determine which body of code to evaluate in the defmethod form. The first argument of the defmulti form is the symbol that holds the multimethod. The second argument is a function, and the number of arguments this function has will also be required by the defmethod forms.

The following example will make a decision on the return vector that's based on the customer and bottle arguments. The vector will look similar to [:sweet :carbon true true].

user> (defmulti customer-drinks (fn [customer bottle] [(if (:sweet? bottle) :sweet :not-sweet) (if (:carbonated? bottle) :carbon :no-carbon) (:drinks-sweet? customer) (:drinks-carbonated? customer)])) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required