How do I operate on every item in a vector AND refer to a previous value in Clojure?
clojure
Solution
user> (reduce (fn [total {:keys [a b]}]
(let [total (+ total b)]
(prn a total)
total))
0 my-vec)
"foo" 10
"bar" 23
"baz" 30
30
Problem
Given: ``` (def my-vec [{:a "foo" :b 10} {:a "bar" :b 13} {:a "baz" :b 7}]) ``` How could iterate over each element to print that element's :a and the sum of all :b's to that point? That is: "foo" 10 "bar" 23 "baz" 30 I'm trying things like this to no avail: ``` ; Does not work! (map #(prn (:a %2) %1) (iterate #(+ (:b %2) %1) 0)) my-vec) ``` This doesn't work because the "iterate" lazy-seq can't refer to the current element in my-vec (as far as I can tell). TIA! Sean