Re: Idiomatic equivalent for double dispatch in clojure?
Thanks. Two months ago I bought that book. So I'll take a look at it ! -- 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
Re: Idiomatic equivalent for double dispatch in clojure?
I think it is a bit dull that for every combination [type1 type2] you have to define an equivalent method for [type2 type1] Is there a way to address this problem? perhaps with sets? i need to think about this one :D On Monday, November 19, 2012 4:32:15 PM UTC+1, Brian Marick wrote: Here's an example of using multimethods. (Note: I overrode the Clojure defmulti/defmethod macros with my own that play more nicely with the repl, and for other purposes. It's a straightforward translation.) You can see all the code here: https://github.com/marick/fp-oo/blob/master/sources/asteroids.clj The code's explained in chapter 14 of https://leanpub.com/fp-oo ;;; Objects of interest (def make (fn [type value-map] (with-meta value-map {:type type}))) (def rim-griffon (make ::starship {:name Rim Griffon, :speed 1000})) (def malse (make ::asteroid {:name Malse, :speed 0.1, :gravitational-pull 0.5})) ;;; Collide with multi-arg dispatch (defgeneric collide (fn [one two] [(type one) (type two)])) (defspecialized collide [::asteroid ::asteroid] (fn [ asteroids] asteroids)) (defspecialized collide [::asteroid ::starship] (fn [asteroid starship] [asteroid (stopped starship)])) (defspecialized collide [::starship ::asteroid] (fn [starship asteroid] [(stopped starship) asteroid])) (defspecialized collide [::starship ::starship] (fn [ starships] (let [speed (apply max (map :speed starships))] (map (partial with-speed speed) starships - Brian Marick, Artisanal Labrador Contract programming in Ruby and Clojure Occasional consulting on Agile Writing /Functional Programming for the Object-Oriented Programmer/: https://leanpub.com/fp-oo -- 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
Re: Idiomatic equivalent for double dispatch in clojure?
On Nov 20, 2012, at 10:22 AM, Thomas Goossens wrote: I think it is a bit dull that for every combination [type1 type2] you have to define an equivalent method for [type2 type1] Is there a way to address this problem? perhaps with sets? According to http://clojure.org/multimethods only vectors work if you want to have a type hierarchy. I wish sets worked, but then you'd have to also have some sort of destructuring mechanism to bind the parameters. - Brian Marick, Artisanal Labrador Contract programming in Ruby and Clojure Occasional consulting on Agile Writing /Functional Programming for the Object-Oriented Programmer/: https://leanpub.com/fp-oo -- 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
Re: Idiomatic equivalent for double dispatch in clojure?
Then probably something like this will help me out (defn defmutualmethods [multifn dispatch-val fn-tail] (defmethod multifn dispatch-val fn-tail) (defmethod multifn (reverse dispatch-val) fn-tail)) Nevertheless i'm making things more complex now because it does two things now! But it will make it easier for me i guess. its just a thought On Tuesday, November 20, 2012 9:19:23 PM UTC+1, Brian Marick wrote: On Nov 20, 2012, at 10:22 AM, Thomas Goossens wrote: I think it is a bit dull that for every combination [type1 type2] you have to define an equivalent method for [type2 type1] Is there a way to address this problem? perhaps with sets? According to http://clojure.org/multimethods only vectors work if you want to have a type hierarchy. I wish sets worked, but then you'd have to also have some sort of destructuring mechanism to bind the parameters. - Brian Marick, Artisanal Labrador Contract programming in Ruby and Clojure Occasional consulting on Agile Writing /Functional Programming for the Object-Oriented Programmer/: https://leanpub.com/fp-oo -- 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
Re: Idiomatic equivalent for double dispatch in clojure?
A long time ago I posted some macros to do double-dispatch with protocols and records. The link was http://paste.lisp.org/+2023 but it no longer works. The basic idea is you dispatch on the first argument to an intermediate type that then dispatches on the second argument. It's complicated, but essentially mechanical. Clojure multimethods have built-in multi-argument dispatch. -S -- 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
Re: Idiomatic equivalent for double dispatch in clojure?
Here's an example of using multimethods. (Note: I overrode the Clojure defmulti/defmethod macros with my own that play more nicely with the repl, and for other purposes. It's a straightforward translation.) You can see all the code here: https://github.com/marick/fp-oo/blob/master/sources/asteroids.clj The code's explained in chapter 14 of https://leanpub.com/fp-oo ;;; Objects of interest (def make (fn [type value-map] (with-meta value-map {:type type}))) (def rim-griffon (make ::starship {:name Rim Griffon, :speed 1000})) (def malse (make ::asteroid {:name Malse, :speed 0.1, :gravitational-pull 0.5})) ;;; Collide with multi-arg dispatch (defgeneric collide (fn [one two] [(type one) (type two)])) (defspecialized collide [::asteroid ::asteroid] (fn [ asteroids] asteroids)) (defspecialized collide [::asteroid ::starship] (fn [asteroid starship] [asteroid (stopped starship)])) (defspecialized collide [::starship ::asteroid] (fn [starship asteroid] [(stopped starship) asteroid])) (defspecialized collide [::starship ::starship] (fn [ starships] (let [speed (apply max (map :speed starships))] (map (partial with-speed speed) starships - Brian Marick, Artisanal Labrador Contract programming in Ruby and Clojure Occasional consulting on Agile Writing /Functional Programming for the Object-Oriented Programmer/: https://leanpub.com/fp-oo -- 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
Re: Idiomatic equivalent for double dispatch in clojure?
Yes indeed. with multimethods you can doue multi-argument dispatch. But that means that for every possible collision. I have to specify a method? Would it be a good idea to introduce taxonomies here? On Monday, November 19, 2012 4:02:18 PM UTC+1, Stuart Sierra wrote: A long time ago I posted some macros to do double-dispatch with protocols and records. The link was http://paste.lisp.org/+2023 but it no longer works. The basic idea is you dispatch on the first argument to an intermediate type that then dispatches on the second argument. It's complicated, but essentially mechanical. Clojure multimethods have built-in multi-argument dispatch. -S -- 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
Re: Idiomatic equivalent for double dispatch in clojure?
for what its worth, I'm building a highly polymorphic board-game engine [1] and I've stayed away from multimethods...I've used every single polymorphism capabillity that clojure provides (records/protocols, map-based, HOFs etc) except multi-methods. Performance is acritical matter for this project especially for chess... Using my lib I've got a purely functional chess, checkers and tic-tac-toe implementations so feel free to check it out... Jim [1] https://github.com/jimpil/Clondie24 On 19/11/12 15:32, Thomas Goossens wrote: Yes indeed. with multimethods you can doue multi-argument dispatch. But that means that for every possible collision. I have to specify a method? Would it be a good idea to introduce taxonomies here? On Monday, November 19, 2012 4:02:18 PM UTC+1, Stuart Sierra wrote: A long time ago I posted some macros to do double-dispatch with protocols and records. The link was http://paste.lisp.org/+2023 but it no longer works. The basic idea is you dispatch on the first argument to an intermediate type that then dispatches on the second argument. It's complicated, but essentially mechanical. Clojure multimethods have built-in multi-argument dispatch. -S -- 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 -- 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
Re: Idiomatic equivalent for double dispatch in clojure?
On Nov 19, 2012, at 9:32 AM, Thomas Goossens wrote: Yes indeed. with multimethods you can doue multi-argument dispatch. But that means that for every possible collision. I have to specify a method? If you read along in `asteroids.clj` file I showed, you'll see an example of a taxonomy. A ::gaussjammer is a subtype of a ::spaceship. - Brian Marick, Artisanal Labrador Contract programming in Ruby and Clojure Occasional consulting on Agile Writing /Functional Programming for the Object-Oriented Programmer/: https://leanpub.com/fp-oo -- 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