On Dec 7, 1:21 am, Tzach <tzach.livya...@gmail.com> wrote:
> Hello
> What is the idiomatic way to implement the classic collide-with
> function in Clojure?
> I would like to implement something similar to the following (pseudo
> code ahead):
>
> (defmulti collide-with foo)
>
> (defmethod collide-with ["asteroid" "spaceship"] (print "Boom"))
> (defmethod collide-with ["asteroid" any] (print " Wiiissh"))
> (defmethod collide-with [any "spaceship"] (print "Wooossh"))
> (defmethod collide-with [any any] (print "Dead Space"))
>
> The idea is to have a default function for a collision of asteroid
> with anything else, and of spaceship with anything else.
> What foo can I use?
>
> Thanks

If order doesn't matter:

(defmulti collide-with
  #(vec (filter #{"asteroid" "spaceship"} %&)))
(defmethod collide-with ["asteroid" "spaceship"] [x y] (print
"Boom"))
(defmethod collide-with ["asteroid"] [x y] (print " Wiiissh"))
(defmethod collide-with ["spaceship"] [x y] (print "Wooossh"))
(defmethod collide-with [] [x y] (print "Dead Space"))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to