(Dictionary new
at: 1 put: 'abc'; at: 2 put: 'abcd'; at: 3 put: 'abcde'; yourself) groupedBy: [:ea | ea size] ... blows up as per attached, whereas I would have expected it to return a dictionary with values re-keyed using the passed block? Cheers! -Boris -- +1.604.689.0322 DeepCove Labs Ltd. 4th floor 595 Howe Street Vancouver, Canada V6C 2T5 http://tinyurl.com/r7uw4 [hidden email] CONFIDENTIALITY NOTICE This email is intended only for the persons named in the message header. Unless otherwise indicated, it contains information that is private and confidential. If you have received it in error, please notify the sender and delete the entire message including any attachments. Thank you. _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc error.txt (33K) Download Attachment |
On Jul 28, 2008, at 8:52 AM, Boris Popov wrote:
> (Dictionary new > at: 1 put: 'abc'; > at: 2 put: 'abcd'; > at: 3 put: 'abcde'; > yourself) > groupedBy: [:ea | ea size] > > ... blows up as per attached, whereas I would have expected it to > return > a dictionary with values re-keyed using the passed block? Would you expect this: (Dictionary with: (3 -> (Dictionary with: 1 -> 'abc')) with: (4 -> (Dictionary with: 2 -> 'abcd')) with: (5 -> (Dictionary with: 3 -> 'abcde')) or (Dictionary with: (3 -> (OrderedCollection with: 'abc')) with: (4 -> (OrderedCollection with: 'abcd')) with: (5 -> (OrderedCollection with: 'abcde')) By default any grouping operation creates the second first. But then there's a 2nd part of the method that tries to be tricky. It basically tries to make the "groups" be the same type as the receiver. The idea being that if you do: (Bag withAll: #('abc' 'abcd' 'abcde')) groupedBy: #size, you get (Dictionary with: (3 -> (Bag with: 'abc')) with: (4 -> (Bag with: 'abcd')) with: (5 -> (Bag with: 'abcde')) I'm not sure what I think of the second stage trickery here. Part of me just thinks the groupedBy: should always do the base thing (OrderedCollections). The other half says that if we're going to make collect: return a dictionary, then we should do as #1 above does. We definitely shouldn't blow up, that's Bad (tm). A less efficient way of writing the method, but one that uses more of the existing services, rather than special casing the return value of species class, would be something like: groupedBy: aBlock | keys | keys := Set withAll: (self collect: aBlock). ^Dictionary withAll: (keys collect: [:eachKey | eachKey -> (self select: [:each | eachKey = (aBlock value: each)])]) This would produce the first result, and work for each receiver types in terms of it's collect:/select: semantics without having to futz around with species types, or recreating collections on a sometimes basis. It is however, about 2.5 x slower. Except for the current Dictionary case, which case it's infinitely faster because the original fails. -- Travis Griggs Objologist "Some people are like slinkies, not really good for much, but they can bring a smile to your face when you push them down the stairs." _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
Travis,
I would expect a second result, actually. If I have a collection of widgets keyed by integers (as is the case for arrays) or by gadgets (as is the case for dictionaries), grouping either one should be producing a dictionary where key is the result of evaluating a block and value is a collection of all items matching that result. I don't care about the original keys in either case... -Boris -- +1.604.689.0322 DeepCove Labs Ltd. 4th floor 595 Howe Street Vancouver, Canada V6C 2T5 http://tinyurl.com/r7uw4 [hidden email] CONFIDENTIALITY NOTICE This email is intended only for the persons named in the message header. Unless otherwise indicated, it contains information that is private and confidential. If you have received it in error, please notify the sender and delete the entire message including any attachments. Thank you. > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On Behalf > Of Travis Griggs > Sent: Monday, July 28, 2008 11:48 AM > To: VWNC List > Subject: Re: [vwnc] [7.6][BUG] Dictionary>>groupedBy: > > On Jul 28, 2008, at 8:52 AM, Boris Popov wrote: > > > (Dictionary new > > at: 1 put: 'abc'; > > at: 2 put: 'abcd'; > > at: 3 put: 'abcde'; > > yourself) > > groupedBy: [:ea | ea size] > > > > ... blows up as per attached, whereas I would have expected it to > > return > > a dictionary with values re-keyed using the passed block? > > > Would you expect this: > > (Dictionary > with: (3 -> (Dictionary with: 1 -> 'abc')) > with: (4 -> (Dictionary with: 2 -> 'abcd')) > with: (5 -> (Dictionary with: 3 -> 'abcde')) > > or > > (Dictionary > with: (3 -> (OrderedCollection with: 'abc')) > with: (4 -> (OrderedCollection with: 'abcd')) > with: (5 -> (OrderedCollection with: 'abcde')) > > > By default any grouping operation creates the second first. But then > there's a 2nd part of the method that tries to be tricky. It basically > tries to make the "groups" be the same type as the receiver. The idea > being that if you do: > > (Bag withAll: #('abc' 'abcd' 'abcde')) groupedBy: #size, you get > > (Dictionary > with: (3 -> (Bag with: 'abc')) > with: (4 -> (Bag with: 'abcd')) > with: (5 -> (Bag with: 'abcde')) > > I'm not sure what I think of the second stage trickery here. Part of > me just thinks the groupedBy: should always do the base thing > (OrderedCollections). The other half says that if we're going to make > collect: return a dictionary, then we should do as #1 above does. We > definitely shouldn't blow up, that's Bad (tm). > > A less efficient way of writing the method, but one that uses more of > the existing services, rather than special casing the return value of > species class, would be something like: > > groupedBy: aBlock > | keys | > keys := Set withAll: (self collect: aBlock). > ^Dictionary > withAll: > (keys > collect: > [:eachKey | eachKey -> (self > | eachKey = (aBlock > value: each)])]) > > This would produce the first result, and work for each receiver types > in terms of it's collect:/select: semantics without having to futz > around with species types, or recreating collections on a sometimes > basis. It is however, about 2.5 x slower. Except for the current > Dictionary case, which case it's infinitely faster because the > original fails. > > -- > Travis Griggs > Objologist > "Some people are like slinkies, not really good for much, but they can > bring a smile to your face when you push them down the stairs." > > _______________________________________________ > vwnc mailing list > [hidden email] > http://lists.cs.uiuc.edu/mailman/listinfo/vwnc _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
On Jul 28, 2008, at 12:07 PM, Boris Popov wrote:
> > I would expect a second result, actually. If I have a collection of > widgets keyed by integers (as is the case for arrays) or by gadgets > (as > is the case for dictionaries), grouping either one should be > producing a > dictionary where key is the result of evaluating a block and value > is a > collection of all items matching that result. I don't care about the > original keys in either case... So what about the Bag case, what would you expect there? -- Travis Griggs Objologist "I think that we should be men first, and subjects afterward." - Henry David Thoreau _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
Yea, I see what you're getting at; you want values to always be the same
type of a collection that you start with... In that case I guess I could always do, aDictionary values groupedBy: [:ea | ea size] in cases where I want to re-key the whole thing, but then you'd still want to fix the error for direct groupedBy: sends. -Boris -- +1.604.689.0322 DeepCove Labs Ltd. 4th floor 595 Howe Street Vancouver, Canada V6C 2T5 http://tinyurl.com/r7uw4 [hidden email] CONFIDENTIALITY NOTICE This email is intended only for the persons named in the message header. Unless otherwise indicated, it contains information that is private and confidential. If you have received it in error, please notify the sender and delete the entire message including any attachments. Thank you. > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On Behalf > Of Travis Griggs > Sent: Monday, July 28, 2008 12:19 PM > To: VWNC List > Subject: Re: [vwnc] [7.6][BUG] Dictionary>>groupedBy: > > On Jul 28, 2008, at 12:07 PM, Boris Popov wrote: > > > > > I would expect a second result, actually. If I have a collection of > > widgets keyed by integers (as is the case for arrays) or by gadgets > > (as > > is the case for dictionaries), grouping either one should be > > producing a > > dictionary where key is the result of evaluating a block and value > > is a > > collection of all items matching that result. I don't care about the > > original keys in either case... > > So what about the Bag case, what would you expect there? > > -- > Travis Griggs > Objologist > "I think that we should be men first, and subjects afterward." - Henry > David Thoreau > > > > _______________________________________________ > vwnc mailing list > [hidden email] > http://lists.cs.uiuc.edu/mailman/listinfo/vwnc _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
In reply to this post by Boris Popov, DeepCove Labs (SNN)
On Jul 28, 2008, at 12:07 PM, Boris Popov wrote:
> Travis, > > I would expect a second result, actually. If I have a collection of > widgets keyed by integers (as is the case for arrays) or by gadgets > (as > is the case for dictionaries), grouping either one should be > producing a > dictionary where key is the result of evaluating a block and value > is a > collection of all items matching that result. I don't care about the > original keys in either case... Restating, just to make sure, you're looking for a Dictionary transform function, which operates on the keys, rather than on values? Is that correct? i.e. (Dictionary with: 4 -> 5 with: 3 -> 7) borisFunctor: #asFloat --> (Dictionary with: 4.0 -> 5 with: 3.0 -> 7) Or am I still being dense? You discovered a bug in groupedBy: either way :) -- Travis Griggs Objologist "I think that we should be men first, and subjects afterward." - Henry David Thoreau _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
Umm, I wasn't thinking that far, all I was trying to do is re-key my
dictionary using another parameter. Say you have a dictionary of widgets keyed by price and you want to get a dictionary of widgets keyed by color. My natural instinct would be to say, $10.00 -> #(aWidget bWidget) $20.00 -> #(cWidget) myWidgetsByPrice groupedBy: #color #blue -> #(aWidget) #green -> #(bWidget) #yellow -> #(cWidget) whereas it appears I really need to do, myWidgetsByPrice values groupedBy: #color -Boris -- +1.604.689.0322 DeepCove Labs Ltd. 4th floor 595 Howe Street Vancouver, Canada V6C 2T5 http://tinyurl.com/r7uw4 [hidden email] CONFIDENTIALITY NOTICE This email is intended only for the persons named in the message header. Unless otherwise indicated, it contains information that is private and confidential. If you have received it in error, please notify the sender and delete the entire message including any attachments. Thank you. > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On Behalf > Of Travis Griggs > Sent: Monday, July 28, 2008 12:46 PM > To: VWNC List > Subject: Re: [vwnc] [7.6][BUG] Dictionary>>groupedBy: > > On Jul 28, 2008, at 12:07 PM, Boris Popov wrote: > > > Travis, > > > > I would expect a second result, actually. If I have a collection of > > widgets keyed by integers (as is the case for arrays) or by gadgets > > (as > > is the case for dictionaries), grouping either one should be > > producing a > > dictionary where key is the result of evaluating a block and value > > is a > > collection of all items matching that result. I don't care about the > > original keys in either case... > > Restating, just to make sure, you're looking for a Dictionary > transform function, which operates on the keys, rather than on values? > Is that correct? > > i.e. > > (Dictionary with: 4 -> 5 with: 3 -> 7) borisFunctor: #asFloat --> > (Dictionary with: 4.0 -> 5 with: 3.0 -> 7) > > Or am I still being dense? > > You discovered a bug in groupedBy: either way :) > > -- > Travis Griggs > Objologist > "I think that we should be men first, and subjects afterward." - Henry > David Thoreau > > > > _______________________________________________ > vwnc mailing list > [hidden email] > http://lists.cs.uiuc.edu/mailman/listinfo/vwnc _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
Actually, I would also need to fold the values,
(((Dictionary new) at: 10 put: #('a' 'bb'); at: 20 put: #('ccc'); yourself) values fold: [:a :b | a , b]) groupedBy: [:ea | ea size] I might just end up with a specialized implementation for re-keying and let you fix the default implementation of groupedBy: on Dictionaries. -Boris -- +1.604.689.0322 DeepCove Labs Ltd. 4th floor 595 Howe Street Vancouver, Canada V6C 2T5 http://tinyurl.com/r7uw4 [hidden email] CONFIDENTIALITY NOTICE This email is intended only for the persons named in the message header. Unless otherwise indicated, it contains information that is private and confidential. If you have received it in error, please notify the sender and delete the entire message including any attachments. Thank you. > -----Original Message----- > From: Boris Popov > Sent: Monday, July 28, 2008 1:03 PM > To: 'Travis Griggs'; VWNC List > Subject: RE: [vwnc] [7.6][BUG] Dictionary>>groupedBy: > > Umm, I wasn't thinking that far, all I was trying to do is re-key my > dictionary using another parameter. Say you have a dictionary of widgets > keyed by price and you want to get a dictionary of widgets keyed by color. > My natural instinct would be to say, > > $10.00 -> #(aWidget bWidget) > $20.00 -> #(cWidget) > > myWidgetsByPrice groupedBy: #color > > #blue -> #(aWidget) > #green -> #(bWidget) > #yellow -> #(cWidget) > > whereas it appears I really need to do, > > myWidgetsByPrice values groupedBy: #color > > -Boris > > -- > +1.604.689.0322 > DeepCove Labs Ltd. > 4th floor 595 Howe Street > Vancouver, Canada V6C 2T5 > http://tinyurl.com/r7uw4 > > [hidden email] > > CONFIDENTIALITY NOTICE > > This email is intended only for the persons named in the message > header. Unless otherwise indicated, it contains information that is > private and confidential. If you have received it in error, please > notify the sender and delete the entire message including any > attachments. > > Thank you. > > > -----Original Message----- > > From: [hidden email] [mailto:[hidden email]] On > Behalf > > Of Travis Griggs > > Sent: Monday, July 28, 2008 12:46 PM > > To: VWNC List > > Subject: Re: [vwnc] [7.6][BUG] Dictionary>>groupedBy: > > > > On Jul 28, 2008, at 12:07 PM, Boris Popov wrote: > > > > > Travis, > > > > > > I would expect a second result, actually. If I have a collection > > > widgets keyed by integers (as is the case for arrays) or by gadgets > > > (as > > > is the case for dictionaries), grouping either one should be > > > producing a > > > dictionary where key is the result of evaluating a block and value > > > is a > > > collection of all items matching that result. I don't care about the > > > original keys in either case... > > > > Restating, just to make sure, you're looking for a Dictionary > > transform function, which operates on the keys, rather than on values? > > Is that correct? > > > > i.e. > > > > (Dictionary with: 4 -> 5 with: 3 -> 7) borisFunctor: #asFloat --> > > (Dictionary with: 4.0 -> 5 with: 3.0 -> 7) > > > > Or am I still being dense? > > > > You discovered a bug in groupedBy: either way :) > > > > -- > > Travis Griggs > > Objologist > > "I think that we should be men first, and subjects afterward." - > > David Thoreau > > > > > > > > _______________________________________________ > > vwnc mailing list > > [hidden email] > > http://lists.cs.uiuc.edu/mailman/listinfo/vwnc _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
In reply to this post by Boris Popov, DeepCove Labs (SNN)
Boris Popov wrote:
> Umm, I wasn't thinking that far, all I was trying to do is re-key my > dictionary using another parameter. Say you have a dictionary of widgets > keyed by price and you want to get a dictionary of widgets keyed by > color. My natural instinct would be to say, > > $10.00 -> #(aWidget bWidget) > $20.00 -> #(cWidget) > > myWidgetsByPrice groupedBy: #color > > #blue -> #(aWidget) > #green -> #(bWidget) > #yellow -> #(cWidget) > > whereas it appears I really need to do, > > myWidgetsByPrice values groupedBy: #color > that match the new key - so if your original dictionary had values that are collections, the new dictionary will have values that are collections of collections. It sort of seems like you need a specific API for your problem here... Michael _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
In reply to this post by Boris Popov, DeepCove Labs (SNN)
Boris, I did submit an AR (54875) for this. Thanks
On Jul 28, 2008, at 8:52 AM, Boris Popov wrote: > (Dictionary new > at: 1 put: 'abc'; > at: 2 put: 'abcd'; > at: 3 put: 'abcde'; > yourself) > groupedBy: [:ea | ea size] > > ... blows up as per attached, whereas I would have expected it to > return > a dictionary with values re-keyed using the passed block? > > Cheers! > > -Boris > > -- > +1.604.689.0322 > DeepCove Labs Ltd. > 4th floor 595 Howe Street > Vancouver, Canada V6C 2T5 > http://tinyurl.com/r7uw4 > > [hidden email] > > CONFIDENTIALITY NOTICE > > This email is intended only for the persons named in the message > header. Unless otherwise indicated, it contains information that is > private and confidential. If you have received it in error, please > notify the sender and delete the entire message including any > attachments. > > Thank you. > > <error.txt>_______________________________________________ > vwnc mailing list > [hidden email] > http://lists.cs.uiuc.edu/mailman/listinfo/vwnc -- Travis Griggs Objologist "I think that we should be men first, and subjects afterward." - Henry David Thoreau _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
Boris,
Would this do for your case? --- Collection>>allElementsGroupedBy: a1block | regrouped | regrouped := Dictionary new. self allElementsDo: [:each| key := a1block value: each. regrouped at: key add: each. ]. ^regrouped allElementsDo: a1block ^self do: [:each| each allElementsDo: a1block] Object>>allElementsDo: a1block ^a1block value: selfTravis, --- Travis, For #groupedBy:, your examples look like they answer something I 'd call a thesaurous. Buta bea carfull, ok, becausea thesa U rous', are, asa you know, asometimes quitea dangerous... Regards, -cstb --- Collection>>groupedBy: a1block | new | grouped := Thesaurus of: self class. self elementsDo: [:each| | key | key := a1block value: each. grouped at: key add: each ]. ^grouped >>addElement: anObject ^self add: anObject >>elementsDo: a1block ^self do: a1block Dictionary>>addElement: anAssociation self addAssociation: anAssociation >>elementsDo: a1block ^self associationsDo: a1block --- Here's a minimal thesaurus, for #groupedBy: --- Collection subclass: Thesaurus iVars: 'newEntry entryClass' Thesaurus class>>of: aCollectionClass ^self new setEntryClass: aCollectionClass ; yourself >>initialize super initialize. self setEntryClass: OrderedCollection >>setEntryClass: aCollectionClass newEntry := [entryClass new]. ^entryClass := aCollectionClass >>at: key add: anObject | entry | entry := self at: key ifAbsentPut: newEntry. ^entry addElement: anObject Regards, -cstb At 12:59 PM 7/31/2008, Travis Griggs wrote: >Boris, I did submit an AR (54875) for this. Thanks > > >On Jul 28, 2008, at 8:52 AM, Boris Popov wrote: > >> (Dictionary new >> at: 1 put: 'abc'; >> at: 2 put: 'abcd'; >> at: 3 put: 'abcde'; >> yourself) >> groupedBy: [:ea | ea size] >> >> ... blows up as per attached, whereas I would have expected it to >> return >> a dictionary with values re-keyed using the passed block? >> >> Cheers! >> >> -Boris >> >> -- >> +1.604.689.0322 >> DeepCove Labs Ltd. >> 4th floor 595 Howe Street >> Vancouver, Canada V6C 2T5 >> http://tinyurl.com/r7uw4 >> >> [hidden email] >> >> CONFIDENTIALITY NOTICE >> >> This email is intended only for the persons named in the message >> header. Unless otherwise indicated, it contains information that is >> private and confidential. If you have received it in error, please >> notify the sender and delete the entire message including any >> attachments. >> >> Thank you. >> >> <error.txt>_______________________________________________ >> vwnc mailing list >> [hidden email] >> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc > >-- >Travis Griggs >Objologist >"I think that we should be men first, and subjects afterward." - Henry >David Thoreau > > > >_______________________________________________ >vwnc mailing list >[hidden email] >http://lists.cs.uiuc.edu/mailman/listinfo/vwnc _______________________________________________ vwnc mailing list [hidden email] http://lists.cs.uiuc.edu/mailman/listinfo/vwnc |
Free forum by Nabble | Edit this page |