Handling���� <some bag> as: Dictionary��
is really quite straightforward.
Add a one-line method to Bag:

Bag>>
associationsDo: aBlock
�� �� contents associationsDo: aBlock.

This is a good idea anyway, because currently there is a bug where
aBag associationsDo: aBlock
fails to pass Associations to aBlock.�� Once you have done that,
'abracadabra' asBag as: Dictionary
just works.�� This one simple addition
��- makes aBag associationsDo: aBlock work when it didn't before
��- makes aBag asDictionary work
��- makes aBag as: Dictionary work
��- makes Dictionary newFrom: aBag work




On Thu, 7 Mar 2019 at 07:35, Sven Van Caekenberghe <sven@stfx.eu> wrote:
I am still not convinced.

> On 6 Mar 2019, at 19:24, 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.

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

�� <some bag> 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, phil@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 <sven@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 <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
>> >>
>> >>
>> >
>>
>>
>>
>