Hi everyone,
About twice a week I start looking for a straightforward way to turn a list of associations into a dictionary, spend a while searching, and give up, ending up with a messy explicit loop. Example: | words lengths | words := #('abc' 'defg'). lengths := Dictionary new. words do: [ :each | lengths at: each put: each size ]. lengths Is there really no way to this with less code? I'd expect the following to work: | words lengths | words := #('abc' 'defg'). lengths := Dictionary withAll: (words collect: [ :each | each -> each size ]). but it looks like Dictionary>>#withAll: was specifically designed to make this impossible (because with the default implementation in Collection it would work). Konrad. |
Dictionary newFromPairs: (words collect: [ :each | each -> each size ]). On Thu, May 2, 2019 at 12:45 PM Konrad Hinsen <[hidden email]> wrote: Hi everyone, |
> On 2 May 2019, at 17:52, Gabriel Cotelli <[hidden email]> wrote: > > Dictionary newFromPairs: (words collect: [ :each | each -> each size ]). > > On Thu, May 2, 2019 at 12:45 PM Konrad Hinsen <[hidden email]> wrote: > Hi everyone, > > About twice a week I start looking for a straightforward way to > turn a list of associations into a dictionary, { #foo->1. #bar->2 } #asDictionary (#('abc' 'defg') collect: [ :each | each -> each size ]) asDictionary. > spend a while searching, > and give up, ending up with a messy explicit loop. > > Example: > > | words lengths | > words := #('abc' 'defg'). > lengths := Dictionary new. > words do: > [ :each | lengths at: each put: each size ]. > lengths > > Is there really no way to this with less code? I'd expect the following > to work: > > | words lengths | > words := #('abc' 'defg'). > lengths := Dictionary withAll: > (words collect: [ :each | each -> each size ]). > > but it looks like Dictionary>>#withAll: was specifically designed to > make this impossible (because with the default implementation in > Collection it would work). > > Konrad. > |
In reply to this post by khinsen
On 02/05/19 9:14 PM, Konrad Hinsen wrote:
> | words lengths | > words := #('abc' 'defg'). > lengths := Dictionary new. > words do: > [ :each | lengths at: each put: each size ]. Dictionary withKeys: words andValues: (words collect: #size) HTH .. Subbu |
> On 2 May 2019, at 18:18, K K Subbu <[hidden email]> wrote: > > On 02/05/19 9:14 PM, Konrad Hinsen wrote: >> | words lengths | >> words := #('abc' 'defg'). >> lengths := Dictionary new. >> words do: >> [ :each | lengths at: each put: each size ]. > > Dictionary withKeys: words andValues: (words collect: #size) > > HTH .. Subbu > Never saw that one, cool. But it is #newFromKeys:andValues: as far as I can see |
On 02/05/19 9:52 PM, Sven Van Caekenberghe wrote:
>> Dictionary withKeys: words andValues: (words collect: #size) >> >> HTH .. Subbu >> > Never saw that one, cool. > > But it is #newFromKeys:andValues: as far as I can see You're right. I got mixed up. Thanks for the correction .. Subbu |
In reply to this post by khinsen
Hi everyone,
> About twice a week I start looking for a straightforward way to > turn a list of associations into a dictionary, spend a while searching, > and give up, ending up with a messy explicit loop. Thanks for all the replies with interesting suggestions! Gabriel Cotelli <[hidden email]> writes: > Dictionary newFromPairs: (words collect: [ :each | each -> each size > ]). Sven Van Caekenberghe <[hidden email]> writes: > { #foo->1. #bar->2 } #asDictionary > > (#('abc' 'defg') collect: [ :each | each -> each size ]) asDictionary. K K Subbu <[hidden email]> writes (after correction): > Dictionary newFromKeys: words andValues: (words collect: #size) For my real-life use case, asDictionary is the most appopriate choice. It does exactly what I need. I had discovered newFromKeys:andValues:, which indeed is a good fit for my small example, but not for my real applications, where the list of associations comes from another method, so I can't change the way it is constructed. What I was most surprised about is newFromPairs:, which works as quoted although its documentation says something else: "Answer an instance of me associating (anArray at: i) to (anArray at: i+1) for each odd i. anArray must have an even number of entries." Konrad. |
Free forum by Nabble | Edit this page |