Skip to content

Commit

Permalink
More overloads for collectors (#3539)
Browse files Browse the repository at this point in the history
  • Loading branch information
serras authored Nov 27, 2024
1 parent 63b162f commit 3808d09
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
8 changes: 8 additions & 0 deletions arrow-libs/fx/arrow-collectors/api/arrow-collectors.api
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ public abstract interface class arrow/collectors/CollectorI {
}

public final class arrow/collectors/CollectorI$Companion {
public final fun nonSuspendOf (Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;Ljava/util/Set;)Larrow/collectors/NonSuspendCollectorI;
public final fun nonSuspendOf (Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Ljava/util/Set;)Larrow/collectors/NonSuspendCollectorI;
public static synthetic fun nonSuspendOf$default (Larrow/collectors/CollectorI$Companion;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;Ljava/util/Set;ILjava/lang/Object;)Larrow/collectors/NonSuspendCollectorI;
public static synthetic fun nonSuspendOf$default (Larrow/collectors/CollectorI$Companion;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Ljava/util/Set;ILjava/lang/Object;)Larrow/collectors/NonSuspendCollectorI;
public final fun of (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Ljava/util/Set;)Larrow/collectors/CollectorI;
public final fun of (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function2;Ljava/util/Set;)Larrow/collectors/CollectorI;
public static synthetic fun of$default (Larrow/collectors/CollectorI$Companion;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Ljava/util/Set;ILjava/lang/Object;)Larrow/collectors/CollectorI;
public static synthetic fun of$default (Larrow/collectors/CollectorI$Companion;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function2;Ljava/util/Set;ILjava/lang/Object;)Larrow/collectors/CollectorI;
}

Expand Down Expand Up @@ -111,8 +115,12 @@ public final class arrow/collectors/NonSuspendCollectorI$DefaultImpls {
}

public final class arrow/collectors/ZipKt {
public static final fun zip (Larrow/collectors/CollectorI;Larrow/collectors/CollectorI;Larrow/collectors/CollectorI;Larrow/collectors/CollectorI;Larrow/collectors/CollectorI;Lkotlin/jvm/functions/Function6;)Larrow/collectors/CollectorI;
public static final fun zip (Larrow/collectors/CollectorI;Larrow/collectors/CollectorI;Larrow/collectors/CollectorI;Larrow/collectors/CollectorI;Lkotlin/jvm/functions/Function5;)Larrow/collectors/CollectorI;
public static final fun zip (Larrow/collectors/CollectorI;Larrow/collectors/CollectorI;Larrow/collectors/CollectorI;Lkotlin/jvm/functions/Function4;)Larrow/collectors/CollectorI;
public static final fun zip (Larrow/collectors/CollectorI;Larrow/collectors/CollectorI;Lkotlin/jvm/functions/Function3;)Larrow/collectors/CollectorI;
public static final fun zip (Larrow/collectors/NonSuspendCollectorI;Larrow/collectors/NonSuspendCollectorI;Larrow/collectors/NonSuspendCollectorI;Larrow/collectors/NonSuspendCollectorI;Larrow/collectors/NonSuspendCollectorI;Lkotlin/jvm/functions/Function5;)Larrow/collectors/NonSuspendCollectorI;
public static final fun zip (Larrow/collectors/NonSuspendCollectorI;Larrow/collectors/NonSuspendCollectorI;Larrow/collectors/NonSuspendCollectorI;Larrow/collectors/NonSuspendCollectorI;Lkotlin/jvm/functions/Function4;)Larrow/collectors/NonSuspendCollectorI;
public static final fun zip (Larrow/collectors/NonSuspendCollectorI;Larrow/collectors/NonSuspendCollectorI;Larrow/collectors/NonSuspendCollectorI;Lkotlin/jvm/functions/Function3;)Larrow/collectors/NonSuspendCollectorI;
public static final fun zip (Larrow/collectors/NonSuspendCollectorI;Larrow/collectors/NonSuspendCollectorI;Lkotlin/jvm/functions/Function2;)Larrow/collectors/NonSuspendCollectorI;
}
Expand Down
6 changes: 6 additions & 0 deletions arrow-libs/fx/arrow-collectors/api/arrow-collectors.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ abstract interface <#A: kotlin/Any?, #B: in kotlin/Any?, #C: out kotlin/Any?> ar
final object Companion { // arrow.collectors/CollectorI.Companion|null[0]
final fun <#A2: kotlin/Any?, #B2: kotlin/Any?, #C2: kotlin/Any?> nonSuspendOf(kotlin/Function0<#A2>, kotlin/Function2<#A2, #B2, kotlin/Unit>, kotlin/Function1<#A2, #C2>, kotlin.collections/Set<arrow.collectors/Characteristics> = ...): arrow.collectors/NonSuspendCollectorI<*, #B2, #C2> // arrow.collectors/CollectorI.Companion.nonSuspendOf|nonSuspendOf(kotlin.Function0<0:0>;kotlin.Function2<0:0,0:1,kotlin.Unit>;kotlin.Function1<0:0,0:2>;kotlin.collections.Set<arrow.collectors.Characteristics>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A2: kotlin/Any?, #B2: kotlin/Any?, #C2: kotlin/Any?> of(kotlin.coroutines/SuspendFunction0<#A2>, kotlin.coroutines/SuspendFunction2<#A2, #B2, kotlin/Unit>, kotlin.coroutines/SuspendFunction1<#A2, #C2>, kotlin.collections/Set<arrow.collectors/Characteristics> = ...): arrow.collectors/CollectorI<*, #B2, #C2> // arrow.collectors/CollectorI.Companion.of|of(kotlin.coroutines.SuspendFunction0<0:0>;kotlin.coroutines.SuspendFunction2<0:0,0:1,kotlin.Unit>;kotlin.coroutines.SuspendFunction1<0:0,0:2>;kotlin.collections.Set<arrow.collectors.Characteristics>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A2: kotlin/Any?, #B2: kotlin/Any?> nonSuspendOf(kotlin/Function0<#B2>, kotlin/Function2<#B2, #A2, kotlin/Unit>, kotlin.collections/Set<arrow.collectors/Characteristics> = ...): arrow.collectors/NonSuspendCollectorI<*, #A2, #B2> // arrow.collectors/CollectorI.Companion.nonSuspendOf|nonSuspendOf(kotlin.Function0<0:1>;kotlin.Function2<0:1,0:0,kotlin.Unit>;kotlin.collections.Set<arrow.collectors.Characteristics>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A2: kotlin/Any?, #B2: kotlin/Any?> of(kotlin.coroutines/SuspendFunction0<#B2>, kotlin.coroutines/SuspendFunction2<#B2, #A2, kotlin/Unit>, kotlin.collections/Set<arrow.collectors/Characteristics> = ...): arrow.collectors/CollectorI<*, #A2, #B2> // arrow.collectors/CollectorI.Companion.of|of(kotlin.coroutines.SuspendFunction0<0:1>;kotlin.coroutines.SuspendFunction2<0:1,0:0,kotlin.Unit>;kotlin.collections.Set<arrow.collectors.Characteristics>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
}
}

