Essentially, you are using two accumulators: acc and (first coll).

You can probably use reduce for this purpose by
(reduce f [acc (first coll)] (rest coll))
adjusting the starting accumulator accordingly and altering f to be (fn
[[acc coll-acc] item] ...)

Frankly, it would probably be more readable to just use loop.  That's what
I do if I have more than one accumulator.

On Sun, Dec 9, 2012 at 7:39 AM, Alexander Semenov <bohtva...@gmail.com>wrote:

> Hi, folks.
>
> I'm wondering if Clojure library has 'reduce' function version which is
> like (reduce f coll) - i.e. it applies function to coll elements without
> the initial value but at the same time allows to use external accumulator
> which is passed to f as well. Better look at the code:
>
> (defn reduce-with-acc
>   "Applies f to first two coll elements and acc producing new acc value for
>   subsequent f calls. Returns acc. On singleton or empty collection returns
>   acc immediately."
>   [f acc coll]
>   (loop [a (first coll) acc acc s (next coll)]
>     (if (seq s)
>       (let [b (first s)]
>         (recur b (f a b acc) (next s)))
>       acc)))
>
> Regards,
> Alexander.
>
> --
> 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

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

Reply via email to