Is it possible to overload Clojure multi-methods on arity?

前端 未结 3 833
借酒劲吻你
借酒劲吻你 2021-02-12 13:35

I have some code that uses multi-methods and would ideally like to overload the function (in this case, multi-function) so that I can pass in a higher order function to help wit

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-12 13:43

    You can do that using multimethods as shown below example:

    (defmulti which-colour-mm (fn [m & args] [(count args) (:colour m)]))
    (defmethod which-colour-mm [0 :blue] [m] (print m))
    (defmethod which-colour-mm [1 :blue] [m f] (f m))
    
    
    user=> (which-colour-mm {:colour :blue :object :ball})
    {:colour :blue, :object :ball}nil
    user=> (which-colour-mm {:colour :blue :object :ball} print)
    {:colour :blue, :object :ball}nil
    

提交回复
热议问题