Skip to content

Instantly share code, notes, and snippets.

Created March 16, 2015 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/96404337149c10c52439 to your computer and use it in GitHub Desktop.
Save anonymous/96404337149c10c52439 to your computer and use it in GitHub Desktop.
(defn- arr-partition-approx
"Splits `arr` into arrays of size between min-len and max-len,
trying to stick to (min+max)/2"
[min-len max-len arr]
(let [chunk-len (half (+ max-len min-len))
len (count arr)]
(when (pos? len)
(loop [acc [], len len, arr arr]
(cond
(<= len max-len)
(conj acc arr)
(>= len (+ chunk-len min-len))
(let [[lhs rhs] (split-at chunk-len arr)]
(recur (conj acc lhs) (- len chunk-len) rhs))
:else
(let [piece-len (half len)
[lhs rhs] (split-at piece-len arr)]
(recur (conj acc lhs) (- len piece-len) rhs)))))))
(doseq [n (range 0 256 16)
:let [res (arr-partition-approx min-len max-len (range n))]]
(println (map count res))
(assert (= (range n) (flatten res)) (pr-str res)))
cratch.clj:14 recur arg for primitive local: len is not matching primitive, had: Object, needed: long
scratch.clj:18 recur arg for primitive local: len is not matching primitive, had: Object, needed: long
Auto-boxing loop arg: len
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment