On 28 дек, 05:36, Conrad <drc...@gmail.com> wrote:
> I've been writing Clojure code today and have noticed the same pattern
> show up multiple times, but can't find an elegant way to code it in
> idiomatic Clojure. I feel like I'm missing an obvious solution...
> anyone else see something I don't? Thanks in advance!
>
> The problem boils down to the following minimal example: Suppose you
> want to write a function "left-total" which takes a list of number and
> returns pairs of said numbers, along with the total of numbers to the
> left:
>
> => (left-total [3 5 10 1 2 7])
> ([3 0] [5 3] [10 8] [1 18] [2 19] [7 21])
>

what about:

(defn left-total [s]
  (reverse (reduce (fn [y x] (cons (list x (reduce + (first y))) y))
() s)))

user> (left-total [3 5 10 1 2 7])
((3 0) (5 3) (10 8) (1 18) (2 19) (7 21))

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