add chunk/window utils for seqs

This commit is contained in:
Adam Jeniski 2025-01-13 00:16:40 -05:00
parent 0327924c7a
commit e61dd6837d

View File

@ -141,6 +141,30 @@
:last (first sorted-nums)} :last (first sorted-nums)}
(rest sorted-nums)))) (rest sorted-nums))))
(defn chunks [n coll]
(loop [colls []
left coll
i n]
(if (<= i 0)
(apply map list colls)
(recur (conj colls (take-nth n left))
(rest left)
(dec i)))))
(defn windows [n coll]
(let [v (vec coll)]
(->> coll
(take (- (count v) (dec n)))
(map-indexed (fn [i _]
(range i (+ i n))))
(mmap v))))
(comment
(def data (map str (map (partial + 2) (range 12))))
(chunks 3 data)
(windows 8 data))
;; Math things ;; Math things
(defn square [n] (* n n)) (defn square [n] (* n n))