(partition n coll)
(partition n step coll)
(partition n step pad coll)
Returns a lazy sequence of lists of n items each, at offsets step
apart. If step is not supplied, defaults to n, i.e. the partitions
do not overlap. If a pad collection is supplied, use its elements as
necessary to complete last partition up to n items. In case there are
not enough padding elements, return a partition with less than n items.
Example
Break a collection into chunks of two
(partition 2 [:n1 :n2 :n3 :n4])
Break a collection into overlapping pairs
(partition 2 1 [:n1 :n2 :n3 :n4])
Source
(defn
partition
"Returns a lazy sequence of lists of n items each, at offsets step\n apart. If step is not supplied, defaults to n, i.e. the partitions\n do not overlap. If a pad collection is supplied, use its elements as\n necessary to complete last partition up to n items. In case there are\n not enough padding elements, return a partition with less than n items."
([n coll] (partition n n coll))
([n step coll]
(lazy-seq
(when-let
[s (seq coll)]
(let
[p (take n s)]
(when
(== n (count p))
(cons p (partition n step (drop step s))))))))
([n step pad coll]
(lazy-seq
(when-let
[s (seq coll)]
(let
[p (take n s)]
(if
(== n (count p))
(cons p (partition n step pad (drop step s)))
(list (take n (concat p pad)))))))))