Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename traverseX and sequenceX functions in core #2715

Merged
merged 13 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ public abstract class arrow/core/Either {
public fun toString ()Ljava/lang/String;
public final fun toValidated ()Larrow/core/Validated;
public final fun toValidatedNel ()Larrow/core/Validated;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public final fun traverseNullable (Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public final fun traverseOption (Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
Expand Down Expand Up @@ -265,6 +267,9 @@ public final class arrow/core/EitherKt {
public static final fun right (Ljava/lang/Object;)Larrow/core/Either;
public static final fun rightIfNotNull (Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Larrow/core/Either;
public static final fun rightIfNull (Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Larrow/core/Either;
public static final fun sequence (Larrow/core/Either;)Larrow/core/Either;
public static final fun sequence (Larrow/core/Either;)Larrow/core/Option;
public static final fun sequence (Larrow/core/Either;)Larrow/core/Validated;
public static final fun sequence (Larrow/core/Either;)Ljava/util/List;
public static final fun sequenceNullable (Larrow/core/Either;)Larrow/core/Either;
public static final fun sequenceOption (Larrow/core/Either;)Larrow/core/Option;
Expand Down Expand Up @@ -445,6 +450,9 @@ public abstract class arrow/core/Ior {
public final fun toEither ()Larrow/core/Either;
public fun toString ()Ljava/lang/String;
public final fun toValidated ()Larrow/core/Validated;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public final fun traverseEither (Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public final fun traverseNullable (Lkotlin/jvm/functions/Function1;)Larrow/core/Ior;
Expand Down Expand Up @@ -531,6 +539,10 @@ public final class arrow/core/IorKt {
public static final fun replicate (Larrow/core/Ior;Larrow/typeclasses/Semigroup;I)Larrow/core/Ior;
public static final fun replicate (Larrow/core/Ior;Larrow/typeclasses/Semigroup;ILarrow/typeclasses/Monoid;)Larrow/core/Ior;
public static final fun rightIor (Ljava/lang/Object;)Larrow/core/Ior;
public static final fun sequence (Larrow/core/Ior;)Larrow/core/Either;
public static final fun sequence (Larrow/core/Ior;)Larrow/core/Ior;
public static final fun sequence (Larrow/core/Ior;)Larrow/core/Option;
public static final fun sequence (Larrow/core/Ior;)Larrow/core/Validated;
public static final fun sequence (Larrow/core/Ior;)Ljava/util/List;
public static final fun sequenceEither (Larrow/core/Ior;)Larrow/core/Either;
public static final fun sequenceNullable (Larrow/core/Ior;)Larrow/core/Ior;
Expand Down Expand Up @@ -645,9 +657,15 @@ public final class arrow/core/MapKt {
public static final fun padZip (Ljava/util/Map;Ljava/util/Map;)Ljava/util/Map;
public static final fun padZip (Ljava/util/Map;Ljava/util/Map;Lkotlin/jvm/functions/Function3;)Ljava/util/Map;
public static final fun salign (Ljava/util/Map;Larrow/typeclasses/Semigroup;Ljava/util/Map;)Ljava/util/Map;
public static final fun sequence (Ljava/util/Map;)Larrow/core/Either;
public static final fun sequence (Ljava/util/Map;)Larrow/core/Option;
public static final fun sequence (Ljava/util/Map;Larrow/typeclasses/Semigroup;)Larrow/core/Validated;
public static final fun sequenceEither (Ljava/util/Map;)Larrow/core/Either;
public static final fun sequenceOption (Ljava/util/Map;)Larrow/core/Option;
public static final fun sequenceValidated (Ljava/util/Map;Larrow/typeclasses/Semigroup;)Larrow/core/Validated;
public static final fun traverse (Ljava/util/Map;Larrow/typeclasses/Semigroup;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
public static final fun traverse (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public static final fun traverse (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public static final fun traverseEither (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public static final fun traverseOption (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public static final fun traverseValidated (Ljava/util/Map;Larrow/typeclasses/Semigroup;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
Expand Down Expand Up @@ -727,9 +745,15 @@ public final class arrow/core/NonEmptyListKt {
public static final fun flatten (Larrow/core/NonEmptyList;)Larrow/core/NonEmptyList;
public static final fun nel (Ljava/lang/Object;)Larrow/core/NonEmptyList;
public static final fun nonEmptyListOf (Ljava/lang/Object;[Ljava/lang/Object;)Larrow/core/NonEmptyList;
public static final fun sequence (Larrow/core/NonEmptyList;)Larrow/core/Either;
public static final fun sequence (Larrow/core/NonEmptyList;)Larrow/core/Option;
public static final fun sequence (Larrow/core/NonEmptyList;Larrow/typeclasses/Semigroup;)Larrow/core/Validated;
public static final fun sequenceEither (Larrow/core/NonEmptyList;)Larrow/core/Either;
public static final fun sequenceOption (Larrow/core/NonEmptyList;)Larrow/core/Option;
public static final fun sequenceValidated (Larrow/core/NonEmptyList;Larrow/typeclasses/Semigroup;)Larrow/core/Validated;
public static final fun traverse (Larrow/core/NonEmptyList;Larrow/typeclasses/Semigroup;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
public static final fun traverse (Larrow/core/NonEmptyList;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public static final fun traverse (Larrow/core/NonEmptyList;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public static final fun traverseEither (Larrow/core/NonEmptyList;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public static final fun traverseOption (Larrow/core/NonEmptyList;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public static final fun traverseValidated (Larrow/core/NonEmptyList;Larrow/typeclasses/Semigroup;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
Expand Down Expand Up @@ -804,6 +828,8 @@ public abstract class arrow/core/Option {
public final fun toEither (Lkotlin/jvm/functions/Function0;)Larrow/core/Either;
public final fun toList ()Ljava/util/List;
public fun toString ()Ljava/lang/String;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public final fun traverseEither (Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public final fun traverseValidated (Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
Expand Down Expand Up @@ -851,6 +877,8 @@ public final class arrow/core/OptionKt {
public static final fun salign (Larrow/core/Option;Larrow/typeclasses/Semigroup;Larrow/core/Option;)Larrow/core/Option;
public static final fun separateEither (Larrow/core/Option;)Lkotlin/Pair;
public static final fun separateValidated (Larrow/core/Option;)Lkotlin/Pair;
public static final fun sequence (Larrow/core/Option;)Larrow/core/Either;
public static final fun sequence (Larrow/core/Option;)Larrow/core/Validated;
public static final fun sequence (Larrow/core/Option;)Ljava/util/List;
public static final fun sequenceEither (Larrow/core/Option;)Larrow/core/Either;
public static final fun sequenceValidated (Larrow/core/Option;)Larrow/core/Validated;
Expand Down Expand Up @@ -2298,6 +2326,8 @@ public abstract class arrow/core/Validated {
public final fun toOption ()Larrow/core/Option;
public fun toString ()Ljava/lang/String;
public final fun toValidatedNel ()Larrow/core/Validated;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public final fun traverse (Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public final fun traverseEither (Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public final fun traverseNullable (Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
Expand Down Expand Up @@ -2373,6 +2403,9 @@ public final class arrow/core/ValidatedKt {
public static final fun redeem (Larrow/core/Validated;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
public static final fun replicate (Larrow/core/Validated;Larrow/typeclasses/Semigroup;I)Larrow/core/Validated;
public static final fun replicate (Larrow/core/Validated;Larrow/typeclasses/Semigroup;ILarrow/typeclasses/Monoid;)Larrow/core/Validated;
public static final fun sequence (Larrow/core/Validated;)Larrow/core/Either;
public static final fun sequence (Larrow/core/Validated;)Larrow/core/Option;
public static final fun sequence (Larrow/core/Validated;)Larrow/core/Validated;
public static final fun sequence (Larrow/core/Validated;)Ljava/util/List;
public static final fun sequenceEither (Larrow/core/Validated;)Larrow/core/Either;
public static final fun sequenceNullable (Larrow/core/Validated;)Larrow/core/Validated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import arrow.core.Either.Left
import arrow.core.Either.Right
import arrow.typeclasses.Monoid
import arrow.typeclasses.Semigroup
import kotlin.experimental.ExperimentalTypeInference
import kotlin.js.JsName
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic
Expand Down Expand Up @@ -1000,21 +1001,35 @@ public sealed class Either<out A, out B> {
is Right -> List(n) { this.value }.right()
}

@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <C> traverse(fa: (B) -> Iterable<C>): List<Either<A, C>> =
fold({ emptyList() }, { fa(it).map { Right(it) } })
fold({ emptyList() }, { fa(it).map(::Right) })

@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <C> traverse(fa: (B) -> Option<C>): Option<Either<A, C>> =
fold({ None }, { right -> fa(right).map(::Right) })

@Deprecated("traverseOption is being renamed to traverse to simplify the Arrow API", ReplaceWith("traverse(fa)"))
public inline fun <C> traverseOption(fa: (B) -> Option<C>): Option<Either<A, C>> =
fold({ None }, { right -> fa(right).map { Right(it) } })
traverse(fa)

public inline fun <C> traverseNullable(fa: (B) -> C?): Either<A, C>? =
fold({ null }, { right -> fa(right)?.let { Right(it) } })
fold({ null }, { right -> fa(right)?.let(::Right) })

public inline fun <AA, C> traverseValidated(fa: (B) -> Validated<AA, C>): Validated<AA, Either<A, C>> =
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <AA, C> traverse(fa: (B) -> Validated<AA, C>): Validated<AA, Either<A, C>> =
when (this) {
is Right -> fa(this.value).map { Right(it) }
is Right -> fa(this.value).map(::Right)
is Left -> this.valid()
}

@Deprecated("traverseValidated is being renamed to traverse to simplify the Arrow API", ReplaceWith("traverse(fa)"))
public inline fun <AA, C> traverseValidated(fa: (B) -> Validated<AA, C>): Validated<AA, Either<A, C>> =
traverse(fa)

public inline fun <AA, C> bitraverse(fe: (A) -> Iterable<AA>, fa: (B) -> Iterable<C>): List<Either<AA, C>> =
fold({ fe(it).map { Left(it) } }, { fa(it).map { Right(it) } })

Expand Down Expand Up @@ -1710,14 +1725,26 @@ public inline fun <A, B, C, D> Either<A, B>.redeemWith(fa: (A) -> Either<C, D>,
public fun <A, B> Either<A, Iterable<B>>.sequence(): List<Either<A, B>> =
traverse(::identity)

@Deprecated("sequenceOption is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence()", "arrow.core.sequence"))
public fun <A, B> Either<A, Option<B>>.sequenceOption(): Option<Either<A, B>> =
traverseOption(::identity)
sequence()

public fun <A, B> Either<A, Option<B>>.sequence(): Option<Either<A, B>> =
traverse(::identity)

@Deprecated("sequenceNullable is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence()", "arrow.core.sequence"))
public fun <A, B> Either<A, B?>.sequenceNullable(): Either<A, B>? =
sequence()

public fun <A, B> Either<A, B?>.sequence(): Either<A, B>? =
traverseNullable(::identity)

@Deprecated("sequenceValidated is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence()", "arrow.core.sequence"))
public fun <A, B, C> Either<A, Validated<B, C>>.sequenceValidated(): Validated<B, Either<A, C>> =
traverseValidated(::identity)
sequence()

public fun <A, B, C> Either<A, Validated<B, C>>.sequence(): Validated<B, Either<A, C>> =
traverse(::identity)

public fun <A, B> Either<Iterable<A>, Iterable<B>>.bisequence(): List<Either<A, B>> =
bitraverse(::identity, ::identity)
Expand Down
47 changes: 42 additions & 5 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 @@ -5,6 +5,7 @@ import arrow.core.Ior.Left
import arrow.core.Ior.Right
import arrow.typeclasses.Monoid
import arrow.typeclasses.Semigroup
import kotlin.experimental.ExperimentalTypeInference
import kotlin.jvm.JvmStatic

public typealias IorNel<A, B> = Ior<Nel<A>, B>
Expand Down Expand Up @@ -543,41 +544,61 @@ public sealed class Ior<out A, out B> {

public fun isNotEmpty(): Boolean = !isLeft

@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <C> traverse(fa: (B) -> Iterable<C>): List<Ior<A, C>> =
fold(
{ a -> listOf(Left(a)) },
{ b -> fa(b).map { Right(it) } },
{ a, b -> fa(b).map { Both(a, it) } }
)

@Deprecated("traverseEither is being renamed to traverse to simplify the Arrow API", ReplaceWith("traverse(fa)"))
public inline fun <AA, C> traverseEither(fa: (B) -> Either<AA, C>): Either<AA, Ior<A, C>> =
traverse(fa)

@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <AA, C> traverse(fa: (B) -> Either<AA, C>): Either<AA, Ior<A, C>> =
fold(
{ a -> Either.Right(Left(a)) },
{ b -> fa(b).map { Right(it) } },
{ a, b -> fa(b).map { Both(a, it) } }
)

public inline fun <C> traverseOption(fa: (B) -> Option<C>): Option<Ior<A, C>> =
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <C> traverse(fa: (B) -> Option<C>): Option<Ior<A, C>> =
fold(
{ a -> Some(Left(a)) },
{ b -> fa(b).map { Right(it) } },
{ a, b -> fa(b).map { Both(a, it) } }
)

@Deprecated("traverseOption is being renamed to traverse to simplify the Arrow API", ReplaceWith("traverse(fa)"))
public inline fun <C> traverseOption(fa: (B) -> Option<C>): Option<Ior<A, C>> =
traverse(fa)

public inline fun <C> traverseNullable(fa: (B) -> C?): Ior<A, C>? =
fold(
{ a -> Left(a) },
{ b -> fa(b)?.let { Right(it) } },
{ a, b -> fa(b)?.let { Both(a, it) } }
)

public inline fun <AA, C> traverseValidated(fa: (B) -> Validated<AA, C>): Validated<AA, Ior<A, C>> =
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <AA, C> traverse(fa: (B) -> Validated<AA, C>): Validated<AA, Ior<A, C>> =
fold(
{ a -> Valid(Left(a)) },
{ b -> fa(b).map { Right(it) } },
{ a, b -> fa(b).map { Both(a, it) } }
)

@Deprecated("traverseValidated is being renamed to traverse to simplify the Arrow API", ReplaceWith("traverse(fa)"))
public inline fun <AA, C> traverseValidated(fa: (B) -> Validated<AA, C>): Validated<AA, Ior<A, C>> =
traverse(fa)

public fun void(): Ior<A, Unit> =
map { Unit }
}
Expand Down Expand Up @@ -676,17 +697,33 @@ public fun <A, B> Ior<A, B>.replicate(SA: Semigroup<A>, n: Int, MB: Monoid<B>):
public fun <A, B> Ior<A, Iterable<B>>.sequence(): List<Ior<A, B>> =
traverse(::identity)

@Deprecated("sequenceEither is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence()", "arrow.core.sequence"))
public fun <A, B, C> Ior<A, Either<B, C>>.sequenceEither(): Either<B, Ior<A, C>> =
traverseEither(::identity)
sequence()

public fun <A, B, C> Ior<A, Either<B, C>>.sequence(): Either<B, Ior<A, C>> =
traverse(::identity)

@Deprecated("sequenceOption is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence()", "arrow.core.sequence"))
public fun <A, B> Ior<A, Option<B>>.sequenceOption(): Option<Ior<A, B>> =
traverseOption(::identity)
sequence()

public fun <A, B> Ior<A, Option<B>>.sequence(): Option<Ior<A, B>> =
traverse(::identity)

@Deprecated("sequenceOption is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence()", "arrow.core.sequence"))
public fun <A, B> Ior<A, B?>.sequenceNullable(): Ior<A, B>? =
sequence()

public fun <A, B> Ior<A, B?>.sequence(): Ior<A, B>? =
traverseNullable(::identity)

@Deprecated("sequenceValidated is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence()", "arrow.core.sequence"))
public fun <A, B, C> Ior<A, Validated<B, C>>.sequenceValidated(): Validated<B, Ior<A, C>> =
traverseValidated(::identity)
sequence()

public fun <A, B, C> Ior<A, Validated<B, C>>.sequence(): Validated<B, Ior<A, C>> =
traverse(::identity)

/**
* Given [B] is a sub type of [C], re-type this value from Ior<A, B> to Ior<A, B>
Expand Down
Loading