Google groups files section is having issues. Here's 'reduction' as discussed in IRC, mostly written by Rich.
--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 -~----------~----~----~----~------~----~------~--~---
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index 4aad594..a027e00 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -535,7 +535,20 @@ "Returns a seq of the items in coll in reverse order. Not lazy." [coll] (reduce conj nil 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)))) + ;;math stuff (defn + "Returns the sum of nums. (+) returns 0."