dave08
12/08/2024, 1:52 PMfun <T> filterCollector(filter: (T) -> Boolean) = Collector.nonSuspendOf(
supply = { mutableListOf<T>() },
accumulate = { acc, t: T -> if (filter(t)) acc.add(t) },
finish = { it }
)
val col1 = filterCollector { it.foo == Foo.One }
val col2 = filterCollector { it.foo != Foo.One && it.bar ... }
val col3 = filterCollector { it.foo != Foo.One && it.bar ... }
// I want to base myself on the it.foo != Foo.One once derive a collector only for each it.bar check
// Also is there a way to use the same col1 but negated?
dave08
12/08/2024, 4:54 PMfun <T> partitionToSets(condition: (T) -> Boolean): NonSuspendCollector<T, Pair<Set<T>, Set<T>>> =
Collector.nonSuspendOf<Pair<MutableSet<T>, MutableSet<T>>, T, Pair<Set<T>, Set<T>>>(
supply = { mutableSetOf<T>() to mutableSetOf() },
accumulate = { (first, second), item ->
if (condition(item)) first.add(item) else second.add(item)
},
finish = { (first, second) -> first.toSet() to second.toSet() }
)
and then to split second
further with another partitionToSets
, while leaving first
the way it is.Alejandro Serrano.Mena
12/08/2024, 8:20 PM