Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function related to partition that includes all elements?

Tags:

clojure

This behavior of Clojure's partition function is not what I need:

user=> (partition 3 (range 3))
((0 1 2))
user=> (partition 3 (range 4))
((0 1 2))
user=> (partition 3 (range 5))
((0 1 2))
user=> (partition 3 (range 6))
((0 1 2) (3 4 5))

I need the 'leftover' portions of the collection to be included, e.g.:

user=> (partition* 3 (range 4))
((0 1 2) (3))
user=> (partition* 3 (range 5))
((0 1 2) (3 4))

Is there a standard library function that does what I want?

like image 273
David J. Avatar asked Dec 14 '22 22:12

David J.


1 Answers

You're looking for partition-all. Just replace it in your example:

user> (partition-all 3 (range 4))
((0 1 2) (3))
user> (partition-all 3 (range 5)) 
((0 1 2) (3 4))
like image 55
Diego Basch Avatar answered Feb 14 '23 07:02

Diego Basch