Expand Down Expand Up @@ -79,6 +81,10 @@ final object arrow.collectors/Collectors { // arrow.collectors/Collectors|null[0
final fun intReducer(kotlin/Function0<kotlin/Int>, kotlin/Function2<kotlin/Int, kotlin/Int, kotlin/Int>): arrow.collectors/NonSuspendCollectorI<*, kotlin/Int, kotlin/Int> // arrow.collectors/Collectors.intReducer|intReducer(kotlin.Function0<kotlin.Int>;kotlin.Function2<kotlin.Int,kotlin.Int,kotlin.Int>){}[0]
}

final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?, #E: kotlin/Any?, #F: kotlin/Any?, #G: kotlin/Any?> arrow.collectors/zip(arrow.collectors/CollectorI<*, #A, #B>, arrow.collectors/CollectorI<*, #A, #C>, arrow.collectors/CollectorI<*, #A, #D>, arrow.collectors/CollectorI<*, #A, #E>, arrow.collectors/CollectorI<*, #A, #F>, kotlin.coroutines/SuspendFunction5<#B, #C, #D, #E, #F, #G>): arrow.collectors/CollectorI<*, #A, #G> // arrow.collectors/zip|zip(arrow.collectors.CollectorI<*,0:0,0:1>;arrow.collectors.CollectorI<*,0:0,0:2>;arrow.collectors.CollectorI<*,0:0,0:3>;arrow.collectors.CollectorI<*,0:0,0:4>;arrow.collectors.CollectorI<*,0:0,0:5>;kotlin.coroutines.SuspendFunction5<0:1,0:2,0:3,0:4,0:5,0:6>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>;3§<kotlin.Any?>;4§<kotlin.Any?>;5§<kotlin.Any?>;6§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?, #E: kotlin/Any?, #F: kotlin/Any?, #G: kotlin/Any?> arrow.collectors/zip(arrow.collectors/NonSuspendCollectorI<*, #A, #B>, arrow.collectors/NonSuspendCollectorI<*, #A, #C>, arrow.collectors/NonSuspendCollectorI<*, #A, #D>, arrow.collectors/NonSuspendCollectorI<*, #A, #E>, arrow.collectors/NonSuspendCollectorI<*, #A, #F>, kotlin/Function5<#B, #C, #D, #E, #F, #G>): arrow.collectors/NonSuspendCollectorI<*, #A, #G> // arrow.collectors/zip|zip(arrow.collectors.NonSuspendCollectorI<*,0:0,0:1>;arrow.collectors.NonSuspendCollectorI<*,0:0,0:2>;arrow.collectors.NonSuspendCollectorI<*,0:0,0:3>;arrow.collectors.NonSuspendCollectorI<*,0:0,0:4>;arrow.collectors.NonSuspendCollectorI<*,0:0,0:5>;kotlin.Function5<0:1,0:2,0:3,0:4,0:5,0:6>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>;3§<kotlin.Any?>;4§<kotlin.Any?>;5§<kotlin.Any?>;6§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?, #E: kotlin/Any?, #F: kotlin/Any?> arrow.collectors/zip(arrow.collectors/CollectorI<*, #A, #B>, arrow.collectors/CollectorI<*, #A, #C>, arrow.collectors/CollectorI<*, #A, #D>, arrow.collectors/CollectorI<*, #A, #E>, kotlin.coroutines/SuspendFunction4<#B, #C, #D, #E, #F>): arrow.collectors/CollectorI<*, #A, #F> // arrow.collectors/zip|zip(arrow.collectors.CollectorI<*,0:0,0:1>;arrow.collectors.CollectorI<*,0:0,0:2>;arrow.collectors.CollectorI<*,0:0,0:3>;arrow.collectors.CollectorI<*,0:0,0:4>;kotlin.coroutines.SuspendFunction4<0:1,0:2,0:3,0:4,0:5>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>;3§<kotlin.Any?>;4§<kotlin.Any?>;5§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?, #E: kotlin/Any?, #F: kotlin/Any?> arrow.collectors/zip(arrow.collectors/NonSuspendCollectorI<*, #A, #B>, arrow.collectors/NonSuspendCollectorI<*, #A, #C>, arrow.collectors/NonSuspendCollectorI<*, #A, #D>, arrow.collectors/NonSuspendCollectorI<*, #A, #E>, kotlin/Function4<#B, #C, #D, #E, #F>): arrow.collectors/NonSuspendCollectorI<*, #A, #F> // arrow.collectors/zip|zip(arrow.collectors.NonSuspendCollectorI<*,0:0,0:1>;arrow.collectors.NonSuspendCollectorI<*,0:0,0:2>;arrow.collectors.NonSuspendCollectorI<*,0:0,0:3>;arrow.collectors.NonSuspendCollectorI<*,0:0,0:4>;kotlin.Function4<0:1,0:2,0:3,0:4,0:5>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>;3§<kotlin.Any?>;4§<kotlin.Any?>;5§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?, #E: kotlin/Any?> arrow.collectors/zip(arrow.collectors/CollectorI<*, #A, #B>, arrow.collectors/CollectorI<*, #A, #C>, arrow.collectors/CollectorI<*, #A, #D>, kotlin.coroutines/SuspendFunction3<#B, #C, #D, #E>): arrow.collectors/CollectorI<*, #A, #E> // arrow.collectors/zip|zip(arrow.collectors.CollectorI<*,0:0,0:1>;arrow.collectors.CollectorI<*,0:0,0:2>;arrow.collectors.CollectorI<*,0:0,0:3>;kotlin.coroutines.SuspendFunction3<0:1,0:2,0:3,0:4>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>;3§<kotlin.Any?>;4§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?, #E: kotlin/Any?> arrow.collectors/zip(arrow.collectors/NonSuspendCollectorI<*, #A, #B>, arrow.collectors/NonSuspendCollectorI<*, #A, #C>, arrow.collectors/NonSuspendCollectorI<*, #A, #D>, kotlin/Function3<#B, #C, #D, #E>): arrow.collectors/NonSuspendCollectorI<*, #A, #E> // arrow.collectors/zip|zip(arrow.collectors.NonSuspendCollectorI<*,0:0,0:1>;arrow.collectors.NonSuspendCollectorI<*,0:0,0:2>;arrow.collectors.NonSuspendCollectorI<*,0:0,0:3>;kotlin.Function3<0:1,0:2,0:3,0:4>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>;3§<kotlin.Any?>;4§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?> arrow.collectors/zip(arrow.collectors/CollectorI<*, #A, #B>, arrow.collectors/CollectorI<*, #A, #C>, kotlin.coroutines/SuspendFunction2<#B, #C, #D>): arrow.collectors/CollectorI<*, #A, #D> // arrow.collectors/zip|zip(arrow.collectors.CollectorI<*,0:0,0:1>;arrow.collectors.CollectorI<*,0:0,0:2>;kotlin.coroutines.SuspendFunction2<0:1,0:2,0:3>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>;3§<kotlin.Any?>}[0]
Expand Down
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 3808d09

Please sign in to comment.