[ANSI-Smalltalk] Behaviour of #collect:

Andrew Wakeling andrew.wakeling at objective.com
Thu Oct 9 06:52:21 BST 2008


Sorry for my belated response to this thread.

We have solved a similar pattern/problem in such a way:

aCollection collectAsSet: [:item | item foo]

This could be assisted with helper method such as:

Collection>>collectAsSpecies: aSpecies from: aBlock

(or even Collection>>collectAsSpecies: aSpecies using: aBlock)

We did a similar "optimization" with Dictionary>>value (as it is
implemented to always return a Bag, which was expensive as it was doing
unwanted equality checks). (e.g. Dictionary>>valuesAsOrderedCollection)

Sure, for my above example (with Sets), the same thing could be achieved
doing:
aCollection asSet: [:item | item foo]

however for large collections there will probably be a consideration
performance difference, especially if you items you're checking have an
expensive equality method.

Using a class method (or constructor) is fine too, however the above
approach is easier to access through our AutoComplete IDE tool.

Just my thoughts.

Regards,
Zak

-----Original Message-----
From: ansi-smalltalk-bounces at lists.openskills.org
[mailto:ansi-smalltalk-bounces at lists.openskills.org] On Behalf Of Paolo
Bonzini
Sent: Thursday, 25 September 2008 7:56 PM
To: Discussion list for the ANSI Smalltalk project
Subject: Re: [ANSI-Smalltalk] Behaviour of #collect:

Andres Valloud wrote:
> Or this?...
> 
> 'abc' collect: [:x | x codePoint] into: Array

But this cannot be implemented except as a call to

  Array collect: ... from: ...

(or whatever the selector), because it's the new collection's class that
knows how to create the new collection.

Paolo

_______________________________________________
ANSI-Smalltalk mailing list
ANSI-Smalltalk at lists.openskills.org
http://lists.openskills.org/cgi-bin/mailman/listinfo/ansi-smalltalk



More information about the ANSI-Smalltalk mailing list