I study clojure (coming from ruby) and experiencing some problems, enveloping my head in the best way to create collections.
I would like to write a function that takes two arguments - the ary vector and the integer sum - and generates a new 2D vector in which each row sum is equal to <= input sum (ignore the input Check). The conceptual point with which I am having problems is to save the state of "sum of the current line", as well as create a new collection.
Here is what I have:
(defn split-after-sum [ary sum] (reduce (fn [acc i] (let [inner-sum (+ (last acc) i)] (if (< inner-sum sum) [(conj (first acc) i) (+ i (last acc))] [(conj (first acc) i "X") 0]))) [[] 0] ary))
I pass reduce 2-element vector to track both the collection I am collecting and the total number of this row.
This is kind of work. I did not understand how to actually make the result a 2D array, so it just sticks to "X" where the splits should be:
(first (split-after-sum [1 1 1 1 1 1 1 1 1] 2)) => [1 1 "X" 1 1 "X" 1 1 "X" 1 1 "X" 1]
Ideal yield:
(split-after-sum [1 1 1 1 1 1 1 1 1] 2) => [[1 1] [1 1] [1 1] [1 1] [1]]
I know that there are several things, but I think that the idiomatic answer to this problem will be enlightened.
source share