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

backport traverse functions on NonEmptyList #54

Merged
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
4 changes: 4 additions & 0 deletions lib/api/lib.api
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ public final class app/cash/quiver/extensions/MapKt {

public final class app/cash/quiver/extensions/NonEmptyListKt {
public static final fun mapNotNone (Larrow/core/NonEmptyList;Lkotlin/jvm/functions/Function1;)Larrow/core/Option;
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 final class app/cash/quiver/extensions/Nullable {
Expand Down
42 changes: 41 additions & 1 deletion lib/src/main/kotlin/app/cash/quiver/extensions/NonEmptyList.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
package app.cash.quiver.extensions

import arrow.core.Either
import arrow.core.Nel
import arrow.core.NonEmptyList
import arrow.core.Option
import arrow.core.raise.either
import arrow.core.raise.option
import arrow.core.toNonEmptyListOrNull
import arrow.core.toOption
import kotlin.experimental.ExperimentalTypeInference

/**
* Applies a function to a NonEmptyList that can result in an optional NonEmptyList
* Applies a function that produces an Option to a NonEmptyList.
* The result is None if the resulting list would be empty, otherwise Some(NonEmptyList).
*/
inline fun <A, B> Nel<A>.mapNotNone(f: (A) -> Option<B>): Option<Nel<B>> =
this.toList().flatMap { a -> f(a).toList() }.toNonEmptyListOrNull().toOption()

/**
* Map a function that returns an Either across the NonEmptyList.
*
* The first Left result from calling the function will be the result, or if no calls result in a Left
* the result will be a Right(NonEmptyList) of all the B's returned.
*/
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
inline fun <E, A, B> Nel<A>.traverse(f: (A) -> Either<E, B>): Either<E, NonEmptyList<B>> =
let { nel -> either { nel.map { f(it).bind() } } }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is let required here because the either computation re-scopes this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, we need to give the NonEmptyList this a name.


/**
* Synonym for traverse((A)-> Either<E, B>): Either<E, NonEmptyList<B>>
*/
inline fun <E, A, B> Nel<A>.traverseEither(f: (A) -> Either<E, B>): Either<E, NonEmptyList<B>> =
traverse(f)

/**
* Map a function that returns an Option across the NonEmptyList.
*
* The first None result from calling the function will be the result, or if no calls result in a None
* the result will be a Some(NonEmptyList) of all the B's returned.
*/
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
inline fun <A, B> NonEmptyList<A>.traverse(f: (A) -> Option<B>): Option<NonEmptyList<B>> =
let { nel -> option { nel.map { f(it).bind() } } }

/**
* Synonym for traverse((A)-> Option<B>): Option<NonEmptyList<B>>
*/
inline fun <A, B> NonEmptyList<A>.traverseOption(f: (A) -> Option<B>): Option<NonEmptyList<B>> =
traverse(f)

Original file line number Diff line number Diff line change
@@ -1,19 +1,76 @@
package app.cash.quiver.extensions

import arrow.core.None
import arrow.core.Option
import arrow.core.left
import arrow.core.nel
import arrow.core.nonEmptyListOf
import arrow.core.right
import arrow.core.some
import io.kotest.assertions.arrow.core.shouldBeLeft
import io.kotest.assertions.arrow.core.shouldBeNone
import io.kotest.assertions.arrow.core.shouldBeRight
import io.kotest.assertions.arrow.core.shouldBeSome
import io.kotest.core.spec.style.StringSpec
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

class NonEmptyListTest : StringSpec({

"mapNotNone should map across the NonEmptyList" {
nonEmptyListOf(1, 2, 3).mapNotNone {
it.some()
}.shouldBeSome(nonEmptyListOf(1, 2, 3))
}

"mapNotNone should allow for suspended functions" {
suspend fun intToOption(i: Int): Option<Int> = suspendCoroutine { it.resume(i.some()) }
nonEmptyListOf(1, 2, 3).mapNotNone {
intToOption(it)
}.shouldBeSome(nonEmptyListOf(1, 2, 3))
}

"mapNotNone should skip None results" {
nonEmptyListOf(1, 2, 3).mapNotNone {
if (it % 2 == 0) {
None
} else {
it.some()
}
}.shouldBeSome(nonEmptyListOf(1, 3))
}

"mapNotNone should return None if all results are None" {
nonEmptyListOf(1, 2, 3).mapNotNone {
None
}.shouldBeNone()
}

"traverseEither maps across the NonEmptyList" {
nonEmptyListOf(1,2,3).traverseEither {
(it.toString()).right()
}.shouldBeRight(nonEmptyListOf("1", "2", "3"))
}

"traverseEither short-circuits on Left" {
nonEmptyListOf(1,2,3,4).traverseEither {
if (it % 2 == 0)
it.toString().left()
else it.right()
}.shouldBeLeft("2")
}

"traverseOption maps Option across the NonEmptyList" {
nonEmptyListOf(1,2,3).traverseOption {
(it.toString()).some()
}.shouldBeSome(nonEmptyListOf("1", "2", "3"))
}

"traverseOption short-circuits on None" {
nonEmptyListOf(1,2,3,4).traverseOption {
if (it % 2 == 0)
None
else it.some()
}.shouldBeNone()
}
})
Loading