Is there a way to derive from other collectors? `...
# arrow
d
Is there a way to derive from other collectors?
Copy code
fun <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?
Ok, this might be done with zip that returns a Collector... but really I'm looking for
Copy code
fun <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.
a
it could be possible to add new "intermediate" operations to collectors, indeed