thanh
10/15/2019, 9:22 AM. /**
* Apply a filter to a structure such that the output structure contains all instances of specified class.
*/
@Suppress("UNCHECKED_CAST")
fun <A, B> Kind<F, A>.filterIsInstance(klass: Class<B>): Kind<F, B> =
filterMap { a -> if(klass.isInstance(a)) Some(a as B) else None }
Imran/Malic
10/15/2019, 9:44 AMaballano
10/15/2019, 9:48 AMImran/Malic
10/15/2019, 9:49 AMthanh
10/15/2019, 9:49 AM/**
* Returns a list containing all elements that are instances of specified class.
*/
public fun <R> Iterable<*>.filterIsInstance(klass: Class<R>): List<R> {
return filterIsInstanceTo(ArrayList<R>(), klass)
}
thanh
10/15/2019, 9:49 AM/**
* Appends all elements that are instances of specified class to the given [destination].
*/
public fun <C : MutableCollection<in R>, R> Iterable<*>.filterIsInstanceTo(destination: C, klass: Class<R>): C {
@Suppress("UNCHECKED_CAST")
for (element in this) if (klass.isInstance(element)) destination.add(element as R)
return destination
}
pakoito
10/15/2019, 1:04 PMpakoito
10/15/2019, 1:05 PMpakoito
10/15/2019, 1:05 PMpakoito
10/15/2019, 1:05 PMfilterIsInstance<Bla>()
thanh
10/15/2019, 1:05 PMfilterIsInstance
should be the same interface with the filterIsInstance
in stdlibpakoito
10/15/2019, 1:06 PMpakoito
10/15/2019, 1:06 PMpakoito
10/15/2019, 1:06 PMpakoito
10/15/2019, 1:06 PMpakoito
10/15/2019, 1:07 PMthanh
10/15/2019, 1:14 PMImran/Malic
10/15/2019, 1:19 PMpakoito
10/15/2019, 1:27 PM/**
* Apply a filter to a structure such that the output structure contains all instances of specified class.
*/
@Suppress("UNCHECKED_CAST")
fun <A, B> Kind<F, A>.filterIsInstance(klass: Class<B>): Kind<F, B> =
filterMap { a -> if(klass.isInstance(a)) Some(a as B) else None }
pakoito
10/15/2019, 1:27 PMpakoito
10/15/2019, 1:27 PMpakoito
10/15/2019, 1:27 PMaballano
10/15/2019, 1:28 PMaballano
10/15/2019, 1:28 PMImran/Malic
10/15/2019, 1:30 PMthanh
10/15/2019, 1:34 PM