Doubtless the original poster expected aBag asDictionary to answer a dictionary mapping the (distinct) elements of the bag to their counts. Mathematically, a bag can usefully be seen as a partial function from a set U to the positive integers, and a dictionary with U keys and positive integer values is precisely that. It's really not unreasonable to expect that, considering that there *is* a method that does just what the OP wanted: #valuesAndCounts. It's rather dangerous to expose private state like that, but it is not a private method. I would expect asDictionary ^contents copy On Thu, 7 Mar 2019 at 02:54, Sven Van Caekenberghe <sven@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