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.