(newbie) String / Array / ArrayedCollection

Lukas Renggli renggli at gmail.com
Sat Dec 31 12:34:48 UTC 2005


> very useful pair of goodies (and I got tired of typing
> componentsSeparatedByString: on nextstep).

I found myself implementing a message called #fold: several times (I
got the idea from functional languages like Haskell, that provide such
a function in their standard library), allowing to write the above
problem like:

	'A series of words to be shuffled' substrings shuffled
		fold: [ :a :b | a , ' ' , b ].

A possible implementation of #fold: could look like:

	SequenceableCollection>>fold: aBlock
		| result |
		result := self first.
		2 to: self size do: [ :index |
			result := aBlock
				value: result
				value: (self at: index) ].
		^ result.

Summing up the numbers from 1 to 100 can be done as easily as:

	(1 to: 100) fold: [ :a :b | a + b ].

I think this is much more useful and generic than to add string
specific functions like #join:, #joinOn:, ...

Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch



More information about the Squeak-dev mailing list