Em 10/03/2010 09:50, Mariano Martinez Peck < marianopeck@gmail.com > escreveu:
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,
IIUC, your intent with this method is/was to get non repeated elements in a generic collection. However, the way is coded the result is Set which is returned instead of the collection from which the method had been called. I didn't miss something, the result of your proposed method is akin to calling Collection>>collect: and sending asSet to it. ITYM: | newSet | newSet := Set new . self do: [:each | newSet add: (aBlock value: each)]. ^newSet as: (self class) HTH -- Cesar Rabak