(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 upto n items. In case there are not enough padding elements, return a partition with less than n items.
;; partition a list of 20 items into 5 (20/4) lists of 4 items
(partition 4 (range 20))
;;=> ((0 1 2 3) (4 5 6 7) (8 9 10 11) (12 13 14 15) (16 17 18 19))
;; partition a list of 22 items into 5 (20/4) lists of 4 items
;; the last two items do not make a complete partition and are dropped.
(partition 4 (range 22))
;;=> ((0 1 2 3) (4 5 6 7) (8 9 10 11) (12 13 14 15) (16 17 18 19))
;; uses the step to select the starting point for each partition
(partition 4 6 (range 20))
;;=> ((0 1 2 3) (6 7 8 9) (12 13 14 15))
;; if the step is smaller than the partition size, items will be reused
(partition 4 3 (range 20))
;;=> ((0 1 2 3) (3 4 5 6) (6 7 8 9) (9 10 11 12) (12 13 14 15) (15 16 17 18))
;; when there are not enough items to fill the last partition, a pad can be supplied.
(partition 3 6 ["a"] (range 20))
;;=> ((0 1 2) (6 7 8) (12 13 14) (18 19 "a"))
;; when a pad is supplied, the last partition may not be of the same size as the rest
(partition 4 6 ["a"] (range 20))
;;=> ((0 1 2 3) (6 7 8 9) (12 13 14 15) (18 19 "a"))
;; but only as many pad elements are used as necessary to fill the final partition.
(partition 4 6 ["a" "b" "c" "d"] (range 20))
;;=> ((0 1 2 3) (6 7 8 9) (12 13 14 15) (18 19 "a" "b"))
;; a step smaller than the partition-size results in reuse.
(partition 3 1 [:a :b :c :d :e :f])
;;=> ((:a :b :c) (:b :c :d) (:c :d :e) (:d :e :f))
;; When there are less than n items in the coll, partition's behaviour
;; depends on whether there is a pad or not
;; without pad
(partition 10 [1 2 3 4])
;;=> ()
;; again, without pad
(partition 10 10 [1 2 3 4])
;;=> ()
;; with a pad this time (note: the pad is an empty sequence)
(partition 10 10 nil [1 2 3 4])
;;=> ((1 2 3 4))
;; or, explicit empty sequence instead of nil
(partition 10 10 [] [1 2 3 4])
;;=> ((1 2 3 4))
;; Partitioning 0 elements will produce an infinite seq of empty sequences
(partition 0 [1 2 3])
;; *hangs*
(take 5 (partition 0 [1 2 3]))
;; => (() () () () ())