Skip to content
Advertisement

How to change keys from a map in scala

How to change the following keys from the map without any variables full functional

JavaScript

to

JavaScript

I tried with map and was able to change the keys to the same strings but not to different ones.

Advertisement

Answer

You can apply a map to all the items but focus only on the keys:

JavaScript

You can define f to be a function or simply a constant map e.g.:

JavaScript

Putting it all together:

JavaScript

Yields the expected result:

JavaScript

If you like one-liners you can also avoid a separate map / function:

JavaScript

Yet another way is to only treat the left side of the tuple:

JavaScript

And just for completeness, if your only elements really are true and false you can just do:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement