Skip to content

Instantly share code, notes, and snippets.

@kretes
Created April 16, 2014 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kretes/10909078 to your computer and use it in GitHub Desktop.
Save kretes/10909078 to your computer and use it in GitHub Desktop.
def takeWhileRec(function: (Int) => Boolean, list:List[Int]):List[Int] = list match {
case Nil => Nil
case head :: tail => if (function(head)) head +: takeWhileRec(function,tail) else Nil
}
"takeWhile" should {
"work the same" in {
val function: (Int) => Boolean = _ < 3
val list: List[Int] = List(1, 2, 3)
takeWhileRec(function,list) === list.takeWhile(function)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment