Last active
July 25, 2021 05:41
-
-
Save eribeiro/6017897 to your computer and use it in GitHub Desktop.
How to do a foldLeft with a Map in Scala, plus basic stuff.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val list = (1 to 10).toList | |
// imutable map | |
val map1 = list.foldLeft(Map.empty[Int,String])( (map, value) => map + (value -> value.toString) ) | |
// this is equivalent to | |
val map1 = list.foldLeft(Map.empty[Int,String])( (map, value) => map + ((value, value.toString)) ) | |
// imutable map with more complex value (note the extra parenthesis) | |
val map2 = list.foldLeft(Map.empty[Int,String])( (map, value) => map + (value -> ("number:" + value.toString)) ) | |
// mutable Map | |
val map3 = list.foldLeft(scala.collection.mutable.Map.empty[Int,String])( (map, value) => map += (value -> value.toString) ) | |
// Basic Map operations | |
val map4 = Map[Int,Int]() // immutable map | |
val map41 = map4 + (1 -> 1) // creates a new Map with (1,"1") as key-value pair | |
val map42 = map4 + ((2,2), (3,3)) | |
val map43 = map4 + (2->2, 3->3) | |
val map5 = scala.collection.mutable.Map[Int,Int]() // mutable Map | |
map5 += 2 -> 2 | |
map5 += ((2,2)) // same as above | |
map5 += ((1,1), (2,2)) | |
map5 += (1->1, 2->2) | |
map5 + ((8,8)) // creates a NEW MAP with KV (8,8), EVEN THOUGH map5 is mutable | |
map5 =- 2 // removes key 2 | |
map5 - 2 // creates a NEW MAP without key 2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment