Skip to content

Instantly share code, notes, and snippets.

@travisdachi
Created May 16, 2017 03:50
Show Gist options
  • Save travisdachi/b787f2b0f28d65edac2da687c17361e5 to your computer and use it in GitHub Desktop.
Save travisdachi/b787f2b0f28d65edac2da687c17361e5 to your computer and use it in GitHub Desktop.
/**
* Returns a list containing only elements matching the given [predicate].
*/
public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
return filterTo(ArrayList<T>(), predicate)
}
/**
* Appends all elements matching the given [predicate] to the given [destination].
*/
public inline fun <T, C : MutableCollection<in T>> Iterable<T>.filterTo(destination: C, predicate: (T) -> Boolean): C {
for (element in this) if (predicate(element)) destination.add(element)
return destination
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment