Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Clojure how can I pass multiple arguments to a defmethod?

Tags:

clojure

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?)
like image 217
yazz.com Avatar asked Apr 06 '11 17:04

yazz.com


2 Answers

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=> 
like image 120
michiakig Avatar answered Nov 05 '22 19:11

michiakig


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

Then you just need

(defmulti some-method (fn [a b] a))
like image 24
Joost Diepenmaat Avatar answered Nov 05 '22 19:11

Joost Diepenmaat