2009/12/30 Rich Hickey <richhic...@gmail.com>:
> This was discussed before, the new version never made it into a patch:
> http://groups.google.com/group/clojure/msg/43de40f078a291cc

Great!

The 'old' reductions behaves slightly different from the 'new'
reductions for the 3 argument form:
    foo=> (reductions + 0 [3 5 10 1 2 7])
    (0 3 8 18 19 21 28)
    foo=> (reductions2 + 0 [3 5 10 1 2 7])
    (3 8 18 19 21 28)

Which output is more correct? On the one hand supplying a preserved
'initial value' from outside does not make much sense, but on the
other hand perhaps it is more in step with 3 argument reduce.

Does it matter? Probably not. It does provide an interesting case
study. The 3 argument form could be considered just a helper to
achieve the 2 argument form, but because it is part of the public
interface people may chose to rely on it:
    (defn left-total3 [coll]
     (map list coll (reductions + 0 coll)))
might have been better written as
    (defn left-total3 [coll]
     (map list coll (cons 0 (reductions + coll))))
So is exposing the 3 argument version a bad thing? It is not very
idiomatic to use the alternatives:
(let-fn [(reducer [f acc coll] ...)]
  (defn reductions [f coll]  ... reducer ...))
And then again if I can call (reduce + 3 [1 2]) maybe it is reasonable
to call (reductions + 3 [1 2])
I'm over analyzing what is really a non-issue, but just found it
thought provoking.

Back to more practical concerns, attached is a modified version for
consideration which preserves the old behavior but is faster. Happy to
supply a git patch if on the right track - let me know what is best :)


Regards,
Tim.

-- 
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
;; reductions by Chris Houser
;; http://groups.google.com/group/clojure/browse_thread/thread/3edf6e82617e18e0
;; improved by Stuart Holloway and Rich Hickey and Mark Engelberg
;; http://groups.google.com/group/clojure/browse_thread/thread/2be768e15d2b717a
;; made backward compatible by Timothy Pratley
;; http://groups.google.com/group/clojure/browse_thread/thread/3e37df49ce5edf44
(defn reductions
  "Returns a lazy seq of the intermediate values of the reduction
  (as per reduce) of coll by f. If an initial value init is supplied,
  it will be at the front of the sequence."
  ([f coll]
   (lazy-seq
     (if-let [s (seq coll)]
       (reductions f (first s) (rest s))
       (cons (f) nil))))
  ([f acc coll]
   (lazy-seq
     (if-let [s (seq coll)]
       (cons acc (reductions f (f acc (first s)) (rest s)))
       (cons acc nil)))))

Reply via email to