2011/10/27 Clara Allende <[email protected]>: > Ok, but this means I have to worry of the receiver's class before I send > collect:, so as I don't fuck it all up.... and I don't want to think about > that!!! See, if I want the collection of grades of the students, I would > like to do this: > grades > ^self students collect: [.aStudent | aStudent grades] > regardless of whatever is the class of students... This does the trick for > all the collections, except for Set. And I don´t know if students is an > instance of Set, and I really don't want to think if it is or not. If I > don't want repeated students and I create my collection as an instance of > Set, an i.e.; I want to calculate the average, then I'm screwed because the > collection I get from the collect: wasn't the want that I wanted. > So, I have to know previously that my collection is an instance of Set, or > if it isn't, so as to know if I have to convert it first.... > And if I use collect: as:, as you suggest, this means the collections are no > longer polymorphic for me :( > Sorry, maybe I'm really stupid, but I don't know how that message solves > best the problem than redefining collect: for Set. For the particular case I > don't want the repeated objects (which AFAIK is not so common) I could send > asSet. >
Hi Clara. IMHO, collect: [:e | e even] snippet makes not much sense, because Set are Unordered. Thus you won't be able to map the order of original Set with produced collection... So what will you do of this collection of true and false ? If you want to count the elements matching your predicate, just tell your intentions so (Set with: 1 with: 2 with:3 with:4 with:5) count:[:e | e even] Collecting grade is more interesting, but we can also answer to you by another question: Why the hell are your students stored in a Set? Do you fear duplicates? or do you want to filter homozygotes out? Why should the choice of collection class be neutral? They all behave differently w.r.t. a message or another... And to me, #collect: is polymorphically suspect, did you try 'bogito ergo sum' collect: [:e | e isVowel]. #[ 0 1 2] collect: [:e | 1 - e ]. Nicolas > On 27 October 2011 11:05, Henrik Sperre Johansen > <[email protected]> wrote: >> >> On 27.10.2011 15:40, Clara Allende wrote: >>> >>> I know, but our students don't :) So they ran into problems because the >>> message send didn't answer what they were specting.... because it makes >>> sense that if I want to transform the objects in my collection, I might get >>> repeated objects... Maybe I'm not thinking in terms of consistency, I'm just >>> putting myself on student's shoes :P >> >> The consistency rule to remember for #collect:/#select:/#reject: is that >> they will return a collection of the receiver's species. >> #collect:as: was added as a general answer to the situation your students >> ran into :) >> >> set := Set withAll: #(1 2 3 4 5 6 7 8). >> result := set collect: #even as: Bag. >> >> Cheers, >> Henry >> >> >> > > > > -- > > "Most good programmers do programming not because they expect to get paid or > get adulation by the public, but because it is fun to program." > > Linus Torvalds >
