For example if the map is equal to:
z = ['x':1, 'y':2]
For f(2) I would like it to become:
[x':2, 'y':4]
I know it could be done using a .each loop, question is whether there is a simpler way.
Thank you
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s the
collectEntriesfunction, which is applied to all keys and values in aMap, and returns a newMapwith the updated values:A couple of notes here:
['z':1]is the same as[z:1], but the latter is groovier and more legiblecollectEntriesclosure must be a map, so be careful there(key), not justkeyHowever, it’s almost certainly just be cleaner to use
each()with the single-argument closure, like so:There’s less gotchyas this way, and it modifies the map in-place, so you don’t have to reassign the variable.