Skip to content

Commit

Permalink
No parZip, bindAccumulating
Browse files Browse the repository at this point in the history
  • Loading branch information
serras committed May 25, 2024
1 parent 930fc4a commit 374b810
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 75 deletions.
2 changes: 2 additions & 0 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -1065,10 +1065,12 @@ public final class arrow/core/raise/RaiseNel : arrow/core/raise/Raise {
public fun bind (Larrow/core/Either;)Ljava/lang/Object;
public fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun bindAccumulating (Larrow/core/Either;)Larrow/core/raise/RaiseNel$Value;
public fun bindAll (Ljava/lang/Iterable;)Ljava/util/List;
public fun bindAll (Ljava/util/Map;)Ljava/util/Map;
public fun bindAll-1TN0_VU (Ljava/util/Set;)Ljava/util/Set;
public fun bindAll-vcjLgH4 (Ljava/util/List;)Ljava/util/List;
public final fun bindAllAccumulating (Ljava/lang/Iterable;)Larrow/core/raise/RaiseNel$Value;
public fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun raise (Ljava/lang/Object;)Ljava/lang/Void;
Expand Down
2 changes: 2 additions & 0 deletions arrow-libs/core/arrow-core/api/arrow-core.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ final class <#A: kotlin/Any?> arrow.core.raise/RaiseNel : arrow.core.raise/Raise
constructor <init>() // arrow.core.raise/RaiseNel.Value.<init>|<init>(){}[0]
}
constructor <init>(arrow.core.raise/Raise<arrow.core/NonEmptyList<#A>>) // arrow.core.raise/RaiseNel.<init>|<init>(arrow.core.raise.Raise<arrow.core.NonEmptyList<1:0>>){}[0]
final fun <#A1: kotlin/Any?> (arrow.core/Either<#A, #A1>).bindAccumulating(): arrow.core.raise/RaiseNel.Value<#A1, #A> // arrow.core.raise/RaiseNel.bindAccumulating|bindAccumulating@arrow.core.Either<1:0,0:0>(){0§<kotlin.Any?>}[0]
final fun <#A1: kotlin/Any?> (kotlin.collections/Iterable<arrow.core/Either<#A, #A1>>).bindAllAccumulating(): arrow.core.raise/RaiseNel.Value<kotlin.collections/List<#A1>, #A> // arrow.core.raise/RaiseNel.bindAllAccumulating|bindAllAccumulating@kotlin.collections.Iterable<arrow.core.Either<1:0,0:0>>(){0§<kotlin.Any?>}[0]
final fun <#A1: kotlin/Any?> accumulating(kotlin/Function1<arrow.core.raise/RaiseAccumulate<#A>, #A1>): arrow.core.raise/RaiseNel.Value<#A1, #A> // arrow.core.raise/RaiseNel.accumulating|accumulating(kotlin.Function1<arrow.core.raise.RaiseAccumulate<1:0>,0:0>){0§<kotlin.Any?>}[0]
final fun raise(#A): kotlin/Nothing // arrow.core.raise/RaiseNel.raise|raise(1:0){}[0]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public class RaiseNel<Error>(
internal fun hasErrors(): Boolean = errors.isNotEmpty()
internal fun raiseErrors(): Nothing = raise.raise(errors.toNonEmptyListOrNull()!!)

public fun <A> Either<Error, A>.bindAccumulating(): Value<A> =
accumulating { this@bindAccumulating.bind() }

public fun <A> Iterable<Either<Error, A>>.bindAllAccumulating(): Value<List<A>> =
accumulating { this@bindAllAccumulating.bindAll() }

public fun <A> accumulating(block: RaiseAccumulate<Error>.() -> A): Value<A> =
recover({
Ok(block(RaiseAccumulate(this)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class RaiseAccumulateSpec {
} shouldBe nonEmptyListOf("bye").left()
}

@Test fun raiseAccumulatingOneFailureEither() {
eitherNel {
val x: Int by 1.right().bindAccumulating()
val y: Int by "bye".left().bindAccumulating()
x + y
} shouldBe nonEmptyListOf("bye").left()
}

@Test fun raiseAccumulatingNoFailure() {
eitherNel<String, _> {
val x by accumulating { 1 }
Expand Down
13 changes: 0 additions & 13 deletions arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.api
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public final class arrow/fx/coroutines/ParMapKt {
}

public final class arrow/fx/coroutines/ParZipKt {
public static final fun parZip (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun parZip (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function10;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun parZip (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function11;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun parZip (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function9;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand All @@ -119,7 +118,6 @@ public final class arrow/fx/coroutines/ParZipKt {
public static final fun parZip (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function6;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun parZip (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function5;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun parZip (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function4;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun parZip$default (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun parZip$default (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function10;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun parZip$default (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function11;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun parZip$default (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function9;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
Expand Down Expand Up @@ -165,17 +163,6 @@ public final class arrow/fx/coroutines/ParZipOrAccumulateKt {
public static final fun parZipOrAccumulate (Larrow/core/raise/Raise;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function4;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class arrow/fx/coroutines/ParZipScope : kotlinx/coroutines/CoroutineScope {
public fun <init> (Lkotlinx/coroutines/CoroutineScope;Lkotlin/coroutines/CoroutineContext;)V
public final fun concurrently (Lkotlin/jvm/functions/Function2;)Larrow/fx/coroutines/ParZipScope$Value;
public fun getCoroutineContext ()Lkotlin/coroutines/CoroutineContext;
}

public final class arrow/fx/coroutines/ParZipScope$Value {
public fun <init> (Larrow/fx/coroutines/ParZipScope;Lkotlinx/coroutines/Deferred;)V
public final fun value (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class arrow/fx/coroutines/Race2Kt {
public static final fun raceN (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun raceN (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down
11 changes: 0 additions & 11 deletions arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ final class arrow.fx.coroutines/CyclicBarrier { // arrow.fx.coroutines/CyclicBar
final class arrow.fx.coroutines/CyclicBarrierCancellationException : kotlin.coroutines.cancellation/CancellationException { // arrow.fx.coroutines/CyclicBarrierCancellationException|null[0]
constructor <init>() // arrow.fx.coroutines/CyclicBarrierCancellationException.<init>|<init>(){}[0]
}
final class arrow.fx.coroutines/ParZipScope : kotlinx.coroutines/CoroutineScope { // arrow.fx.coroutines/ParZipScope|null[0]
constructor <init>(kotlinx.coroutines/CoroutineScope, kotlin.coroutines/CoroutineContext) // arrow.fx.coroutines/ParZipScope.<init>|<init>(kotlinx.coroutines.CoroutineScope;kotlin.coroutines.CoroutineContext){}[0]
final fun <#A1: kotlin/Any?> concurrently(kotlin.coroutines/SuspendFunction1<kotlinx.coroutines/CoroutineScope, #A1>): arrow.fx.coroutines/ParZipScope.Value<#A1> // arrow.fx.coroutines/ParZipScope.concurrently|concurrently(kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,0:0>){0§<kotlin.Any?>}[0]
final inner class <#A1: kotlin/Any?> Value { // arrow.fx.coroutines/ParZipScope.Value|null[0]
constructor <init>(kotlinx.coroutines/Deferred<#A1>) // arrow.fx.coroutines/ParZipScope.Value.<init>|<init>(kotlinx.coroutines.Deferred<1:0>){}[0]
final suspend fun value(): #A1 // arrow.fx.coroutines/ParZipScope.Value.value|value(){}[0]
}
final val coroutineContext // arrow.fx.coroutines/ParZipScope.coroutineContext|{}coroutineContext[0]
final fun <get-coroutineContext>(): kotlin.coroutines/CoroutineContext // arrow.fx.coroutines/ParZipScope.coroutineContext.<get-coroutineContext>|<get-coroutineContext>(){}[0]
}
final fun <#A: kotlin/Any?> (kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ResourceScope, #A>).arrow.fx.coroutines/asFlow(): kotlinx.coroutines.flow/Flow<#A> // arrow.fx.coroutines/asFlow|asFlow@kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ResourceScope,0:0>(){0§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?> (kotlinx.coroutines.flow/Flow<#A>).arrow.fx.coroutines/metered(kotlin.time/Duration): kotlinx.coroutines.flow/Flow<#A> // arrow.fx.coroutines/metered|metered@kotlinx.coroutines.flow.Flow<0:0>(kotlin.time.Duration){0§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?> (kotlinx.coroutines.flow/Flow<#A>).arrow.fx.coroutines/metered(kotlin/Long): kotlinx.coroutines.flow/Flow<#A> // arrow.fx.coroutines/metered|metered@kotlinx.coroutines.flow.Flow<0:0>(kotlin.Long){0§<kotlin.Any?>}[0]
Expand All @@ -67,7 +57,6 @@ final suspend fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlin.collections/Iterabl
final suspend fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlin.collections/Iterable<#A>).arrow.fx.coroutines/parMapNotNull(kotlin.coroutines/CoroutineContext = ..., kotlin/Int, kotlin.coroutines/SuspendFunction2<kotlinx.coroutines/CoroutineScope, #A, #B?>): kotlin.collections/List<#B> // arrow.fx.coroutines/parMapNotNull|parMapNotNull@kotlin.collections.Iterable<0:0>(kotlin.coroutines.CoroutineContext;kotlin.Int;kotlin.coroutines.SuspendFunction2<kotlinx.coroutines.CoroutineScope,0:0,0:1?>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final suspend fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ResourceScope, #A>).arrow.fx.coroutines/use(kotlin.coroutines/SuspendFunction1<#A, #B>): #B // arrow.fx.coroutines/use|use@kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ResourceScope,0:0>(kotlin.coroutines.SuspendFunction1<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final suspend fun <#A: kotlin/Any?> (kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ResourceScope, #A>).arrow.fx.coroutines/allocated(): kotlin/Pair<#A, kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ExitCase, kotlin/Unit>> // arrow.fx.coroutines/allocated|allocated@kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ResourceScope,0:0>(){0§<kotlin.Any?>}[0]
final suspend fun <#A: kotlin/Any?> arrow.fx.coroutines/parZip(kotlin.coroutines/CoroutineContext = ..., kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ParZipScope, #A>): #A // arrow.fx.coroutines/parZip|parZip(kotlin.coroutines.CoroutineContext;kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ParZipScope,0:0>){0§<kotlin.Any?>}[0]
final suspend fun <#A: kotlin/Any?> arrow.fx.coroutines/resourceScope(kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ResourceScope, #A>): #A // arrow.fx.coroutines/resourceScope|resourceScope(kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ResourceScope,0:0>){0§<kotlin.Any?>}[0]
final suspend fun arrow.fx.coroutines/cancelAndCompose(kotlinx.coroutines/Deferred<*>, kotlinx.coroutines/Deferred<*>) // arrow.fx.coroutines/cancelAndCompose|cancelAndCompose(kotlinx.coroutines.Deferred<*>;kotlinx.coroutines.Deferred<*>){}[0]
final suspend inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?, #E: kotlin/Any?, #F: kotlin/Any?, #G: kotlin/Any?, #H: kotlin/Any?, #I: kotlin/Any?, #J: kotlin/Any?, #K: kotlin/Any?> (arrow.core.raise/Raise<#A>).arrow.fx.coroutines/parZipOrAccumulate(crossinline kotlin/Function2<#A, #A, #A>, crossinline kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ScopedRaiseAccumulate<#A>, #B>, crossinline kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ScopedRaiseAccumulate<#A>, #C>, crossinline kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ScopedRaiseAccumulate<#A>, #D>, crossinline kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ScopedRaiseAccumulate<#A>, #E>, crossinline kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ScopedRaiseAccumulate<#A>, #F>, crossinline kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ScopedRaiseAccumulate<#A>, #G>, crossinline kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ScopedRaiseAccumulate<#A>, #H>, crossinline kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ScopedRaiseAccumulate<#A>, #I>, crossinline kotlin.coroutines/SuspendFunction1<arrow.fx.coroutines/ScopedRaiseAccumulate<#A>, #J>, crossinline kotlin.coroutines/SuspendFunction10<kotlinx.coroutines/CoroutineScope, #B, #C, #D, #E, #F, #G, #H, #I, #J, #K>): #K // arrow.fx.coroutines/parZipOrAccumulate|parZipOrAccumulate@arrow.core.raise.Raise<0:0>(kotlin.Function2<0:0,0:0,0:0>;kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ScopedRaiseAccumulate<0:0>,0:1>;kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ScopedRaiseAccumulate<0:0>,0:2>;kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ScopedRaiseAccumulate<0:0>,0:3>;kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ScopedRaiseAccumulate<0:0>,0:4>;kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ScopedRaiseAccumulate<0:0>,0:5>;kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ScopedRaiseAccumulate<0:0>,0:6>;kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ScopedRaiseAccumulate<0:0>,0:7>;kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ScopedRaiseAccumulate<0:0>,0:8>;kotlin.coroutines.SuspendFunction1<arrow.fx.coroutines.ScopedRaiseAccumulate<0:0>,0:9>;kotlin.coroutines.SuspendFunction10<kotlinx.coroutines.CoroutineScope,0:1,0:2,0:3,0:4,0:5,0:6,0:7,0:8,0:9,0:10>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>;3§<kotlin.Any?>;4§<kotlin.Any?>;5§<kotlin.Any?>;6§<kotlin.Any?>;7§<kotlin.Any?>;8§<kotlin.Any?>;9§<kotlin.Any?>;10§<kotlin.Any?>}[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,33 +826,3 @@ public suspend inline fun <A, B, C, D, E, F, G, H, I, J> parZip(
val res = awaitAll(faa, fbb, fcc, fdd, fee, fDef, fgg, fhh, fii)
f(res[0] as A, res[1] as B, res[2] as C, res[3] as D, res[4] as E, res[5] as F, res[6] as G, res[7] as H, res[8] as I)
}

public suspend fun <A> parZip(
ctx: CoroutineContext = EmptyCoroutineContext,
block: suspend ParZipScope.() -> A
): A = coroutineScope {
block(ParZipScope(this@coroutineScope, ctx))
}

public class ParZipScope(
private val scope: CoroutineScope,
private val ctx: CoroutineContext
): CoroutineScope by scope {
private val tasks: MutableList<Deferred<*>> = mutableListOf()

internal suspend fun runTasks() = tasks.awaitAll()

public fun <A> concurrently(block: suspend CoroutineScope.() -> A): Value<A> {
val task = async(context = ctx, block = block)
tasks.add(task)
return Value(task)
}

public inner class Value<A>(private val task: Deferred<A>) {
// public suspend operator fun getValue(value: Nothing?, property: KProperty<*>): A {
public suspend fun value(): A {
runTasks()
return task.await()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,6 @@ class ParZip2Test {
}
}

@Test fun parZip2RunsInParallelWithConcurrently() = runTestUsingDefaultDispatcher {
checkAll(Arb.int(), Arb.int()) { a, b ->
val r = AtomicInt(0)
val modifyGate = CompletableDeferred<Int>()

parZip {
val _a = concurrently {
modifyGate.await()
r.update { i -> i + a }
}
val _b = concurrently {
r.value = b
modifyGate.complete(0)
}
Pair(_a.value(), _b.value())
}

r.value shouldBe b + a
}
}

@Test fun cancellingParZip2CancelsAllParticipants() = runTestUsingDefaultDispatcher {
checkAll(Arb.int(), Arb.int()) { a, b ->
val s = Channel<Unit>()
Expand Down

0 comments on commit 374b810

Please sign in to comment.