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

CU-ev2g8f Fix Sequence#travserse, Option#apEval & Either#apEval #2260

Merged
merged 6 commits into from
Feb 26, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ fun <A, B, C> EitherOf<A, B>.ap(ff: EitherOf<A, (B) -> C>): Either<A, C> =
flatMap { a -> ff.fix().map { f -> f(a) } }

fun <A, B, C> Either<A, B>.apEval(ff: Eval<Either<A, (B) -> C>>): Eval<Either<A, C>> =
ff.map { this.ap(it) }
fold({ l -> Eval.now(l.left()) }, { r -> ff.map { it.map { f -> f(r) } } })
Copy link
Member

Choose a reason for hiding this comment

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

is there or should be a specific test for this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

We should add many tests.. 😭😅 This code was taken from the EitherApply.kt code, and is verified in TraverseTest. By reverting this change it makes TraverseTest fail.

Copy link
Member

Choose a reason for hiding this comment

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

By reverting this change it makes TraverseTest fail.

That's what I meant, perfect then! 👌


fun <A, B> EitherOf<A, B>.combineK(y: EitherOf<A, B>): Either<A, B> =
when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ sealed class Option<out A> : OptionOf<A> {
ff.fix().flatMap { this.fix().map(it) }

fun <B> apEval(ff: Eval<Option<(A) -> B>>): Eval<Option<B>> =
ff.map { ap(it) }
fold({ Eval.now(none()) }, { r -> ff.map { it.map { f -> f(r) } } })

inline fun <B> crosswalk(f: (A) -> Option<B>): Option<Option<B>> =
when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ fun <E, A> Sequence<Validated<E, Sequence<A>>>.flatSequenceValidated(semigroup:
flatTraverseValidated(semigroup, ::identity)

fun <E, A, B> Sequence<A>.flatTraverseEither(f: (A) -> Either<E, Sequence<B>>): Either<E, Sequence<B>> =
foldRight<A, Either<E, Sequence<B>>>(emptySequence<B>().right()) { a, acc ->
f(a).ap(acc.map { bs -> { b: Sequence<B> -> b + bs } })
}
foldRight<A, Either<E, Sequence<B>>>(Eval.now(emptySequence<B>().right())) { a, acc ->
f(a).apEval(acc.map { it.map { bs -> { b: Sequence<B> -> b + bs } } })
}.value()

fun <E, A, B> Sequence<A>.flatTraverseValidated(semigroup: Semigroup<E>, f: (A) -> Validated<E, Sequence<B>>): Validated<E, Sequence<B>> =
foldRight<A, Validated<E, Sequence<B>>>(emptySequence<B>().valid()) { a, acc ->
f(a).ap(semigroup, acc.map { bs -> { b: Sequence<B> -> b + bs } })
}
foldRight<A, Validated<E, Sequence<B>>>(Eval.now(emptySequence<B>().valid())) { a, acc ->
f(a).apEval(semigroup, acc.map { it.map { bs -> { b: Sequence<B> -> b + bs } } })
}.value()

fun <A> Sequence<Sequence<A>>.flatten(): Sequence<A> =
flatMap(::identity)
Expand All @@ -133,9 +133,6 @@ fun <A, B> Sequence<A>.foldMap(MB: Monoid<B>, f: (A) -> B): B = MB.run {
}
}

inline fun <A, B> Sequence<A>.foldRight(initial: B, operation: (A, B) -> B): B =
toList().foldRight(initial, operation)

fun <A, B> Sequence<A>.foldRight(lb: Eval<B>, f: (A, Eval<B>) -> Eval<B>): Eval<B> {
fun Iterator<A>.loop(): Eval<B> =
if (hasNext()) f(next(), Eval.defer { loop() }) else lb
Expand Down Expand Up @@ -469,26 +466,26 @@ fun <A> Sequence<A>.tail(): Sequence<A> =
drop(1)

fun <E, A, B> Sequence<A>.traverseEither(f: (A) -> Either<E, B>): Either<E, Sequence<B>> =
foldRight<A, Either<E, Sequence<B>>>(emptySequence<B>().right()) { a, acc ->
f(a).ap(acc.map { bs -> { b: B -> sequenceOf(b) + bs } })
}
foldRight<A, Either<E, Sequence<B>>>(Eval.now(sequenceOf<B>().right())) { a, acc ->
f(a).apEval(acc.map { it.map { bs -> { b: B -> sequenceOf(b) + bs } } })
}.value()

fun <E, A> Sequence<A>.traverseEither_(f: (A) -> Either<E, *>): Either<E, Unit> {
val void = { _: Unit -> { _: Any? -> Unit } }
return foldRight<A, Either<E, Unit>>(Unit.right()) { a, acc ->
f(a).ap(acc.map(void))
}
val void: (Either<E, Unit>) -> Either<E, (Any?) -> Unit> = { it.map { { Unit } } }
return foldRight<A, Either<E, Unit>>(Eval.now(Unit.right())) { a, acc ->
f(a).apEval(acc.map(void))
}.value()
}

fun <E, A, B> Sequence<A>.traverseValidated(semigroup: Semigroup<E>, f: (A) -> Validated<E, B>): Validated<E, Sequence<B>> =
foldRight<A, Validated<E, Sequence<B>>>(emptySequence<B>().valid()) { a, acc ->
f(a).ap(semigroup, acc.map { bs -> { b: B -> sequenceOf(b) + bs } })
}
foldRight<A, Validated<E, Sequence<B>>>(Eval.now(emptySequence<B>().valid())) { a, acc ->
f(a).apEval(semigroup, acc.map { it.map { bs -> { b: B -> sequenceOf(b) + bs } } })
}.value()

fun <E, A> Sequence<A>.traverseValidated_(semigroup: Semigroup<E>, f: (A) -> Validated<E, *>): Validated<E, Unit> =
foldRight<A, Validated<E, Unit>>(Unit.valid()) { a, acc ->
f(a).ap(semigroup, acc.map { { Unit } })
}
foldRight<A, Validated<E, Unit>>(Eval.now(Unit.valid())) { a, acc ->
f(a).apEval(semigroup, acc.map { it.map { { Unit } } })
}.value()

/**
* Pairs [B] with [A] returning a Sequence<Pair<B, A>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1108,15 +1108,13 @@ internal fun <K, V> MutableMap<in K, in V>.putAll(tuples: Sequence<Tuple2<K, V>>
operator fun <K, V> Map<out K, V>.plus(tuple: Tuple2<K, V>): Map<K, V> =
if (this.isEmpty()) mapOf(tuple) else LinkedHashMap(this).apply { put(tuple.a, tuple.b) }


@Deprecated(
"Tuple2 is deprecated in favor of Kotlin's Pair",
ReplaceWith("this.plus(tuples.map { (a, b) -> Pair(a, b) })")
)
operator fun <K, V> Map<out K, V>.plus(tuples: Iterable<Tuple2<K, V>>): Map<K, V> =
if (this.isEmpty()) tuples.toMap() else LinkedHashMap(this).apply { putAll(tuples) }


@Deprecated(
"Tuple2 is deprecated in favor of Kotlin's Pair",
ReplaceWith("this.plus(tuples.map { (a, b) -> Pair(a, b) })")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,9 @@ inline fun <E, A, B> ValidatedOf<E, A>.ap(SE: Semigroup<E>, f: Validated<E, (A)
}
}

fun <E, A, B> Validated<E, A>.apEval(SE: Semigroup<E>, ff: Eval<Validated<E, (A) -> B>>): Eval<Validated<E, B>> =
ff.map { this.ap(SE, it) }

@Deprecated(
"To keep API consistent with Either and Option please use `handleErrorWith` instead",
ReplaceWith("handleErrorWith(f)")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package arrow.typeclasses

import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import arrow.core.extensions.option.applicative.applicative
import arrow.core.extensions.sequence.traverse.sequence
import arrow.core.Left
import arrow.core.Right
import arrow.core.sequenceEither
import io.kotlintest.shouldBe
import io.kotlintest.specs.StringSpec

class TraverseTest : StringSpec({
"traverse is stacksafe over very long collections and short circuits properly" {
// This has to traverse 50k elements till it reaches None and terminates
generateSequence(0) { it + 1 }.map { if (it < 50_000) Some(it) else None }
.sequence(Option.applicative()) shouldBe None
generateSequence(0) { it + 1 }.map { if (it < 50_000) Right(it) else Left(Unit) }
.sequenceEither() shouldBe Left(Unit)
}
})