Creating a dictionary from a list of associations
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 <konrad.hinsen@fastmail.net> wrote:
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.
On 2 May 2019, at 17:52, Gabriel Cotelli <g.cotelli@gmail.com> wrote:
Dictionary newFromPairs: (words collect: [ :each | each -> each size ]).
On Thu, May 2, 2019 at 12:45 PM Konrad Hinsen <konrad.hinsen@fastmail.net> 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.
On 2 May 2019, at 18:18, K K Subbu <kksubbu.ml@gmail.com> 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
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 <g.cotelli@gmail.com> writes:
Dictionary newFromPairs: (words collect: [ :each | each -> each size ]).
Sven Van Caekenberghe <sven@stfx.eu> writes:
{ #foo->1. #bar->2 } #asDictionary
(#('abc' 'defg') collect: [ :each | each -> each size ]) asDictionary.
K K Subbu <kksubbu.ml@gmail.com> 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.
participants (4)
-
Gabriel Cotelli -
K K Subbu -
Konrad Hinsen -
Sven Van Caekenberghe