In 2003, I implemented the following method. It is something
that comes up from time to time.
!SequenceableCollection publicMethods !
splitEvery: chunkSize
"Answer a collection of
sub-collections each of the specified chunk size.
If the receiver size
is not a multiple of chunkSize, the last sub-collection will be
shorter."
| chunks stream |
chunkSize < 1 ifTrue: [
^self splitEvery: 1 ].
stream := ReadStream on: self.
chunks :=
OrderedCollection new.
[stream atEnd] whileFalse:
[
chunks add: (stream nextOrLess:
chunkSize).
].
^chunks.
! !
#(16 17 17 16 18 17 18 19 19 19 18 19 19
20 19 20 19 20 20 20 19 20)
->
{ #(17 17 16 18 17 18 19) . #(19 19 18 19 19 20 19) . #(20 19 20 20 20 19
20)}
any code?