To me, a bag is first and foremost a multiset.
Yes, it's a collection, but it's not *just* a collection.
It's a very specific kind of collection with a very
salient "characteristic function".

In my own Smalltalk library, #asDictionary isn't even *defined*
on things that are not dictionaries or some kind of mapping,
because let's face it, a dictionary *isn't* a collection of
Associations, and trying to make it look like one made
Smalltalk-80 rather inconsistent.  For example, if a dictionary
were a collection of associations, you would expect
(Dictionary with: #a -> 1 with: #b -> 2) includes: #a -> 1
to be true. No, it's false.  You would expect
(Dictionary with: #a -> 1 with: #b -> 2) asArray
to be (a->1 b->2).  No, it's (1 2).  In order to avoid major
confusion, I had to learn never to think of Associations in
connection with Dictionaries.  So I expect #asDictionary to
have something to do with #keysAndValuesDo:.
or something like that.

For {1->2. 3->4} asDictionary I would have to write
Dictionary withAllAssociations: {1->2. 3->4}
except that I never actually found a use for it.

#valuesAndCounts is a method that violates good OO practice,
because it exposes (what you'd expect to be) private state.
For example,
  b := 'abracadabra' asBag.
  b valuesAndCounts at: $a put: 'BOOM!'.
  b
select, Print It, BOOM!.  It should *copy* the dictionary.



On Thu, 7 Mar 2019 at 04:50, Sven Van Caekenberghe <[email protected]> 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 <[email protected]> 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 <[email protected]> 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 <[email protected]> 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
> >>
> >>
> >
>
>
>

Reply via email to