Q: Is it possible to overload Clojure multi-methods on arity?
A: Yes. Below a code example
(def u1 {:name "bob" :company "Facebook" :salary 100000}) (def u2 {:name "jane" :company "Google" :salary 150000}) (def u3 {:name "mike" :company "Other" :salary 160000}) (defmulti tm (fn [user & etc] (:company user))) (defmethod tm "Google" [user] "Google user") (defmethod tm "Facebook" [user] "Facebook user") (defmethod tm "Other" ([user] "Other user with one argument") ([user y] "Other user with two argument") ([user y & etc] "Other user with many arguments")) ;; (tm u1) ;; "Facebook user" ;; (tm u2) ;; "Google user" ;; (tm u3) ;; "Other user with one argument" ;; (tm u3 1) ;; "Other user with two argument" ;; (tm u3 1 3) ;; "Other user with many arguments"
No comments:
Post a Comment