On Wed, Mar 6, 2019 at 10:25 AM Tim Mackinnon <tim@testit.works> wrote:
> I still am tempted to make asDictionary work - because the the moment you > get a #key , DNU error - which is definitely not right. > This argument gives me the willies. Sorry. I can see an argument that a sequenceable collection can implement a meaningful #asDictionary, using #keysAndValuesDo:. The key is the index of each element, so it makes sense. But, #asDictionary for a non-sequenceable collection doesn't make sense (other than for Dictionary and its variants, obviously). What would the keys be from a Set, for example? Bag>>#asDictionary makes sense if (and in my opinion, only if) you define it to rely on the internal implementation. #valuesAndCounts make sense because it doesn't rely on internal implementation details to define it, although it typically is implemented to use those internal implementation details. I think expressing it as a conversion is a poor choice. Asking it for a different representation is good. [And as an aside, given that the API expresses #occurrencesOf:, #valuesAndCounts might have been better named #valuesAndOccurrences (and perhaps "With" rather than "And").] > 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, p...@highoctane.be 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 <s...@stfx.eu> 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 <tim@testit.works> 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. >> > >> > <PastedGraphic-3.png> >> > >> > >> > 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 <s...@stfx.eu> 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 <tim@testit.works> 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 >> >> >> >> >> > >> >> >> >> >