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

Refactor - #2812 sequence separate performance #2818

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -593,23 +593,27 @@ public fun <A> Sequence<A>.salign(
* @receiver Iterable of Validated
* @return a tuple containing Sequence with [Either.Left] and another Sequence with its [Either.Right] values.
*/
public fun <A, B> Sequence<Either<A, B>>.separateEither(): Pair<Sequence<A>, Sequence<B>> {
val asep = flatMap { gab -> gab.fold({ sequenceOf(it) }, { emptySequence() }) }
val bsep = flatMap { gab -> gab.fold({ emptySequence() }, { sequenceOf(it) }) }
return asep to bsep
}
public fun <A, B> Sequence<Either<A, B>>.separateEither(): Pair<Sequence<A>, Sequence<B>> =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the return type should be Pair<List<A>, List<B>>. Because we evaluate this sequence and return sequence is not lazy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree with that, is it something that I can change in this PR or would it be done along with other breaking changes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hoc081098 that is a great point! We should probably post-pone that breaking change for Arrow 2.0. Shall we create an Arrow 2.0 ticket for the breaking change, and go ahead with these changes?

@Khepu the same improvement I commented on the Iterable.kt PR can probably be applied here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just looked into how Sequence methods are implemented in the STD. It will be a bit different so as to avoid realizing the entire sequence just to separate it. Most of the implementations included an internal class that implements a custom iterator object. I should be able to have a PoC tomorrow though!

fold(sequenceOf<A>() to sequenceOf<B>()) { (lefts, rights), either ->
when (either) {
is Left -> lefts + either.value to rights
is Right -> lefts to rights + either.value
}
}

/**
* Separate the inner [Validated] values into the [Validated.Invalid] and [Validated.Valid].
*
* @receiver Iterable of Validated
* @return a tuple containing Sequence with [Validated.Invalid] and another Sequence with its [Validated.Valid] values.
*/
public fun <A, B> Sequence<Validated<A, B>>.separateValidated(): Pair<Sequence<A>, Sequence<B>> {
val asep = flatMap { gab -> gab.fold({ sequenceOf(it) }, { emptySequence() }) }
val bsep = flatMap { gab -> gab.fold({ emptySequence() }, { sequenceOf(it) }) }
return asep to bsep
}
public fun <A, B> Sequence<Validated<A, B>>.separateValidated(): Pair<Sequence<A>, Sequence<B>> =
fold(sequenceOf<A>() to sequenceOf<B>()) { (invalids, valids), validated ->
when (validated) {
is Valid -> invalids to valids + validated.value
is Invalid -> invalids + validated.value to valids
}
}

public fun <E, A> Sequence<Either<E, A>>.sequence(): Either<E, List<A>> =
traverse(::identity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,31 @@ class SequenceKTest : UnitSpec() {
ints.asSequence().filterOption().toList() shouldBe ints.filterOption()
}
}

"separateEither" {
checkAll(Arb.sequence(Arb.int())) { ints ->
val sequence = ints.map {
if (it % 2 == 0) it.left()
else it.right()
}

val (lefts, rights) = sequence.separateEither()

lefts.toList() to rights.toList() shouldBe ints.partition { it % 2 == 0 }
}
}

"separateValidated" {
checkAll(Arb.sequence(Arb.int())) { ints ->
val sequence = ints.map {
if (it % 2 == 0) it.invalid()
else it.valid()
}

val (invalids, valids) = sequence.separateValidated()

invalids.toList() to valids.toList() shouldBe ints.partition { it % 2 == 0 }
}
}
}
}