user2947615
user2947615

Reputation: 497

Filtering a list by a function in Scala

I need to filter a list by the result of a set. For example this works:

(List(1, 2, 3, 4, 5, 6, 7, 8, 9) filter (_!= 1))

Output List(2,3,4,5,6,7,8,9) However I need to filter this list by two function that return a set (for example it returns Set(1,2)) and the current list needs to be filtered by these functions.

Any ideas how this can be done? I already tried using the && but it doesn't work

Thanks in advance.

Upvotes: 0

Views: 789

Answers (1)

tiran
tiran

Reputation: 2431

Try this,

scala> val raw = Set(1,2)
raw: scala.collection.immutable.Set[Int] = Set(1, 2)

scala> val column = Set(3,4)
column: scala.collection.immutable.Set[Int] = Set(3, 4)

scala> val l = (1 to 9).toList
l: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> l.filterNot(x => raw(x) || column(x))
res9: List[Int] = List(5, 6, 7, 8, 9)

Based on @4e6 's comment,

scala> l.filterNot(raw ++ column)
res9: List[Int] = List(5, 6, 7, 8, 9)

Upvotes: 1

Related Questions