On Fri, Dec 5, 2008 at 10:50 AM, Chouser <[EMAIL PROTECTED]> wrote:
> Google groups files section is having issues.
> Here's 'reduction' as discussed in IRC, mostly written by Rich.
I messed it up anyway -- tried to use if-let too early.
Try this patch instead.
--Chouser
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
commit f491694550c8ea0b528afd2d98268572a5dab599
Author: Chouser <[EMAIL PROTECTED]>
Date: Fri Dec 5 11:32:59 2008 -0500
reduction
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 4aad594..75da416 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -2738,6 +2738,19 @@
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]]
+ (when (seq coll)
+ (if-let [[x2 & xs2] xs]
+ (reduction f (f x x2) xs2)
+ (cons x 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