Created
June 18, 2010 07:25
-
-
Save kmizu/443358 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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