Collecting Smalltalk idioms

agree at carltonfields.com agree at carltonfields.com
Tue Aug 10 21:47:39 UTC 1999


> 	(ReadStream on: aCollection) do: [ :each |
> 		each printOn: aStream.
> 		aStream atEnd ifFalse: [ aStream nextPut: $,]]

I'm not sure quoted code works.  The atEnd test is made on aStream, which is the OUTPUT stream, not the input stream.  In my original message, I suggested the following example (which had a typo exactly like the quoted code, by the way), recast here for clarity:

	inStream _ ReadStream on: aCollection.
	[inStream atEnd] whileFalse:
		[<Your "per-element" code here, referencing "inStream next">.
		 inStream atEnd ifFalse: [<Your delimiter code here>]]

The following also works, looking a bit more like the quoted passage:

 	inStream _ ReadStream on: aCollection.
	inStream do: 		[:each |
		 <Your "per-element" code here, referencing "each">.
		 inStream atEnd ifFalse: [<Your delimiter code here>]]

I still find myself strangely drawn to Marcel's template:

	aCollection inject: [] into:
		[:middleBlock :each |
		 middleBlock value.
		 <Your "per-element" code here, referencing "each">.
		 [<Your delimiter code here>]]

in preference to:

	aCollection inject: true into:
		[:isFirst :each |
		 isFirst ifFalse: [<Your delimiter code here>].
		 <Your "per-element" code here, referencing "each">.
		 false]

And so it goes.  I ask the question then, which is the preferable course when building an application (hopefully) of general utility for the Squeak community:

	(1) Use one of the above idioms when the issue arises;
	(2) Use another idiom;
	(3) Extend Collection with #do:separatedBy:, however implemented; or
	(4) Lobby Squeak Central to add #do:separatedBy:?

I, for one, am quite glad that I asked the stupid question, for I found the discussion entertaining and illuminating.  Thanks to all for your insights.





More information about the Squeak-dev mailing list