-
Notifications
You must be signed in to change notification settings - Fork 7
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
aparkersquare
merged 1 commit into
main
from
aparker/20230905-backport-deprecated-traverse-functions
Sep 7, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 41 additions & 1 deletion
42
lib/src/main/kotlin/app/cash/quiver/extensions/NonEmptyList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } } } | ||
|
||
/** | ||
* 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) | ||
|
57 changes: 57 additions & 0 deletions
57
lib/src/test/kotlin/app/cash/quiver/extensions/NonEmptyListTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theeither
computation re-scopesthis
?There was a problem hiding this comment.
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.