# Why can't a Bag answer as a dictionary?

13 messages
Open this post in threaded view
|

## Why can't a Bag answer as a dictionary?

 I was surprised to find that a Bag can’t convert to a dictionary - e.g. Bag new         addAll: 'aabbbbcddd’;         asDictionary Gives an error - Dnu #key It looks to me like Bag is inheriting a bad version of #associationsDo:  and instead could simply forward it to #doWithOccurences: instead? I know Bag is not used that much - but it comes up a lot in programming exercises. Tim
Open this post in threaded view
|

## Re: Why can't a Bag answer as a dictionary?

 Why would that work ? What would you expect the output to be ? Try:   #(1 2 3) asDictionary it fails in exactly the same way. You need key/value pairs (Associations). These do work   Bag new add: #foo->100; asDictionary.   Bag new addAll: 'ABABABAAAA'; valuesAndCounts. > On 6 Mar 2019, at 14:25, Tim Mackinnon <[hidden email]> wrote: > > I was surprised to find that a Bag can’t convert to a dictionary - e.g. > > Bag new > addAll: 'aabbbbcddd’; > asDictionary > > Gives an error - Dnu #key > > > It looks to me like Bag is inheriting a bad version of #associationsDo:  and instead could simply forward it to #doWithOccurences: instead? > > I know Bag is not used that much - but it comes up a lot in programming exercises. > > Tim
Open this post in threaded view
|

## Re: Why can't a Bag answer as a dictionary?

 Doubtless the original poster expected aBag asDictionaryto answer a dictionary mapping the (distinct) elements ofthe bag to their counts.  Mathematically, a bag canusefully be seen as a partial function from a set U tothe positive integers, and a dictionary with U keys andpositive integer values is precisely that.It's really not unreasonable to expect that,considering that there *is* a method that does justwhat the OP wanted: #valuesAndCounts.  It's ratherdangerous to expose private state like that, but itis not a private method.  I would expectasDictionary  ^contents copyOn Thu, 7 Mar 2019 at 02:54, Sven Van Caekenberghe <[hidden email]> wrote:Why would that work ? What would you expect the output to be ? Try:   #(1 2 3) asDictionary it fails in exactly the same way. You need key/value pairs (Associations). These do work   Bag new add: #foo->100; asDictionary.   Bag new addAll: 'ABABABAAAA'; valuesAndCounts. > On 6 Mar 2019, at 14:25, Tim Mackinnon <[hidden email]> wrote: > > I was surprised to find that a Bag can’t convert to a dictionary - e.g. > > Bag new >       addAll: 'aabbbbcddd’; >       asDictionary > > Gives an error - Dnu #key > > > It looks to me like Bag is inheriting a bad version of #associationsDo:  and instead could simply forward it to #doWithOccurences: instead? > > I know Bag is not used that much - but it comes up a lot in programming exercises. > > Tim
Open this post in threaded view
|

## Re: Why can't a Bag answer as a dictionary?

 In reply to this post by Sven Van Caekenberghe-2 As Richard said - as a bag is relationship between keys and frequencies, I would expect it to be able to convert to a dictionary.It displays in an inspector just like a Dictionary - which is why I figured I could convert to pass it back to the exercise that was written with Dictionaries in mind.The code to fix it is quite simple, but on these kinds of things - I thought it worth checking before submitting a PR.TimOn 6 Mar 2019, at 13:53, Sven Van Caekenberghe <[hidden email]> wrote:Why would that work ? What would you expect the output to be ?Try:  #(1 2 3) asDictionary it fails in exactly the same way. You need key/value pairs (Associations).These do work  Bag new add: #foo->100; asDictionary.  Bag new addAll: 'ABABABAAAA'; valuesAndCounts.On 6 Mar 2019, at 14:25, Tim Mackinnon <[hidden email]> wrote:I was surprised to find that a Bag can’t convert to a dictionary - e.g.Bag new addAll: 'aabbbbcddd’; asDictionary Gives an error - Dnu #keyIt looks to me like Bag is inheriting a bad version of #associationsDo:  and instead could simply forward it to #doWithOccurences: instead?I know Bag is not used that much - but it comes up a lot in programming exercises.Tim
Open this post in threaded view
|

## Re: Why can't a Bag answer as a dictionary?

 I was just explaining how it is now, what I think the rationale is behind it. I understand #asDictionary as working on a collection of pairs/associations (because it basically goes to #withAll:). A bag is just a collection that is optimised for many duplicates, the fact that you have values and counts is more an implementation detail than an intrinsic property. The conversion that you want, and that already exists in #valuesAndCounts is one interpretation of what a bag is, not the only one. A bag is foremost a collection of things. I am not immediately convinced that #valuesAndCounts should be the default #asDictionary interpretation. What about the inverse for example ?   { #foo->2. #bar->3 } asDictionary asBag. But this is certainly an interesting discussion. > On 6 Mar 2019, at 16:23, Tim Mackinnon <[hidden email]> wrote: > > As Richard said - as a bag is relationship between keys and frequencies, I would expect it to be able to convert to a dictionary. > > It displays in an inspector just like a Dictionary - which is why I figured I could convert to pass it back to the exercise that was written with Dictionaries in mind. > > > > > The code to fix it is quite simple, but on these kinds of things - I thought it worth checking before submitting a PR. > > Tim > > >> On 6 Mar 2019, at 13:53, Sven Van Caekenberghe <[hidden email]> wrote: >> >> Why would that work ? What would you expect the output to be ? >> >> Try: >> >>  #(1 2 3) asDictionary >> >> it fails in exactly the same way. You need key/value pairs (Associations). >> >> These do work >> >>  Bag new add: #foo->100; asDictionary. >> >>  Bag new addAll: 'ABABABAAAA'; valuesAndCounts. >> >>> On 6 Mar 2019, at 14:25, Tim Mackinnon <[hidden email]> wrote: >>> >>> I was surprised to find that a Bag can’t convert to a dictionary - e.g. >>> >>> Bag new >>> addAll: 'aabbbbcddd’; >>> asDictionary >>> >>> Gives an error - Dnu #key >>> >>> >>> It looks to me like Bag is inheriting a bad version of #associationsDo:  and instead could simply forward it to #doWithOccurences: instead? >>> >>> I know Bag is not used that much - but it comes up a lot in programming exercises. >>> >>> Tim >> >> >
Open this post in threaded view
|

## Re: Why can't a Bag answer as a dictionary?

 And when asking the object:#(1 2 4 5 6 7 2 1 4 3) asBag  isDictionarywe get false. So, not, not a dictionary.I still have been using #valuesAndCounts to get the dictionary out of a bag on several occasions.And is useful indeed.#(1 2 4 5 6 7 2 1 4 3) asBag occurrencesOf: 2and #(1 2 4 5 6 7 2 1 4 3) asBag valuesAndCounts at: 2 seem pretty much the same.But in this age of streams/reactive streams and lazy evaluations, what do we have in Pharo on that front?PhilOn Wed, Mar 6, 2019 at 4:49 PM Sven Van Caekenberghe <[hidden email]> wrote:I was just explaining how it is now, what I think the rationale is behind it. I understand #asDictionary as working on a collection of pairs/associations (because it basically goes to #withAll:). A bag is just a collection that is optimised for many duplicates, the fact that you have values and counts is more an implementation detail than an intrinsic property. The conversion that you want, and that already exists in #valuesAndCounts is one interpretation of what a bag is, not the only one. A bag is foremost a collection of things. I am not immediately convinced that #valuesAndCounts should be the default #asDictionary interpretation. What about the inverse for example ?   { #foo->2. #bar->3 } asDictionary asBag. But this is certainly an interesting discussion. > On 6 Mar 2019, at 16:23, Tim Mackinnon <[hidden email]> wrote: > > As Richard said - as a bag is relationship between keys and frequencies, I would expect it to be able to convert to a dictionary. > > It displays in an inspector just like a Dictionary - which is why I figured I could convert to pass it back to the exercise that was written with Dictionaries in mind. > > > > > The code to fix it is quite simple, but on these kinds of things - I thought it worth checking before submitting a PR. > > Tim > > >> On 6 Mar 2019, at 13:53, Sven Van Caekenberghe <[hidden email]> wrote: >> >> Why would that work ? What would you expect the output to be ? >> >> Try: >> >>  #(1 2 3) asDictionary >> >> it fails in exactly the same way. You need key/value pairs (Associations). >> >> These do work >> >>  Bag new add: #foo->100; asDictionary. >> >>  Bag new addAll: 'ABABABAAAA'; valuesAndCounts. >> >>> On 6 Mar 2019, at 14:25, Tim Mackinnon <[hidden email]> wrote: >>> >>> I was surprised to find that a Bag can’t convert to a dictionary - e.g. >>> >>> Bag new >>>     addAll: 'aabbbbcddd’; >>>     asDictionary >>> >>> Gives an error - Dnu #key >>> >>> >>> It looks to me like Bag is inheriting a bad version of #associationsDo:  and instead could simply forward it to #doWithOccurences: instead? >>> >>> I know Bag is not used that much - but it comes up a lot in programming exercises. >>> >>> Tim >> >> >
