> Hi, this is my first post (please be gentle xD)
Thanks!
>In VW you get the same.
> The #collect: message commonly return a new collection like the receiver, but this is really unpractical in some cases
>
> e.g.
> col := Set with: 'one'->1 with: 'two'->2 with: 'dos'->2.
> (col collect: [:each | each value ]) sum
> In this example #sum will return 3 instead of 5 (I searched a test for #sum but I didn�t find it).I think that this is the invariant for collect: ?
>
> Ok, I can use the message #asBag (or something like that) but does the second line have to depend on the type of the receiver (i.e. the type of col)?
> For example, the implementation of #collect: in SortedCollection returns an OrderedCollection (in the current image)in VW too
> and in other dialects the message #collect: when the receiver is a Set returns a Bag ...which ones?
> Basically, what do you think of an implementation like this?
>
> Set >> collect: aBlock
> �"Evaluate aBlock with each of the receiver's elements as the argument.
> �Collect the resulting values into a Bag. Answer the new collection."
> �| newBag |
> �newBag := Bag new: self size.
> �array do: [:each | each ifNotNil: [newBag add: (aBlock value: each)]].
> �^ newBag
True.It can be impractical, but no need to change #collect:
Please use #detectSum:
(Set with: 'one'->1 with: 'two'->2 with: 'dos'->2) detectSum: [:e |e value].
�
or #collect:as:
((Set with: 'one'->1 with: 'two'->2 with: 'dos'->2) collect: [:e |e
value] as: Array) sum.
Nicolas
2010/5/3 Germ�n Leiva <leivagerman@gmail.com>:
> Hi, this is my first post (please be gentle xD)
>
> The #collect: message commonly return a new collection�like the receiver,
> but this is really unpractical in some cases
> e.g.
> col := Set with: 'one'->1 with: 'two'->2 with: 'dos'->2.
> (col collect: [:each | each value ]) sum
> In this example #sum will return 3 instead of 5 (I searched a test for #sum
> but I didn�t find it).
>
> Ok, I can use the message #asBag (or something like that) but does the
> second line have to depend on the type of the�receiver (i.e. the type of
> col)?
> For example, the implementation of #collect: in SortedCollection returns an
> OrderedCollection (in the current image) and in other dialects the message
> #collect: when the receiver is a Set returns a Bag ...
>
> Basically, what do you think of an implementation like this?
>
> Set >> collect: aBlock
> �"Evaluate aBlock with each of the receiver's elements as the argument.
> �Collect the resulting values into a Bag. Answer�the new collection."
> �| newBag |
> �newBag := Bag new: self size.
> �array do: [:each | each ifNotNil: [newBag add: (aBlock value: each)]].
> �^ newBag
> Cordially,
> --
> Germ�n Leiva
> LeivaGerman@gmail.com
>
> _______________________________________________
> Pharo-project mailing list
> Pharo-project@lists.gforge.inria.fr
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>
_______________________________________________
Pharo-project mailing list
Pharo-project@lists.gforge.inria.fr
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project