[squeak-dev] The Inbox: Collections-ct.962.mcz

Marcel Taeumel marcel.taeumel at hpi.de
Tue Nov 16 14:06:15 UTC 2021


Hi Christoph --

So you want to convert something that looks like a bag to actually be an instance of Bag? Uhh... I expect that you use Dictionary instead of Bag in the first place because of performance? Well, it feels like you would want to be able to convert a Dictionary to a Morph, too, if that dict happens to have #color and #bounds set? Doesn't feel right. Like importing something from JSON, getting a Dictionary and now wanting to find the system's matching classes to keep on working with domain-specific objects.

I would write your example like this:

counts := Dictionary new at: 6 put: 2; at: 7 put: 1; at: 61 put: 17; yourself.

bag := Bag new.
counts keysAndValuesDo: [:object :count |
   bag add: object withOccurrences: count].

Or use a bag in the first place.

Well, I think I could live with something like Dictionary >> #asBag if each association's #value happens to be an integer.

Why do you want to have that anyway? Just for HistogramMorph? You could as well make that compatible with Dictionary.

Best,
Marcel
Am 16.11.2021 14:41:19 schrieb christoph.thiede at student.hpi.uni-potsdam.de <christoph.thiede at student.hpi.uni-potsdam.de>:
Hi Marcel, hi Tobias,

I would rather call it a coincidence that this constructor takes the same format as we use internally in the Bag. Maybe we should rename this into something like #newFromCounts:?

For a real use case, I use my new BenchmarkSimulator to count primitive calls and end up with a dictionary primitiveCounts that maps primitive numbers to counts. I think this is a valid use case and deserves its own protocol. Otherwise, I would need to do something like this:

    primitiveCounts := Dictionary new at: 6 put: 2; at: 7 put: 1; at: 61 put: 17; yourself.
    (primitiveCounts associations collect: [:assoc | Array new: assoc value withAll: assoc key]) flatten as: Bag

Which really feels a bit cumbersome. :-)

Best,
Christoph

---
Sent from Squeak Inbox Talk [https://github.com/hpi-swa-lab/squeak-inbox-talk]

On 2021-11-16T12:04:54+01:00, marcel.taeumel at hpi.de wrote:

> > That contents is a dict is implementation detail not to be leaked.
>
> To be fair, there is an actual extension point in #contentsClass. Yet, it remains an implementation detail, hidden from clients. Clients should use #newFrom: or #withAll: to fill a bag with existing contents.
>
> Best,
> Marcel
> Am 16.11.2021 11:58:09 schrieb Tobias Pape <das.linux at gmx.de>:
> -1
>
> this is to my eyes doubtlessly private :)
> That contents is a dict is implementation detail not to be leaked.
>
> Best regards
> -Tobias
>
> > On 16. Nov 2021, at 11:28, Marcel Taeumel wrote:
> >
> >> ==================== Summary ====================
> >>
> >> Name: Collections-ct.962
> >> Author: ct
> >> Time: 13 November 2021, 2:29:12.457577 am
> >> UUID: 762c672f-abb5-194e-9f54-14cce19aa7f9
> >> Ancestors: Collections-pre.961
> >>
> >> Adds convenience constructor to Bag.
> >>
> >> Bag newFromContents: (Dictionary new at: #mon put: 7; at: #tue put: 2; at: #wed put: 4; yourself).
> >>
> >> =============== Diff against Collections-pre.961 ===============
> >>
> >> Item was added:
> >> + ----- Method: Bag class>>newFromContents: (in category 'instance creation') -----
> >> + newFromContents: aDictionary
> >> +
> >> + ^ self new setContents: aDictionary!
>
>
>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20211116/ee077bd0/attachment.html>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20211116/95c5b816/attachment.html>


More information about the Squeak-dev mailing list