Welcome! Please see the About page for a little more info on how this works.

0 votes
in Collections by
edited by

I was expecting (partition-all n step coll) to be equal to (partition n step nil coll) but that's not always the case.

(partition-all 3 1 (range 5))
;=> ((0 1 2) (1 2 3) (2 3 4) (3 4) (4))
;; Notice the second to last partition is (3 4).  Why not stop there?

;; My work-around is to use a nil pad.  This gives my expected result
(partition 3 1 nil  (range 5))
;=> ((0 1 2) (1 2 3) (2 3 4) (3 4))

1 Answer

0 votes
by

You asked to slide through (range 5) at step 1.
In my opinion, it's fair to get the sequences (0 1 2 3 4), (1 2 3 4), (2 3 4), (3 4) and (4) as a result.
You also asked to limit the results to 3 elements, which results consequently in what you see:
(0 1 2) ;; Two elements cut off
(1 2 3) ;; One element thrown away
(2 3 4) ;; Nothing got cut
(3 4) ;; dito
(4) ;; dito

...