The purpose of the following code is to, given a vector, compute the two accumulators: 1. head-till-nth element, 2. (n+1)th element-till-last (where n, and this is the catch, iterates from 1 till length-of-list, inclusive).
(let [my-list [1 2 3 4 5 4 3 2 6]]
(loop [elem 0]
(when (<= elem (count my-list))
(do (println (reduce + (take elem my-list)))
(println (reduce + (drop elem my-list)))
(recur (inc elem))))))
While this code gets the job done, it is not functional but rather imperative (using a local, elem, as a running index and comparing against the vector's length to determine termination).
I considered using doseq
but I can see no immediate way to incrementally slice the vector per the requirements. Using for
is also wrong as I'm not trying to create a list comprehension.
I'd appreciate any answer showing the idiomatic Clojure way of doing this.