Skip to content

Commit

Permalink
rename traverseX and sequenceX for Iterable (#2692)
Browse files Browse the repository at this point in the history
* init

* prog

* progress

* Update API files

* check

* Update arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt

Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>

* Update arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt

Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>

* Update arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt

Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>

* Update arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt

Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>

* Update arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt

Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>

* Update arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt

Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>

* Update arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt

Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>

* Update arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt

Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>

* Update arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt

Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>

* show that traverse Nullable and Either sequence interoperate

* Apply suggestions from code review

Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>

* show that traverse Nullable and Either sequence interoperate

Co-authored-by: i-walker <i-walker@users.noreply.github.com>
Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 23, 2022
1 parent 128bc08 commit 3540e3d
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 77 deletions.
12 changes: 12 additions & 0 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,12 @@ public final class arrow/core/IterableKt {
public static final fun salign (Ljava/lang/Iterable;Larrow/typeclasses/Semigroup;Ljava/lang/Iterable;)Ljava/lang/Iterable;
public static final fun separateEither (Ljava/lang/Iterable;)Lkotlin/Pair;
public static final fun separateValidated (Ljava/lang/Iterable;)Lkotlin/Pair;
public static final fun sequence (Ljava/lang/Iterable;)Larrow/core/Either;
public static final fun sequence (Ljava/lang/Iterable;)Larrow/core/Option;
public static final fun sequence (Ljava/lang/Iterable;)Larrow/core/Validated;
public static final fun sequence (Ljava/lang/Iterable;)Ljava/lang/Object;
public static final fun sequence (Ljava/lang/Iterable;)Ljava/util/List;
public static final fun sequence (Ljava/lang/Iterable;Larrow/typeclasses/Semigroup;)Larrow/core/Validated;
public static final fun sequenceEither (Ljava/lang/Iterable;)Larrow/core/Either;
public static final fun sequenceNullable (Ljava/lang/Iterable;)Ljava/util/List;
public static final fun sequenceOption (Ljava/lang/Iterable;)Larrow/core/Option;
Expand All @@ -594,6 +600,12 @@ public final class arrow/core/IterableKt {
public static final fun singleOrNone (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public static final fun split (Ljava/lang/Iterable;)Lkotlin/Pair;
public static final fun tail (Ljava/lang/Iterable;)Ljava/util/List;
public static final fun traverse (Ljava/lang/Iterable;Larrow/typeclasses/Semigroup;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
public static final fun traverse (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public static final fun traverse (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public static final fun traverse (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
public static final fun traverse (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun traverse (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public static final fun traverseEither (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public static final fun traverseNullable (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public static final fun traverseOption (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import arrow.core.Either.Right
import arrow.typeclasses.Monoid
import arrow.typeclasses.Semigroup
import kotlin.Result.Companion.success
import kotlin.experimental.ExperimentalTypeInference

public inline fun <B, C, D, E> Iterable<B>.zip(
c: Iterable<C>,
Expand Down Expand Up @@ -282,7 +283,13 @@ public inline fun <B, C, D, E, F, G, H, I, J, K, L> Iterable<B>.zip(
internal fun <T> Iterable<T>.collectionSizeOrDefault(default: Int): Int =
if (this is Collection<*>) this.size else default

public inline fun <E, A, B> Iterable<A>.traverseEither(f: (A) -> Either<E, B>): Either<E, List<B>> {
@Deprecated("traverseEither is being renamed to traverse to simplify the Arrow API", ReplaceWith("traverse(f)", "arrow.core.traverse"))
public inline fun <E, A, B> Iterable<A>.traverseEither(f: (A) -> Either<E, B>): Either<E, List<B>> =
traverse(f)

@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <E, A, B> Iterable<A>.traverse(f: (A) -> Either<E, B>): Either<E, List<B>> {
val destination = ArrayList<B>(collectionSizeOrDefault(10))
for (item in this) {
when (val res = f(item)) {
Expand All @@ -293,50 +300,94 @@ public inline fun <E, A, B> Iterable<A>.traverseEither(f: (A) -> Either<E, B>):
return destination.right()
}

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

public fun <E, A> Iterable<Either<E, A>>.sequence(): Either<E, List<A>> =
traverse(::identity)

public inline fun <A, B> Iterable<A>.traverseResult(f: (A) -> Result<B>): Result<List<B>> {
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <A, B> Iterable<A>.traverse(f: (A) -> Result<B>): Result<List<B>> {
val destination = ArrayList<B>(collectionSizeOrDefault(10))
for (item in this) {
f(item).fold(destination::add) { throwable ->
return@traverseResult Result.failure(throwable)
return@traverse Result.failure(throwable)
}
}
return success(destination)
}

@Deprecated("traverseResult is being renamed to traverse to simplify the Arrow API", ReplaceWith("traverse(f)", "arrow.core.traverse"))
public inline fun <A, B> Iterable<A>.traverseResult(f: (A) -> Result<B>): Result<List<B>> =
traverse(f)

@Deprecated("sequenceResult is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence()", "arrow.core.sequence"))
public fun <A> Iterable<Result<A>>.sequenceResult(): Result<List<A>> =
traverseResult(::identity)
sequence()

public fun <A> Iterable<Result<A>>.sequence(): Result<List<A>> =
traverse(::identity)

@Deprecated("traverseValidated is being renamed to traverse to simplify the Arrow API", ReplaceWith("traverse(semigroup, f)", "arrow.core.traverse"))
public inline fun <E, A, B> Iterable<A>.traverseValidated(
semigroup: Semigroup<E>,
f: (A) -> Validated<E, B>
): Validated<E, List<B>> = semigroup.run {
fold(Valid(ArrayList<B>(collectionSizeOrDefault(10))) as Validated<E, MutableList<B>>) { acc, a ->
when (val res = f(a)) {
is Validated.Valid -> when (acc) {
is Valid -> acc.also { it.value.add(res.value) }
is Invalid -> acc
}
is Validated.Invalid -> when (acc) {
is Valid -> res
is Invalid -> Invalid(acc.value.combine(res.value))
): Validated<E, List<B>> =
traverse(semigroup, f)

@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <E, A, B> Iterable<A>.traverse(
semigroup: Semigroup<E>,
f: (A) -> Validated<E, B>
): Validated<E, List<B>> =
semigroup.run {
fold(Valid(ArrayList<B>(collectionSizeOrDefault(10))) as Validated<E, MutableList<B>>) { acc, a ->
when (val res = f(a)) {
is Validated.Valid -> when (acc) {
is Valid -> acc.also { it.value.add(res.value) }
is Invalid -> acc
}
is Validated.Invalid -> when (acc) {
is Valid -> res
is Invalid -> Invalid(acc.value.combine(res.value))
}
}
}
}
}

@Deprecated("traverseValidated is being renamed to traverse to simplify the Arrow API", ReplaceWith("traverse(f)", "arrow.core.traverse"))
public inline fun <E, A, B> Iterable<A>.traverseValidated(f: (A) -> ValidatedNel<E, B>): ValidatedNel<E, List<B>> =
traverseValidated(Semigroup.nonEmptyList(), f)
traverse(f)

@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <E, A, B> Iterable<A>.traverse(f: (A) -> ValidatedNel<E, B>): ValidatedNel<E, List<B>> =
traverse(Semigroup.nonEmptyList(), f)

@Deprecated("sequenceValidated is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence(semigroup)", "arrow.core.sequence"))
public fun <E, A> Iterable<Validated<E, A>>.sequenceValidated(semigroup: Semigroup<E>): Validated<E, List<A>> =
traverseValidated(semigroup, ::identity)
sequence(semigroup)

public fun <E, A> Iterable<Validated<E, A>>.sequence(semigroup: Semigroup<E>): Validated<E, List<A>> =
traverse(semigroup, ::identity)

@Deprecated("sequenceValidated is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence()", "arrow.core.sequence"))
public fun <E, A> Iterable<ValidatedNel<E, A>>.sequenceValidated(): ValidatedNel<E, List<A>> =
traverseValidated(Semigroup.nonEmptyList(), ::identity)
sequence()

public fun <E, A> Iterable<ValidatedNel<E, A>>.sequence(): ValidatedNel<E, List<A>> =
traverse(Semigroup.nonEmptyList(), ::identity)

public inline fun <A, B> Iterable<A>.traverseOption(f: (A) -> Option<B>): Option<List<B>> {
@Deprecated("traverseOption is being renamed to traverse to simplify the Arrow API", ReplaceWith("traverse(f)", "arrow.core.traverse"))
public inline fun <A, B> Iterable<A>.traverseOption(f: (A) -> Option<B>): Option<List<B>> =
traverse(f)

@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <A, B> Iterable<A>.traverse(f: (A) -> Option<B>): Option<List<B>> {
val destination = ArrayList<B>(collectionSizeOrDefault(10))
for (item in this) {
when (val res = f(item)) {
Expand All @@ -347,10 +398,20 @@ public inline fun <A, B> Iterable<A>.traverseOption(f: (A) -> Option<B>): Option
return destination.some()
}

@Deprecated("sequenceOption is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence()", "arrow.core.sequence"))
public fun <A> Iterable<Option<A>>.sequenceOption(): Option<List<A>> =
this.traverseOption { it }
sequence()

public fun <A> Iterable<Option<A>>.sequence(): Option<List<A>> =
traverse(::identity)

public inline fun <A, B> Iterable<A>.traverseNullable(f: (A) -> B?): List<B>? {
@Deprecated("traverseNullable is being renamed to traverse to simplify the Arrow API", ReplaceWith("traverse(f)", "arrow.core.traverse"))
public inline fun <A, B> Iterable<A>.traverseNullable(f: (A) -> B?): List<B>? =
traverse(f)

@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <A, B> Iterable<A>.traverse(f: (A) -> B?): List<B>? {
val acc = mutableListOf<B>()
forEach { a ->
val res = f(a)
Expand All @@ -363,8 +424,12 @@ public inline fun <A, B> Iterable<A>.traverseNullable(f: (A) -> B?): List<B>? {
return acc.toList()
}

@Deprecated("sequenceNullable is being renamed to sequence to simplify the Arrow API", ReplaceWith("sequence()", "arrow.core.sequence"))
public fun <A> Iterable<A?>.sequenceNullable(): List<A>? =
this.traverseNullable { it }
sequence()

public fun <A> Iterable<A?>.sequence(): List<A>? =
traverse(::identity)

public fun <A> Iterable<A>.void(): List<Unit> =
map { }
Expand Down
Loading

0 comments on commit 3540e3d

Please sign in to comment.