Issues with #collect: (Was: Re: [squeak-dev] Splitting Set and
Dictionary)
Levente Uzonyi
leves at elte.hu
Sat Nov 21 04:02:49 UTC 2009
Hi,
I uploaded a proposal to the inbox (Collections-ul.205) which tries to
solve the issues with #collect:.
The idea is to let #collect: use #species and add two new interface
methods to the Collections hierarchy to handle exceptional cases. The two
methods are: #collect:into: and #collect:as:.
Both work like #collect:, but #collect:into: expects a Collection as it's
second argument and collects the values into that collection. #collect:as:
expects a subclass of Collection and returns an instance filled with the
collected values.
A few examples demonstrate possible usage:
(1 to: 9) collect: [ :each | each asString first ] as: ByteString
==> '123456789'.
(1 to: 9) collect: [ :each | each \\ 5 ] as: Set
==> a Set(0 1 2 3 4)
(1 to: 9) asSet collect: [ :each | 10 - each ] as: SortedCollection
==> a SortedCollection(1 2 3 4 5 6 7 8 9)
#(1 5 2 4 3) collect: #yourself as: Heap
==> a Heap(1 3 2 5 4)
(Dictionary newFrom: { #one -> 1. #two -> 2 }) collect: #squared as: IdentityDictionary
==> an IdentityDictionary(#one->1 #two->4 ).
(Dictionary newFrom: { #one -> 1. #two -> 2 }) collect: #squared as: Array
==> #(4 1)
(1 to: 10) collect: [ :each | each > 1 and: [ each isPrime ] ] as: Dictionary
==> a Dictionary(1->false 2->true 3->true 4->false 5->true 6->false 7->true 8->false 9->false 10->false )
#(20 30 40) collect: [ :each | each // 10 ] as: IdentityDictionary
==> an IdentityDictionary(1->2 2->3 3->4 )
a := #(1 2 3). a collect: #squared into: a
==> #(1 4 9).
Even #collect: could be implemented as: ^self collect: aBlock as: self species.
Of course there are combinations which don't work, but i think those
don't make much sense. For example you can't collect a Set into a
Dictionary.
What do you think?
Cheers,
Levente
> On Fri, 20 Nov 2009, Igor Stasenko wrote:
>
>> hmm.. WeakSet/IdentitySet>>collect: returning an instance of Set?
More information about the Squeak-dev
mailing list
|