On Sat, Aug 7, 2010 at 12:46 PM, Steve Purcell <st...@sanityinc.com> wrote: > > Nice - that's about twice as fast as my version (with the 100 limit scaled up > to 1 million), though perhaps a less general pattern since the code structure > assumes knowledge of +'s cumulative nature. >
Yes, it needs a proper initializer(or Zero() in monad speak) which needs to be passed in to make it generic. What it does is simply carrying the accumulator in the sequence as a tuple(or vector in clojure term, I am still more familar with Haskell/F#) so the accumulator can be anything. There was a quirk where I need a [0 0] which can be fixed in the following: user=> (map second (take-while #(< (first %) 100) (reductions #(vector (+ (first %1) (first %2)) (first %2)) (map #(vector % %) (iterate inc 0))))) (0 1 2 3 4 5 6 7 8 9 10 11 12 13) here the #(vector % %) kind of work around by producing the initializer, assuming the accumulator and the element are the same type. If this assumption doesn't hold, it would become: (map #(vector Zero(%) %) collection) where Zero(%) needs to be something that produce the initializer, say (list) if the accumulator is a list. -- 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