splitBy: to sequence.slate

=?X-UNKNOWN?Q?Olli_Pietil=E4inen?= ollip at sdf.lonestar.org
Mon Jul 19 09:47:37 PDT 2004


Hi.

I wrote a small method for sequence.slate:

s@(Sequence traits) splitBy: n
"Splits the sequence to n-sized sequences. If size of the sequence is not
divisible by n, last element will be smaller."
[| subSeqs sepIndex nextSeq remainder |
   n <= 0 ifTrue: [error: 'Split size must be positive.'].
   n > s size ifTrue: [error: 'Split size shouldn\'t be bigger than sequence size.'].

   subSeqs: ExtensibleArray newEmpty.
   sepIndex: 0.
   [sepIndex < (s size - (s size mod: n))]
      whileTrue: [ nextSeq: (s copyFrom: sepIndex to: (sepIndex + n) - 1).
                   subSeqs addLast: nextSeq.
                   sepIndex: sepIndex + n].
   sepIndex = s size
      ifFalse: [ remainder: (s copyFrom: sepIndex to: (s size - 1)).
                 subSeqs addLast: remainder].
   subSeqs
].


I don't know if this is a good name for it. Maybe groupBy: would be better?


Olli




More information about the Slate mailing list