Before we had reducers we had a optimization (and still have this
optimization) called internal-reduce:

https://github.com/clojure/clojure/blob/master/src/clj/clojure/core/protocols.clj#L86

If you run reduce on a seq, then your only option is to walk through
the structure one cell at a time, doing if null checks at each step,
and then invoking next, first, etc. However with vectors, there's a
better way, simply use a loop with a index to the current item.

So the idea was that Clojure defines a protocol called
"internal-reduce" you can extend any datatype (for instance strings)
to support this protocol, and then reduce in anyone's code will use
this fast path when reducing your collection. This means that

(reduce + [1 2 3 4 5])

will be faster than

(reduce + (seq [1 2 3 4 5]))

And in the case of vectors gets around the problem of allocating a new
indexed seq on each call to (next). Also see
https://github.com/clojure/clojure/blob/master/src/clj/clojure/core/protocols.clj#L94

Hope this helps,

Timothy

-- 
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