Reputation: 639
I am trying to filter a list that contains pairs.
For example my list is
List((1,2),(3,4),(5,2))
I want to use filter to filter the list to only contain answers where the first number in the pair is greater than the second. How can I do this?
So, The resulting list would be
List((5,2))
Upvotes: 0
Views: 1727
Reputation: 1828
Your example doesn't make sense, but I guess you still want to filter your list on some predicate.
The easier is to deconstruct the pair:
scala> val l = List((1,2), (3,4), (4,5))
l: List[(Int, Int)] = List((1,2), (3,4), (4,5))
scala> l.filter { case (a, b) => a > b }
res0: List[(Int, Int)] = List()
Upvotes: 4
Reputation: 2101
Filter for first number in pair is greater than second:
val ls = List((1,2),(3,4),(4,5),(6,2))
ls.filter(pair => pair._1 > pair._2) // List((6, 2))
Upvotes: 4