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.