Third time's charm?  The previous versions of 'reduction' returned nil
for empty collection when no init was given.  This version follows
'reduce' more closely, calling the given function with no arguments:

user=> (reduction + [])
(0)

--Chouser

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

commit 9ca38b1bc2f27d29192f4ecf2312bb45a53557d1
Author: Chouser <[EMAIL PROTECTED]>
Date:   Fri Dec 5 21:41:44 2008 -0500

    reduction, v3

diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 4aad594..aa6048e 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -2738,6 +2738,20 @@
               coll (range (count coll)))
       (map #(if-let [e (find smap %)] (val e) %) coll)))
 
+(defn reduction
+  "Returns a lazy seq of the intermediate values of the reduction (as
+  per reduce) of coll by f, starting with init."
+  ([f [x & xs :as coll]]
+   (if (seq coll)
+     (if-let [[x2 & xs2] xs]
+       (reduction f (f x x2) xs2)
+       (cons x nil))
+     (cons (f) nil)))
+  ([f init [x & xs :as coll]]
+   (if (seq coll)
+     (lazy-cons init (reduction f (f init x) xs))
+     (cons init nil))))
+
 (defmacro dosync 
   "Runs the exprs (in an implicit do) in a transaction that encompasses
   exprs and any nested calls.  Starts a transaction if none is already

Reply via email to