Question - Projecting a collection onto a larger collection

Ned Konz ned at bike-nomad.com
Fri Jun 20 14:14:55 UTC 2003


On Friday 20 June 2003 05:31 am, Brent Pinkney wrote:
> Hi,
>
> What is the preffered method of porjecting on collection onto a
> larger collection ?
>
> collect:, select: and reject only map a collection onto a smaller
> collection.
>
> 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 ].
>
> nc is an appropriate collection.
>
> I know I can achieve this in multiple lines of code - is there a
> preferred method ?

PackageInfo (now in the image) provides #gather: which flattens to one 
level:

{ Date. Time } gather: [ :ea | { ea. ea class } ].

Ruby has a "flatten" method that does a recursive flattening of 
Arrays. A generic flatten method for Collections would have the 
effect of blowing out Strings into Characters, though.

There is a #flattenOnStream: method already in the image; by defining 
its write methods on Stream you get the same behavior. See the 
attached CS for a test of this.

-- 
Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Flatten-nk.1.cs
Type: text/x-squeak-changeset
Size: 1217 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20030620/58cbdaee/Flatten-nk.1.bin


More information about the Squeak-dev mailing list