On Feb 3, 4:43 pm, Anand Patil <[email protected]>
wrote:
> Hi all,
>
> Messing around with preduce at the REPL I saw this:
>
> user=> (defn q [sofar new] (do (print new sofar"\n") (+ (+ 1 new)
> sofar)))
> #'user/q
> user=> (reduce q 0 [1 2
> 3])
> 1 0
> 2 2
> 3 5
> 9
> user=> (preduce q 0 [1 2
> 3])
> 3 2
> 6 1
> 8
>
> It looks like preduce takes its arguments in the opposite order from
> reduce, but of course with this fn it shouldn't make any difference:
>
> user=> (defn q [new sofar] (do (print new sofar"\n") (+ (+ 1 new)
> sofar)))
> #'user/q
> user=> (preduce q 0 [1 2
> 3])
> 2 3
> 1 6
> 8
>
> Looks like it's using 3 where it should compute (q 3). This is a bug,
> correct?
>
No, it's not. as the docs for preduce say: http://clojure.org/api#preduce
"Also note that (f base an-element) might be performed many times"
in fact, an arbitrary number of times depending on how many parallel
tasks are fired up. So, the function with base value should produce an
identity, e.g. (+ 0 x) and (* 1 x), and your q with 0 does not.
Rich
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---