The Artima Developer Community
Sponsored Link

Agile Buzz Forum
A Peek at some more Extensions

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
A Peek at some more Extensions Posted: Jun 8, 2004 2:15 PM
Reply to this message Reply

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
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Travis Griggs - Blog

Advertisement
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.

nextUntil: aTestBlock 
	| stream |
	stream := self contentsSpecies new writeStream.
	[self atEnd or: [aTestBlock value: self peek]]
		whileFalse: [stream nextPut: self next]
	^stream contents

[self atEnd or: [(aTestBlock value: self peek) not]] 
		whileFalse: [self next]

Using these we can express all kinds of new parsing constructs pretty easily. For example:

nextNumberIn: aStream
	aStream skipWhile: [:ch | ch isDigit not].
	^aStream nextUnitl: [:ch | ch isDigit not]

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

Read: A Peek at some more Extensions

Topic: Re: GLORP for TOPLink Users Previous Topic   Next Topic Topic: BottomFeeder 3.5 out

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use