Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Created August 2, 2010 10:58
Show Gist options
  • Save etorreborre/504472 to your computer and use it in GitHub Desktop.
Save etorreborre/504472 to your computer and use it in GitHub Desktop.
/**
* 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