

Method Definition: def filter (p: ( (A, B))> Boolean): Map A, B Return Type: It returns a new map consisting all the elements of the map which satisfies the given predicate. This allows us to perform this chain of map / filter / slice transformations with only a single traversal, and only creating a single output collection. val mutMap = (1 -> "Ronaldo", 2 -> "Messi", 3 -> "Mbappe") mutMap(1) = "Lewandowski" The filter () method is utilized to select all elements of the map which satisfies a stated predicate.
#Scala filter method map update
Then we can define a new mutable map and update its value. myMap(1) = "Lewandowski" //error: value update is not a member of Map //myMap(1) = "Lewandowski"
#Scala filter method map generator
In Python 3, however, the function returns a map object which is a generator object. When we try to update a value in our immutable map, we will get an error. Map In Python 2, the map() function returns a list. val myMap : Map = Map(1 -> "Ronaldo", 2 -> "Messi", 3 -> "Mbappe") val myMap2 = Map(4 -> "Bruyne", 5 -> "Salah") val conMap = myMap ++ myMap2 //conMap: = Map(5 -> //Salah, 1 -> Ronaldo, 2 -> Messi, 3 -> Mbappe, 4 -> Bruyne) def double (el: Int) : Int = Ĭoncatenation of two maps notice that there is no automated sorting between the keys. val collection = List(1, 3, 2, 5, 4, 7, 6, 0) //returns the minimum value of list val res = collection.reduce((x, y) => x min y) map (as function)Īpply a function over each element in a collection.

It applies a binary operation on every element of a collection and produce a single value. var sum = 0 val arr = Array(1,2,3,4) arr.foreach( sum += _) println(sum) //6 reduce We can use another variable inside the function passed to foreach. This, perhaps, is the first usage of underscore we come across when learning Scala. We widely use the underscore as a wildcard and in matching unknown patterns. Due to the two different component types of a Query (lifted and plain, e.g. Or even str.foreach(println) //will give the same output In this tutorial, we’ll look at the different and most common usages of underscores in Scala. Collection values are represented by the Query class (a RepSeqT) which contains many standard collection methods like flatMap, filter, take and groupBy. Let’s use anonymous function this time val str = "James" str.foreach(i => println(i)) //J //a //m //e //s
