Re: Possible bug in reducer code? Or dead code? Or derp on my part?

2012-06-29 Thread Timothy Baldridge
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


Possible bug in reducer code? Or dead code? Or derp on my part?

2012-06-29 Thread retnuH
Hi there, I've been browsing through the code trying to wrap my head the 
new reducers library.

I came across 2 functions called 'reduce' in APersistentVector.java around 
line 447.  The don't have the same 'reduced?' check that ArrayChunk.java 
has in it's implementation of reduce (around line 57 or so).

Now, as far as I can tell, those 2 reduce functions aren't actually 
reachable through the normal reduce code-path, but that's only based on 
what I could tell - I'm certainly no expert on clojure internals!  But it 
did occur to me that they could still be called directly.

So I'm just wondering if this is a latent bug-in-waiting, or dead code, or 
a misundersting on my part.

Cheers,

H

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