cool breeze cool breeze - 1 month ago 11
Scala Question

Using filterNot with a Map

I have a simple map

val m = Map("a1" -> "1", "b2" -> "2", "c3" -> "3")
val someList = List("b", "d")
m.filterNot( (k,v) => someList.exist(l => k.startsWith(l)) )


I'm getting an error:

error: missing parameter type


I'm doing something silly here I'm sure, why isn' this compiling?

Answer

filterNot needs a case keyword and {} when you extract k, v from tuple.

note that its not exist its exists

m.filterNot { case (k,v) => someList.exists(l => k.startsWith(l)) }

or

m.filterNot(pair => someList.exists(l => pair._1.startsWith(l)))

Explanation

As you are extracting k, v from the tuple using extractor syntax you have to use case keyword and {}

Without extractor syntax you can do

m.filterNot(pair => someList.exists(l => pair._1.startsWith(l)))

Scala REPL

scala> val m = Map("a1" -> "1", "b2" -> "2", "c3" -> "3")
m: scala.collection.immutable.Map[String,String] = Map(a1 -> 1, b2 -> 2, c3 -> 3)

scala> val someList = List("b", "d")
someList: List[String] = List(b, d)

scala>  m.filterNot { case (k,v) => someList.exists(l => k.startsWith(l)) }
res15: scala.collection.immutable.Map[String,String] = Map(a1 -> 1, c3 -> 3)

Without extractor syntax

Now you need not use case keyword and {} as we are not using extracting the key and value using extractor syntax

scala> m.filterNot(pair => someList.exists(l => pair._1.startsWith(l)))
res18: scala.collection.immutable.Map[String,String] = Map(a1 -> 1, c3 -> 3)

scala> val m = Map("a1" -> "1", "b2" -> "2", "c3" -> "3")
m: scala.collection.immutable.Map[String,String] = Map(a1 -> 1, b2 -> 2, c3 -> 3)

scala> val someList = List("b", "d")
someList: List[String] = List(b, d)

scala> m.filterNot(pair => someList.exists(l => pair._1.startsWith(l)))
res19: scala.collection.immutable.Map[String,String] = Map(a1 -> 1, c3 -> 3)
Comments