Hi, On Apr 14, 3:53 pm, Chris Perkins <chrisperkin...@gmail.com> wrote:
> I'll counter with a challenge to you, Stuart - explain what the heck > you just said in a way that a dummy like me can understand :) Really, > I have so far been unable to wrap my head around the whole defprotocol > thing - I just don't get what the point of it is. A concrete example > explaining the "why" would help a lot. The seq abstraction is a very important aspect of Clojure's core library. In order to use things like map, filter and friends, Clojure calls seq on the given thing(s), which returns a seq - a view on the thing. One way to implement seq is to have a function with a huge condp which checks the type of the collection and calls the appropriate my- collection-seq function. However this is closed: what is not contained in the condp cannot be converted. Ok. So let's use in interface: every collection brings its own .seq method implementing - say - ISeqable. However this is closed again: while you can easily equip new collection types with the necessary .seq method it is not possible to retrofit this on existing types, say java.lang.String. Now on to protocols: (defprotocol PSeqable (seq [x] "Returns a seq on x")) New types can implement the PSeqable interface on Java side (or the protocol on deftype/reify side). But also old types can be retrofitted! (extend-protocol PSeqable String (seq [s] do-stuff-here)) So you get this hairy data collection from some third-party library you have no control over? No problem. Extend the PSeqable protocol for your collection and suddenly map, filter, and friends are actually your friends again. While this is already possible with multimethods, they have a performance impact. Protocols - on the other hand - are almost as fast as a Java interface method call. (AFAIU) This is roughly my understanding. Please speak up and correct me if I said something wrong. Sincerely Meikel -- 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 To unsubscribe, reply using "remove me" as the subject.