Last active
July 31, 2017 19:51
-
-
Save ramannanda9/d1bbd3cb7ab05957ee28af840ac2b8fe to your computer and use it in GitHub Desktop.
filter in scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def filter[K](arr: List[K], f: K => Boolean): List[K] = arr match { | |
case Nil => arr | |
case list: List[K] => if (f(list.head)) list.head :: filter(list.tail, f) else filter(list.tail, f) | |
} | |
val listToFilter = 2 :: 5 :: Nil | |
listToFilter.filter(_ > 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment