Created
August 2, 2010 10:58
-
-
Save etorreborre/504472 to your computer and use it in GitHub Desktop.
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
/** | |
* remove the first element satifying the predicate | |
* @return an iterable minus the first element satisfying the predicate | |
*/ | |
def removeFirst(predicate: T => Boolean): Iterable[T] = { | |
if (xs.isEmpty) xs | |
else if (predicate(xs.head)) xs.drop(1) | |
else xs.take(1) ++ xs.removeFirst(predicate) | |
} | |
/** | |
* replace the first element of the iterable with another element | |
*/ | |
def replaceFirst(function: T => T): Iterable[T] = { | |
if (xs.isEmpty) xs | |
else xs.take(1).map(f(_)) ++ xs.drop(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment