Skip to content

Commit

Permalink
Bring back handleErrorWith (#3456)
Browse files Browse the repository at this point in the history
  • Loading branch information
serras authored Jun 19, 2024
1 parent b98255e commit 082c779
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 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 @@ -150,6 +150,7 @@ public final class arrow/core/EitherKt {
public static final fun flatMap (Larrow/core/Either;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public static final fun flatten (Larrow/core/Either;)Larrow/core/Either;
public static final fun getOrElse (Larrow/core/Either;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun handleErrorWith (Larrow/core/Either;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public static final fun left (Ljava/lang/Object;)Larrow/core/Either;
public static final fun leftNel (Ljava/lang/Object;)Larrow/core/Either;
public static final fun merge (Larrow/core/Either;)Ljava/lang/Object;
Expand Down Expand Up @@ -244,6 +245,7 @@ public final class arrow/core/IorKt {
public static final fun flatMap (Larrow/core/Ior;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;)Larrow/core/Ior;
public static final fun flatten (Larrow/core/Ior;Lkotlin/jvm/functions/Function2;)Larrow/core/Ior;
public static final fun getOrElse (Larrow/core/Ior;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun handleErrorWith (Larrow/core/Ior;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;)Larrow/core/Ior;
public static final fun leftIor (Ljava/lang/Object;)Larrow/core/Ior;
public static final fun rightIor (Ljava/lang/Object;)Larrow/core/Ior;
public static final fun toIorNel (Larrow/core/Ior;)Larrow/core/Ior;
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 @@ -312,6 +312,7 @@ final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?> (
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?> (kotlin.collections/Map<#A, #B>).arrow.core/padZip(kotlin.collections/Map<#A, #C>, kotlin/Function3<#A, #B?, #C?, #D>): kotlin.collections/Map<#A, #D> // arrow.core/padZip|[email protected]<0:0,0:1>(kotlin.collections.Map<0:0,0:2>;kotlin.Function3<0:0,0:1?,0:2?,0:3>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>;3§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?> (kotlin.sequences/Sequence<#A>).arrow.core/zip(kotlin.sequences/Sequence<#B>, kotlin.sequences/Sequence<#C>, kotlin/Function3<#A, #B, #C, #D>): kotlin.sequences/Sequence<#D> // arrow.core/zip|[email protected]<0:0>(kotlin.sequences.Sequence<0:1>;kotlin.sequences.Sequence<0:2>;kotlin.Function3<0:0,0:1,0:2,0:3>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>;3§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?, #D: kotlin/Any?> (kotlin/Triple<#A, #B, #C>).arrow.core/plus(#D): arrow.core/Tuple4<#A, #B, #C, #D> // arrow.core/plus|[email protected]<0:0,0:1,0:2>(0:3){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>;3§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (arrow.core/Either<#A, #B>).arrow.core/handleErrorWith(kotlin/Function1<#A, arrow.core/Either<#C, #B>>): arrow.core/Either<#C, #B> // arrow.core/handleErrorWith|[email protected]<0:0,0:1>(kotlin.Function1<0:0,arrow.core.Either<0:2,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (arrow.core/NonEmptyList<#C>).arrow.core/unzip(kotlin/Function1<#C, kotlin/Pair<#A, #B>>): kotlin/Pair<arrow.core/NonEmptyList<#A>, arrow.core/NonEmptyList<#B>> // arrow.core/unzip|[email protected]<0:2>(kotlin.Function1<0:2,kotlin.Pair<0:0,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Iterable<#A>).arrow.core/crosswalkMap(kotlin/Function1<#A, kotlin.collections/Map<#B, #C>>): kotlin.collections/Map<#B, kotlin.collections/List<#C>> // arrow.core/crosswalkMap|[email protected]<0:0>(kotlin.Function1<0:0,kotlin.collections.Map<0:1,0:2>>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Map<#A, #B>).arrow.core/align(kotlin.collections/Map<#A, #C>): kotlin.collections/Map<#A, arrow.core/Ior<#B, #C>> // arrow.core/align|[email protected]<0:0,0:1>(kotlin.collections.Map<0:0,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
Expand Down Expand Up @@ -505,6 +506,7 @@ final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (arrow.core
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (arrow.core/Either<#A, #B>).arrow.core/flatMap(kotlin/Function1<#B, arrow.core/Either<#A, #C>>): arrow.core/Either<#A, #C> // arrow.core/flatMap|[email protected]<0:0,0:1>(kotlin.Function1<0:1,arrow.core.Either<0:0,0:2>>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (arrow.core/Either<#A, #C>).arrow.core/recover(kotlin/Function2<arrow.core.raise/Raise<#B>, #A, #C>): arrow.core/Either<#B, #C> // arrow.core/recover|[email protected]<0:0,0:2>(kotlin.Function2<arrow.core.raise.Raise<0:1>,0:0,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (arrow.core/Ior<#A, #B>).arrow.core/flatMap(kotlin/Function2<#A, #A, #A>, kotlin/Function1<#B, arrow.core/Ior<#A, #C>>): arrow.core/Ior<#A, #C> // arrow.core/flatMap|[email protected]<0:0,0:1>(kotlin.Function2<0:0,0:0,0:0>;kotlin.Function1<0:1,arrow.core.Ior<0:0,0:2>>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (arrow.core/Ior<#A, #B>).arrow.core/handleErrorWith(kotlin/Function2<#B, #B, #B>, kotlin/Function1<#A, arrow.core/Ior<#C, #B>>): arrow.core/Ior<#C, #B> // arrow.core/handleErrorWith|[email protected]<0:0,0:1>(kotlin.Function2<0:1,0:1,0:1>;kotlin.Function1<0:0,arrow.core.Ior<0:2,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (arrow.core/NonEmptyList<#B>).arrow.core/mapOrAccumulate(kotlin/Function2<#A, #A, #A>, kotlin/Function2<arrow.core.raise/RaiseAccumulate<#A>, #B, #C>): arrow.core/Either<#A, arrow.core/NonEmptyList<#C>> // arrow.core/mapOrAccumulate|[email protected]<0:1>(kotlin.Function2<0:0,0:0,0:0>;kotlin.Function2<arrow.core.raise.RaiseAccumulate<0:0>,0:1,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (arrow.core/NonEmptyList<#B>).arrow.core/mapOrAccumulate(kotlin/Function2<arrow.core.raise/RaiseAccumulate<#A>, #B, #C>): arrow.core/Either<arrow.core/NonEmptyList<#A>, arrow.core/NonEmptyList<#C>> // arrow.core/mapOrAccumulate|[email protected]<0:1>(kotlin.Function2<arrow.core.raise.RaiseAccumulate<0:0>,0:1,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Iterable<#A>).arrow.core/align(kotlin.collections/Iterable<#B>, kotlin/Function1<arrow.core/Ior<#A, #B>, #C>): kotlin.collections/List<#C> // arrow.core/align|[email protected]<0:0>(kotlin.collections.Iterable<0:1>;kotlin.Function1<arrow.core.Ior<0:0,0:1>,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,21 @@ public inline fun <A, B, C> Either<A, B>.flatMap(f: (right: B) -> Either<A, C>):
}
}

/**
* Binds the given function across [Left], that is,
* Map, or transform, the left value [A] of this [Either] into a new [Either] with a left value of type [C].
* Returns a new [Either] with either the original right value of type [B] or the newly transformed left value of type [C].
*
* @param f The function to bind across [Left].
*/
public fun <A, B, C> Either<A, B>.handleErrorWith(f: (A) -> Either<C, B>): Either<C, B> {
contract { callsInPlace(f, InvocationKind.AT_MOST_ONCE) }
return when (this) {
is Left -> f(this.value)
is Right -> this
}
}

public fun <A, B> Either<A, Either<A, B>>.flatten(): Either<A, B> =
flatMap(::identity)

Expand Down
16 changes: 16 additions & 0 deletions arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Ior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,22 @@ public inline fun <A, B, D> Ior<A, B>.flatMap(combine: (A, A) -> A, f: (B) -> Io
}
}

/**
* Binds the given function across [Ior.Left].
*
* @param f The function to bind across [Ior.Left].
*/
public inline fun <A, B, D> Ior<A, B>.handleErrorWith(combine: (B, B) -> B, f: (A) -> Ior<D, B>): Ior<D, B> =
when (this) {
is Left -> f(value)
is Right -> this
is Both -> when (val l = f(leftValue)) {
is Left -> Both(l.value, this.rightValue)
is Right -> Right(combine(this.rightValue, l.value))
is Both -> Both(l.leftValue, combine(this.rightValue, l.rightValue))
}
}

public inline fun <A, B> Ior<A, B>.getOrElse(default: (A) -> B): B {
contract { callsInPlace(default, InvocationKind.AT_MOST_ONCE) }
return when (this) {
Expand Down

0 comments on commit 082c779

Please sign in to comment.