Vandexel Vandexel - 2 months ago 9
Scala Question

Using scala's filter on a list of pairs

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))

Answer

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))
Comments