Hmmm... I didn't think of using cons/vectors to avoid the reverse...

On Dec 27, 10:47 pm, David Cabana <[email protected]> wrote:
> If speed matters, I found both of these to be faster than the version
> using reductions.
>
> First version is to roll my own replacement for reductions,
> specialized to addition:
>
> (defn partial-sums [coll]
>   (loop [result [0]
>             tot      0
>             terms coll]
>     (if (empty? terms)
>       result
>       (let [next (+ tot (first terms))]
>         (recur (conj result next)
>                next
>                (rest terms))))))
>
> (defn left-tot1 [lst]
>  (map vector lst
>       (partial-sums lst)))
>
> Even faster is this one, which is something along the lines of one of
> you original versions,
> but without a list reversal.
>
> (defn left-tot2 [lst]
>   (loop [result  [ ]
>             tot       0
>             terms  lst]
>
>     (if (empty? terms)
>       result
>       (let [f (first terms)]
>         (recur (conj result [f tot])
>                   (+ tot f)
>                   (rest terms))))))
>
>
>
> On Sun, Dec 27, 2009 at 9:22 PM, Conrad <[email protected]> wrote:
> > ...for some reason, however, this version is a lot slower than my 3
> > versions of the code- Not sure if it's the laziness of this version,
> > or if there's stuff being put on the call stack by "reductions"...

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
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

Reply via email to