Created
May 16, 2017 03:50
-
-
Save travisdachi/b787f2b0f28d65edac2da687c17361e5 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
/** | |
* 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