Search code examples
clojure

How do I sum the inner sequence in a list of lists?


I want to take a sequence of sequences and sum up each inner sequence.

Data ((1 2 3) (2 3 4) (3 4 5) (4 5 6))

Desired output ((6) (9) (12) (15)) or (6 9 12 15)

I've tried apply map + which doesn't give the desired output. I've also experimented with reduce.


Solution

  • (map #(apply + %) '((1 2 3) (2 3 4) (3 4 5) (4 5 6)))
    ;; => (6 9 12 15)
    
    (map #(list (apply + %)) '((1 2 3) (2 3 4) (3 4 5) (4 5 6)))
    ;; => ((6) (9) (12) (15))