splitBy: to sequence.slate
Brian T. Rice
water at tunes.org
Wed Jul 21 14:13:44 PDT 2004
Olli Pietiläinen <ollip at freeshell.org> said:
> On Wed, 21 Jul 2004, Brian T. Rice wrote:
>
> > Here's a slightly tweaked version of it:
> Looks better than my version, but has a bug in it now. When there is a
> remainder, it doesn't get added to subSeqs because WriteStream doesn't
> have addLast: defined on. Changing addLast: to nextPut: corrects
> it. Thus the working code:
>
> s@(Sequence traits) splitIntoSize: n
> "Answers the result of splitting the sequence into n-sized sequences.
> If size of the sequence is not divisible by n, the last element will be
> smaller."
> [| subSeqs sepIndex nextSeq remainder |
> n isPositive ifFalse: [error: 'Split size must be positive.'].
> n >= s size ifTrue: [^ {s copy}].
> subSeqs: (Array newSize: s size // n + 1) writer.
> sepIndex: 0.
> [sepIndex < (s size - (s size mod: n))]
> whileTrue: [nextSeq: (s copyFrom: sepIndex to: sepIndex + n - 1).
> subSeqs nextPut: nextSeq.
> sepIndex: sepIndex + n].
> sepIndex = s size
> ifFalse: [remainder: (s copyFrom: sepIndex to: s indexLast).
> subSeqs nextPut: remainder].
> subSeqs contents
> ].
>
> Is this correct now?
Ah, yes. You are quite right. That's what I get for not testing it
thoroughly. I'll put in the fix when I'm at my workstation again.
Thanks
-Brian
More information about the Slate
mailing list