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 Sequence sequenceX, traverseX and deprecate Sequence.some #2686

Merged
merged 11 commits into from
Mar 21, 2022
Prev Previous commit
Next Next commit
update Sequence
i-walker committed Mar 15, 2022

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit 9aea6ae0d8f13a6c34b93edb502357efdfd3d5dd
5 changes: 5 additions & 0 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
@@ -1411,12 +1411,17 @@ public final class arrow/core/SequenceKt {
public static final fun salign (Lkotlin/sequences/Sequence;Larrow/typeclasses/Semigroup;Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence;
public static final fun separateEither (Lkotlin/sequences/Sequence;)Lkotlin/Pair;
public static final fun separateValidated (Lkotlin/sequences/Sequence;)Lkotlin/Pair;
public static final fun sequence (Lkotlin/sequences/Sequence;)Larrow/core/Either;
public static final fun sequence (Lkotlin/sequences/Sequence;)Larrow/core/Option;
public static final fun sequenceEither (Lkotlin/sequences/Sequence;)Larrow/core/Either;
public static final fun sequenceOption (Lkotlin/sequences/Sequence;)Larrow/core/Option;
public static final fun sequenceValidated (Lkotlin/sequences/Sequence;Larrow/typeclasses/Semigroup;)Larrow/core/Validated;
public static final fun some (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence;
public static final fun split (Lkotlin/sequences/Sequence;)Lkotlin/Pair;
public static final fun tail (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence;
public static final fun traverse (Lkotlin/sequences/Sequence;Larrow/typeclasses/Semigroup;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
public static final fun traverse (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public static final fun traverse (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public static final fun traverseEither (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
public static final fun traverseOption (Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
public static final fun traverseValidated (Lkotlin/sequences/Sequence;Larrow/typeclasses/Semigroup;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
Original file line number Diff line number Diff line change
@@ -610,15 +610,20 @@ public fun <A, B> Sequence<Validated<A, B>>.separateValidated(): Pair<Sequence<A
return asep to bsep
}

@Deprecated("Terminal operators on Sequence are being deprecated. Please use either one of the supported terminal operators of Sequence and opt for supported [traverse] functions")
public fun <E, A> Sequence<Either<E, A>>.sequence(): Either<E, List<A>> =
traverse(::identity)

@Deprecated("use sequence", ReplaceWith("sequence().map { it.asSequence() }", "arrow.core.sequence"))
public fun <E, A> Sequence<Either<E, A>>.sequenceEither(): Either<E, Sequence<A>> =
traverseEither(::identity)
traverse(::identity).map { it.asSequence() }

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

@Deprecated("Terminal operators on Sequence are being deprecated. Please use either one of the supported terminal operators of Sequence and opt for supported [traverse] functions")
@Deprecated("use sequence", ReplaceWith("sequence().map { it.asSequence() }", "arrow.core.sequence"))
public fun <A> Sequence<Option<A>>.sequenceOption(): Option<Sequence<A>> =
traverseOption(::identity)
traverse(::identity).map { it.asSequence() }

@Deprecated("Terminal operators on Sequence are being deprecated. Please use either one of the supported terminal operators of Sequence and opt for supported [traverse] functions")
public fun <E, A> Sequence<Validated<E, A>>.sequenceValidated(semigroup: Semigroup<E>): Validated<E, Sequence<A>> =
traverseValidated(semigroup, ::identity)

@@ -650,52 +655,65 @@ public fun <A> Sequence<A>.split(): Pair<Sequence<A>, A>? =
public fun <A> Sequence<A>.tail(): Sequence<A> =
drop(1)

@Deprecated("Terminal operators on Sequence are being deprecated. Please use either one of the supported terminal operators of Sequence and opt for supported [traverse] functions")
public fun <E, A, B> Sequence<A>.traverseEither(f: (A) -> Either<E, B>): Either<E, Sequence<B>> {
public fun <E, A, B> Sequence<A>.traverse(f: (A) -> Either<E, B>): Either<E, List<B>> {
// Note: Using a mutable list here avoids the stackoverflows one can accidentally create when using
// Sequence.plus instead. But we don't convert the sequence to a list beforehand to avoid
// forcing too much of the sequence to be evaluated.
val acc = mutableListOf<B>()
forEach { a ->
when (val res = f(a)) {
is Right -> acc.add(res.value)
is Left -> return@traverseEither res
is Left -> return@traverse res
}
}
return acc.asSequence().right()
return acc.toList().right()
}

@Deprecated("Terminal operators on Sequence are being deprecated. Please use either one of the supported terminal operators of Sequence and opt for supported [traverse] functions")
public fun <A, B> Sequence<A>.traverseOption(f: (A) -> Option<B>): Option<Sequence<B>> {
@Deprecated("use traverse instead", ReplaceWith("traverse(f).map { it.asSequence() }", "arrow.core.traverse"))
public fun <E, A, B> Sequence<A>.traverseEither(f: (A) -> Either<E, B>): Either<E, Sequence<B>> =
traverse(f).map { it.asSequence() }

public fun <A, B> Sequence<A>.traverse(f: (A) -> Option<B>): Option<List<B>> {
// Note: Using a mutable list here avoids the stackoverflows one can accidentally create when using
// Sequence.plus instead. But we don't convert the sequence to a list beforehand to avoid
// forcing too much of the sequence to be evaluated.
val acc = mutableListOf<B>()
forEach { a ->
when (val res = f(a)) {
is Some -> acc.add(res.value)
is None -> return@traverseOption res
is None -> return@traverse res
}
}
return Some(acc.asSequence())
return Some(acc)
}

@Deprecated("Terminal operators on Sequence are being deprecated. Please use either one of the supported terminal operators of Sequence and opt for supported [traverse] functions")
public fun <E, A, B> Sequence<A>.traverseValidated(
@Deprecated("use traverse instead", ReplaceWith("traverse(f).map { it.asSequence() }", "arrow.core.traverse"))
public fun <A, B> Sequence<A>.traverseOption(f: (A) -> Option<B>): Option<Sequence<B>> =
traverse(f).map { it.asSequence() }

public fun <E, A, B> Sequence<A>.traverse(
semigroup: Semigroup<E>,
f: (A) -> Validated<E, B>
): Validated<E, Sequence<B>> = fold(mutableListOf<B>().valid() as Validated<E, MutableList<B>>) { acc, a ->
when (val res = f(a)) {
is Valid -> when (acc) {
is Valid -> acc.also { it.value.add(res.value) }
is Invalid -> acc
}
is Invalid -> when (acc) {
is Valid -> res
is Invalid -> semigroup.run { acc.value.combine(res.value).invalid() }
): Validated<E, List<B>> =
fold(mutableListOf<B>().valid() as Validated<E, MutableList<B>>) { acc, a ->
when (val res = f(a)) {
is Valid -> when (acc) {
is Valid -> acc.also { it.value.add(res.value) }
is Invalid -> acc
}
is Invalid -> when (acc) {
is Valid -> res
is Invalid -> semigroup.run { acc.value.combine(res.value).invalid() }
}
}
}
}.map { it.asSequence() }

@Deprecated("traverse(semigroup, f).map { it.asSequence() }", ReplaceWith("arrow.core.traverse"))
public fun <E, A, B> Sequence<A>.traverseValidated(
semigroup: Semigroup<E>,
f: (A) -> Validated<E, B>
): Validated<E, Sequence<B>> =
traverse(semigroup, f).map { it.asSequence() }

/**
* splits an union into its component parts.