On 13.03.2010 00:14, csra...@bol.com.br wrote:
Although the end result should be the same, I wonder if for a collection of 
items where there is a lot of duplicates the sequential nature of collecting 
and then converting to a Set wouldn't be less performant than Mariano's 
proposed method?

--
Cesar Rabak
In my experience, yes, it would.
The difference in practice is usually so small you wouldn't prefer one over the other unless it show up in a profile of a time-critical method though.

Cheers,
Henry

Few distinct:
[(1 to: 500000) collect: [:each | each odd] as: Set] timeToRun 1347

[|aSet |
aSet := Set new.
(1 to: 500000) do: [:each | aSet add: each odd] ] timeToRun 375

All distinct:
[(1 to: 500000) collect: [:each | each +1] as: Set] timeToRun  1350

[|aSet |
aSet := Set new: 500000.
(1 to: 500000) do: [:each | aSet add: each +1] ] timeToRun  1740




Em 10/03/2010 10:07, Nicolas Cellier<  nicolas.cellier.aka.n...@gmail.com>  
escreveu:
self collect: aBlock as: Set

2010/3/10 Mariano Martinez Peck :
Hi. I was needing something like the SQL select distinct, that doesn't take
into account repeated objects. I didn't found anything useful in Collection,
and thus, I have implemented this:

Collection>>  collectDistinct: aBlock


collectDistinct: aBlock
     "Evaluate aBlock with each of the receiver's elements as the argument.
     Collect the resulting values into a Set, thus repeated objects will not
be present.
     Answer the new collection."

     | newSet |
     newSet := self species new asSet.
     self do: [:each | newSet add: (aBlock value: each)].
     ^ newSet


Is there a better way ?   Do you think it make sense to put this in Pharo ?

Cheers

Mariano

_______________________________________________
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


_______________________________________________
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

Reply via email to