Question - Projecting a collection onto a larger collection

Bert Freudenberg bert at isg.cs.uni-magdeburg.de
Fri Jun 20 18:23:02 UTC 2003


Am Freitag, 20.06.03 um 20:00 Uhr schrieb Ingo Hohmann:

> Hi Brent,
>
> Brent Pinkney wrote:
> <...>
>> e.g. I want to map {Date. Time } to { Date. Date class. Time. Time 
>> class }
>> I dimly remember seeing some mention of a project: method
>> I am looking for Something of the flavour:
>> 	{Date. Time } project: [ :nc :e | nc add: e; add: e class ].
>
> You may be thinking about inject:into: , which is used like this:
>
> {Date. Time} inject: {} into: [ :coll :each |
> 	coll _ coll copyWith: each. coll copyWith: each class]
>
> Which gives:
>
>  #(Date Date class Time Time class)

Actually, you should not store into an argument. I'd argue it is a bug 
that the compiler does not complain. It's unnecessary, too:

	{Date. Time} inject: {} into: [:coll :each | coll, {each. each class}]

That's about as short as you can get now. More efficient is this:

	Array streamContents: [:stream |
		{Date. Time} do: [:each | stream nextPut: each; nextPut: each class]]

This version does not unnecessarily create lots of intermediate 
collections.

-- Bert



More information about the Squeak-dev mailing list