This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: A Peek at some more Extensions
Feed Title: Travis Griggs - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/travis-rss.xml
Feed Description: This TAG Line is Extra
I was going to blog about the handy out selector today, inspired by David Buck's comments, but Eric took care of it for me, and that's probably just about enough about those
Done any stream parsing? Do file or socket reading, or heavy duty string parsing and you're sure to have. You've probably got lots of extensions like nextLine, or nextWord, or dropSeparators, or upToDigit, or skipComments, or all kinds of stuff like that. If you really do lots of one of those, then they deserve their own canonized method name. But a lot of stream parsing is just arbitrary boolean functions. And for those, its just really handy to express the conditions with blocks (just like we do with ifTrue: and ifFalse:). There are two basic things we do with streams, skip their contents, and extract them. From that we can build two new methods for PeekableStream, one for skipping, one for extracting, which use blocks to determine when to stop.
Having written that... it dons on me that the inverse ops, skipUntil: and nextWhile: might be nice.
Hey Cincom! Fix this!
There's not nearly as much interesting about this code as yesterdays. They were more complex and did some EndOfStreamNotification stuff, so I simplified them, ran the tests (see below), discovered why it used the EndOfStreamNotification, and then rewrote it to make it work. The problem is that peek and next have totally different behaviors when they encounter end of stream conditions! What's up with that? Peek will return a nil when the stream is atEnd. But next will return and EndOfStreamNotification. Shouldn't they oughta be the same? I would think peek should also raise the exception.
The Tests
testNextUntil
| rs |
rs := (1 to: 10) readStream.
self sequence: (rs nextUntil: [:each | each = 4]) sameAs: (1 to: 3).
rs
reset;
next;
next;
next.
self
sequence: (rs nextUntil: [:each | each = 6]) sameAs: #(4 5);
sequence: (rs nextUntil: [:each | each = 20]) sameAs: (6 to: 10)
testSkipWhile
| rs |
rs := (1 to: 10) readStream.
rs skipWhile: [:each | each #&60 4].
self assert: rs next = 4.
rs skipWhile: [:each | each #&62 4].
self assert: rs atEnd