Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala - using filter with pattern matching

I have the following lists :

case class myPair(ids:Int,vals:Int)

val someTable = List((20,30), (89,90), (40,65), (45,75), (35,45))

val someList:List[myPair] =
  someTable.map(elem => myPair(elem._1, elem._2)).toList

I would like to filter all "ids" > 45 . I tried something like this article filter using pattern matching):

someList.filter{ case(myPair) => ids >= 45 }

but without success. appreciate your help

like image 810
igx Avatar asked Feb 18 '13 17:02

igx


1 Answers

You don't need pattern matching at all, type is known at compile time:

someList.filter(_.ids >= 45)

or slightly more verbose/readable:

someList.filter(pair => pair.ids >= 45)
like image 170
Tomasz Nurkiewicz Avatar answered Nov 03 '22 10:11

Tomasz Nurkiewicz