Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created June 18, 2010 07:25
Show Gist options
  • Save kmizu/443358 to your computer and use it in GitHub Desktop.
Save kmizu/443358 to your computer and use it in GitHub Desktop.
def removeFirst[A](l: List[A])(f: A => Boolean): List[A] = {
l.foldLeft((List[A](), false)) {
case ((r, found), e) => if((!found) && f(e)) (r, true) else (e::r, found)
}._1.reverse
}
println(removeFirst(List(1, 2, 1))(_ == 3)) // List(1, 2, 1)
println(removeFirst(List(1, 2, 1))(_ == 2)) // List(1, 1)
println(removeFirst(List(1, 2, 1))(_ == 1)) // List(2, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment