Skip to content

Commit

Permalink
More overloads for collectors
Browse files Browse the repository at this point in the history
  • Loading branch information
serras committed Nov 27, 2024
1 parent 63b162f commit 2bd75ce
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public interface CollectorI<InternalAccumulator, in Value, out Result> {
override suspend fun finish(current: InternalAccumulator): Result = finish(current)
}

/**
* Constructs a new [Collector] from its components,
* where the accumulated value is directly its result
*/
public fun <Value, Result> of(
supply: suspend () -> Result,
accumulate: suspend (current: Result, value: Value) -> Unit,
characteristics: Set<Characteristics> = setOf(),
): Collector<Value, Result> = of(
supply, accumulate, { it }, characteristics + Characteristics.IDENTITY_FINISH
)

/**
* Constructs a new [Collector] from its components
*/
Expand All @@ -88,6 +100,18 @@ public interface CollectorI<InternalAccumulator, in Value, out Result> {

override fun finishNonSuspend(current: InternalAccumulator): Result = finish(current)
}

/**
* Constructs a new [Collector] from its components,
* where the accumulated value is directly its result
*/
public fun <Value, Result> nonSuspendOf(
supply: () -> Result,
accumulate: (current: Result, value: Value) -> Unit,
characteristics: Set<Characteristics> = setOf(),
): NonSuspendCollector<Value, Result> = nonSuspendOf(
supply, accumulate, { it }, characteristics + Characteristics.IDENTITY_FINISH
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,79 @@ public fun <A, R, S, T, V> zip(
z: NonSuspendCollector<A, T>,
combine: (R, S, T) -> V,
): NonSuspendCollector<A, V> = x.zip(y).zipNonSuspend(z) { (a, b), c -> combine(a, b, c) }

/**
* Combines four [Collector]s by performing the phases
* of each of them in parallel.
*
* @param c1 First [Collector]
* @param c2 Second [Collector]
* @param c3 Third [Collector]
* @param c4 Fourth [Collector]
* @param combine Function that combines the end results
*/
public fun <A, C1, C2, C3, C4, R> zip(
c1: Collector<A, C1>,
c2: Collector<A, C2>,
c3: Collector<A, C3>,
c4: Collector<A, C4>,
combine: suspend (C1, C2, C3, C4) -> R,
): Collector<A, R> = c1.zip(c2).zip(c3).zip(c4) { (ab, c), d -> combine(ab.first, ab.second, c, d) }

/**
* Combines four [NonSuspendCollector]s by performing the phases
* of each of them in parallel.
*
* @param c1 First [NonSuspendCollector]
* @param c2 Second [NonSuspendCollector]
* @param c3 Third [NonSuspendCollector]
* @param c4 Fourth [NonSuspendCollector]
* @param combine Function that combines the end results
*/
public fun <A, C1, C2, C3, C4, R> zip(
c1: NonSuspendCollector<A, C1>,
c2: NonSuspendCollector<A, C2>,
c3: NonSuspendCollector<A, C3>,
c4: NonSuspendCollector<A, C4>,
combine: (C1, C2, C3, C4) -> R,
): NonSuspendCollector<A, R> = c1.zip(c2).zip(c3).zipNonSuspend(c4) { (ab, c), d -> combine(ab.first, ab.second, c, d) }

/**
* Combines five [Collector]s by performing the phases
* of each of them in parallel.
*
* @param c1 First [Collector]
* @param c2 Second [Collector]
* @param c3 Third [Collector]
* @param c4 Fourth [Collector]
* @param c5 Fifth [Collector]
* @param combine Function that combines the end results
*/
public fun <A, C1, C2, C3, C4, C5, R> zip(
c1: Collector<A, C1>,
c2: Collector<A, C2>,
c3: Collector<A, C3>,
c4: Collector<A, C4>,
c5: Collector<A, C5>,
combine: suspend (C1, C2, C3, C4, C5) -> R,
): Collector<A, R> = c1.zip(c2).zip(c3).zip(c4).zip(c5) { (abc, d), e -> combine(abc.first.first, abc.first.second, abc.second, d, e) }

/**
* Combines four [NonSuspendCollector]s by performing the phases
* of each of them in parallel.
*
* @param c1 First [NonSuspendCollector]
* @param c2 Second [NonSuspendCollector]
* @param c3 Third [NonSuspendCollector]
* @param c4 Fourth [NonSuspendCollector]
* @param c5 Fifth [NonSuspendCollector]
* @param combine Function that combines the end results
*/
public fun <A, C1, C2, C3, C4, C5, R> zip(
c1: NonSuspendCollector<A, C1>,
c2: NonSuspendCollector<A, C2>,
c3: NonSuspendCollector<A, C3>,
c4: NonSuspendCollector<A, C4>,
c5: NonSuspendCollector<A, C5>,
combine: (C1, C2, C3, C4, C5) -> R,
): NonSuspendCollector<A, R> = c1.zip(c2).zip(c3).zip(c4).zipNonSuspend(c5) { (abc, d), e -> combine(abc.first.first, abc.first.second, abc.second, d, e) }

0 comments on commit 2bd75ce

Please sign in to comment.