On Thu, 11 Mar 2010, Henrik Johansen wrote:
Den 11.03.2010 01:54, skrev Nicolas Cellier:
2010/3/11 Igor Stasenko <siguctua@gmail.com>:
On 10 March 2010 15:07, Nicolas Cellier <nicolas.cellier.aka.nice@gmail.com> wrote:
self collect: aBlock as: Set
One more time, i found this very beatiful and useful extension to Collection protocol. And i am proud being a witness when this thing is born! :)
Yes, I remember saying I was not convinced some times ago... http://lists.squeakfoundation.org/pipermail/squeak-dev/2008-June/129457.html Being able to change own opinion is a good thing !
Nicolas
I have to admit, I still don't quite see what's wrong with 1. (self collect: aBlock) asSet
('foo' collect: [ :each | each asciiValue ]) asSet "===> boom :)"
or 2. mySet := Set new: self size. self do: aBlockAddingToMySetAtEnd.
That's ok, but it's two lines + a temporary declaration.
or (well, this one is IMO indeed a bit ugly) 3. self inject: (Set new:self size) into: [ :sub :next | sub add: next collectValue; yourself ] ...
Sure, they're a bit verbose, and when you'd like to use 1 vs 2 or 3 varies abit on the collection contents, but they all work in all dialects, and aren't THAT much longer, unless your collect block is really short.
I did a quick sampling (not representative) and found that 1 out of 20 collect blocks is longer than a single line. #collect:as: is a solution to the problem about #species which limits the use of #collect: in some cases. You can find the proposal about #collect:as: here if you're interested: http://lists.squeakfoundation.org/pipermail/squeak-dev/2009-November/141245.... And who knows, maybe other dialects will implement these methods too, until then, don't use #collect:as: in code which should be portable across dialects. Levente
Cheers, Henry
2010/3/10 Mariano Martinez Peck <marianopeck@gmail.com>:
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
-- Best regards, Igor Stasenko AKA sig.
_______________________________________________ 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