stdlib: Introduced filterIsInstance version

without class-literal as a parameter
This commit is contained in:
Denis Zharkov
2014-11-04 19:02:16 +04:00
committed by Andrey Breslav
parent da5164acd8
commit e768b4e285
5 changed files with 161 additions and 6 deletions
@@ -324,6 +324,27 @@ public fun ShortArray.fill(element: Short): ShortArray {
return this
}
/**
* Returns a list containing all elements that are instances of specified type parameter R
*/
public inline fun <reified R> Array<*>.filterIsInstance(): List<R> {
return filterIsInstanceTo(ArrayList<R>())
}
/**
* Returns a list containing all elements that are instances of specified type parameter R
*/
public inline fun <reified R> Iterable<*>.filterIsInstance(): List<R> {
return filterIsInstanceTo(ArrayList<R>())
}
/**
* Returns a stream containing all elements that are instances of specified type parameter R
*/
public inline fun <reified R> Stream<*>.filterIsInstance(): Stream<R> {
return FilteringStream(this, true, { it is R }) as Stream<R>
}
/**
* Returns a list containing all elements that are instances of specified class
*/
@@ -345,6 +366,30 @@ public fun <T, R : T> Stream<T>.filterIsInstance(klass: Class<R>): Stream<R> {
return FilteringStream(this, true, { klass.isInstance(it) }) as Stream<R>
}
/**
* Appends all elements that are instances of specified type parameter R to the given *destination*
*/
public inline fun <reified R, C : MutableCollection<in R>> Array<*>.filterIsInstanceTo(destination: C): C {
for (element in this) if (element is R) destination.add(element)
return destination
}
/**
* Appends all elements that are instances of specified type parameter R to the given *destination*
*/
public inline fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterIsInstanceTo(destination: C): C {
for (element in this) if (element is R) destination.add(element)
return destination
}
/**
* Appends all elements that are instances of specified type parameter R to the given *destination*
*/
public inline fun <reified R, C : MutableCollection<in R>> Stream<*>.filterIsInstanceTo(destination: C): C {
for (element in this) if (element is R) destination.add(element)
return destination
}
/**
* Appends all elements that are instances of specified class to the given *destination*
*/