Open this post in threaded view
|

## Re: Why can't a Bag answer as a dictionary?

 I still am tempted to make asDictionary work - because the the moment you get a #key , DNU error - which is definitely not right.So should Bag asDictionary give you a reasonable dictionary, or should it throw a proper exception - something like:DomainError signal: ‘#asDictionary not supported, use #valueAndCounts’Is that better - or do we accept that it inherits it from Collection and so can do something useful?TimOn 6 Mar 2019, at 18:11, [hidden email] wrote:And when asking the object:#(1 2 4 5 6 7 2 1 4 3) asBag  isDictionarywe get false. So, not, not a dictionary.I still have been using #valuesAndCounts to get the dictionary out of a bag on several occasions.And is useful indeed.#(1 2 4 5 6 7 2 1 4 3) asBag occurrencesOf: 2and #(1 2 4 5 6 7 2 1 4 3) asBag valuesAndCounts at: 2 seem pretty much the same.But in this age of streams/reactive streams and lazy evaluations, what do we have in Pharo on that front?PhilOn Wed, Mar 6, 2019 at 4:49 PM Sven Van Caekenberghe <[hidden email]> wrote:I was just explaining how it is now, what I think the rationale is behind it. I understand #asDictionary as working on a collection of pairs/associations (because it basically goes to #withAll:). A bag is just a collection that is optimised for many duplicates, the fact that you have values and counts is more an implementation detail than an intrinsic property. The conversion that you want, and that already exists in #valuesAndCounts is one interpretation of what a bag is, not the only one. A bag is foremost a collection of things. I am not immediately convinced that #valuesAndCounts should be the default #asDictionary interpretation. What about the inverse for example ?   { #foo->2. #bar->3 } asDictionary asBag. But this is certainly an interesting discussion. > On 6 Mar 2019, at 16:23, Tim Mackinnon <[hidden email]> wrote: > > As Richard said - as a bag is relationship between keys and frequencies, I would expect it to be able to convert to a dictionary. > > It displays in an inspector just like a Dictionary - which is why I figured I could convert to pass it back to the exercise that was written with Dictionaries in mind. > > > > > The code to fix it is quite simple, but on these kinds of things - I thought it worth checking before submitting a PR. > > Tim > > >> On 6 Mar 2019, at 13:53, Sven Van Caekenberghe <[hidden email]> wrote: >> >> Why would that work ? What would you expect the output to be ? >> >> Try: >> >>  #(1 2 3) asDictionary >> >> it fails in exactly the same way. You need key/value pairs (Associations). >> >> These do work >> >>  Bag new add: #foo->100; asDictionary. >> >>  Bag new addAll: 'ABABABAAAA'; valuesAndCounts. >> >>> On 6 Mar 2019, at 14:25, Tim Mackinnon <[hidden email]> wrote: >>> >>> I was surprised to find that a Bag can’t convert to a dictionary - e.g. >>> >>> Bag new >>>     addAll: 'aabbbbcddd’; >>>     asDictionary >>> >>> Gives an error - Dnu #key >>> >>> >>> It looks to me like Bag is inheriting a bad version of #associationsDo:  and instead could simply forward it to #doWithOccurences: instead? >>> >>> I know Bag is not used that much - but it comes up a lot in programming exercises. >>> >>> Tim >> >> >
Open this post in threaded view
|

## Re: Why can't a Bag answer as a dictionary?

 I am still not convinced. > On 6 Mar 2019, at 19:24, Tim Mackinnon <[hidden email]> wrote: > > I still am tempted to make asDictionary work - because the the moment you get a #key , DNU error - which is definitely not right. The error might not be super clear, but it is not wrong, the elements of your collection do not respond to #key, a necessity to create a dictionary from a collection of associations/pairs. Also, if you modify #asDictionary like that, then   as: #Dictionary will still fail. This is all not so simple as it seems. > So should Bag asDictionary give you a reasonable dictionary, or should it throw a proper exception - something like: > > DomainError signal: ‘#asDictionary not supported, use #valueAndCounts’ > > Is that better - or do we accept that it inherits it from Collection and so can do something useful? > > Tim > >> On 6 Mar 2019, at 18:11, [hidden email] wrote: >> >> And when asking the object: >> >> #(1 2 4 5 6 7 2 1 4 3) asBag  isDictionary >> >> we get false. So, not, not a dictionary. >> >> I still have been using #valuesAndCounts to get the dictionary out of a bag on several occasions. >> >> And is useful indeed. >> >> #(1 2 4 5 6 7 2 1 4 3) asBag occurrencesOf: 2 >> >> and >> >> #(1 2 4 5 6 7 2 1 4 3) asBag valuesAndCounts at: 2 >> >> seem pretty much the same. >> >> But in this age of streams/reactive streams and lazy evaluations, what do we have in Pharo on that front? >> >> Phil >> >> >> >> >> >> On Wed, Mar 6, 2019 at 4:49 PM Sven Van Caekenberghe <[hidden email]> wrote: >> I was just explaining how it is now, what I think the rationale is behind it. >> >> I understand #asDictionary as working on a collection of pairs/associations (because it basically goes to #withAll:). >> >> A bag is just a collection that is optimised for many duplicates, the fact that you have values and counts is more an implementation detail than an intrinsic property. >> >> The conversion that you want, and that already exists in #valuesAndCounts is one interpretation of what a bag is, not the only one. A bag is foremost a collection of things. >> >> I am not immediately convinced that #valuesAndCounts should be the default #asDictionary interpretation. >> >> What about the inverse for example ? >> >>   { #foo->2. #bar->3 } asDictionary asBag. >> >> But this is certainly an interesting discussion. >> >> > On 6 Mar 2019, at 16:23, Tim Mackinnon <[hidden email]> wrote: >> > >> > As Richard said - as a bag is relationship between keys and frequencies, I would expect it to be able to convert to a dictionary. >> > >> > It displays in an inspector just like a Dictionary - which is why I figured I could convert to pass it back to the exercise that was written with Dictionaries in mind. >> > >> > >> > >> > >> > The code to fix it is quite simple, but on these kinds of things - I thought it worth checking before submitting a PR. >> > >> > Tim >> > >> > >> >> On 6 Mar 2019, at 13:53, Sven Van Caekenberghe <[hidden email]> wrote: >> >> >> >> Why would that work ? What would you expect the output to be ? >> >> >> >> Try: >> >> >> >>  #(1 2 3) asDictionary >> >> >> >> it fails in exactly the same way. You need key/value pairs (Associations). >> >> >> >> These do work >> >> >> >>  Bag new add: #foo->100; asDictionary. >> >> >> >>  Bag new addAll: 'ABABABAAAA'; valuesAndCounts. >> >> >> >>> On 6 Mar 2019, at 14:25, Tim Mackinnon <[hidden email]> wrote: >> >>> >> >>> I was surprised to find that a Bag can’t convert to a dictionary - e.g. >> >>> >> >>> Bag new >> >>>     addAll: 'aabbbbcddd’; >> >>>     asDictionary >> >>> >> >>> Gives an error - Dnu #key >> >>> >> >>> >> >>> It looks to me like Bag is inheriting a bad version of #associationsDo:  and instead could simply forward it to #doWithOccurences: instead? >> >>> >> >>> I know Bag is not used that much - but it comes up a lot in programming exercises. >> >>> >> >>> Tim >> >> >> >> >> > >> >> >> >
Open this post in threaded view
|

## Re: Why can't a Bag answer as a dictionary?

Open this post in threaded view
|

## Re: Why can't a Bag answer as a dictionary?

Open this post in threaded view
|