Skip to content

Instantly share code, notes, and snippets.

@sireliah
Created January 28, 2020 23:43
Show Gist options
  • Select an option

  • Save sireliah/bfe070fdcd3df037e438a5482fa73480 to your computer and use it in GitHub Desktop.

Select an option

Save sireliah/bfe070fdcd3df037e438a5482fa73480 to your computer and use it in GitHub Desktop.
(def m {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8 :i 9 :j 534})
;; Split to chunks by count
(defn bucketize [rows]
(reduce
(fn [acc [k v]]
(if (< (count (last acc)) 3)
(assoc-in acc [(- (count acc) 1)] (conj (last acc) v))
(conj acc [v])))
[[]] rows))
(bucketize m)
;; [[5 7 3] [534 8 2] [4 6 9] [1]]
;; Split to chunks by sum
(defn bucketize [rows]
(reduce
(fn [acc [k v]]
(if (< (reduce (fn [a x] (+ a x v)) 0 (last acc)) 9)
(assoc-in acc [(- (count acc) 1)] (conj (last acc) v))
(conj acc [v])))
[[]] rows))
(bucketize m)
;; [[5] [7] [3] [534] [8] [2 4] [6] [9] [1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment