Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. (defmacro defmethods
  2. "A convenience over writing lots of short `defmethod` statements that
  3. use the same arguments:
  4.  
  5. (defmulti mm (fn [o x y] o))
  6. (defmethods mm [_ x y]
  7. '+ (+ x y)
  8. '- (- x y)
  9. '* (* x y)
  10. '/ (/ x y))
  11.  
  12. (mm '+ 1 2) ; => 3
  13.  
  14. Shorter and more readable than the alternative."
  15. [mm fn-args & dispatch-map]
  16. (assert (even? (count dispatch-map)))
  17. (let [dm (partition 2 dispatch-map)]
  18. `(do ~@(map (fn [[dv f]] `(defmethod ~mm ~dv ~fn-args ~f)) dm))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement