开发者

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

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

(defmethod some-method "some value"
  [ a b 开发者_C百科]
  b)

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

(defmulti some-method
  WHAT GOES HERE?)


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=> 


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

Then you just need

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