11

I wish to create a multi method which I call like this:

(defmethod some-method "some value" [ a b ] b) 

: but which selects the function based only on the first paramter 'a'. How can I do this:

(defmulti some-method WHAT GOES HERE?) 

2 Answers 2

12

I didn't completely understand your question, but I think you want to dispatch only on one argument. You can do that like this, I think:

user=> (defmulti even-or-odd (fn [x _] (even? x))) #'user/even-or-odd user=> (defmethod even-or-odd true [a _] :even) #<MultiFn clojure.lang.MultiFn@293bdd36> user=> (defmethod even-or-odd false [a _] :odd) #<MultiFn clojure.lang.MultiFn@293bdd36> user=> (even-or-odd 2 3) :even user=> (even-or-odd 3 3) :odd user=> 
Sign up to request clarification or add additional context in comments.

Comments

7

Do you mean select the function based on the value of a?

Then you just need

(defmulti some-method (fn [a b] a)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.