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

Conversation

aparkersquare
Copy link
Collaborator

@aparkersquare aparkersquare commented Sep 6, 2023

This PR adds extension functions for traverse to NonEmptyList that are deprecated in Arrow.

traverseEither and traverse

  • Apply a function that returns an Either across the contents of the NonEmptyList
  • NonEmptyList<A>.traverse(f: (A) -> Either<E, B>): Either<E, NonEmptyList<B>

traverseOption and traverse

  • Apply a function that returns an Option across the contents of the NonEmptyList
  • NonEmptyList<A>.traverse(f: (A) -> Option<B>): Option<NonEmptyList<B>>

@aparkersquare aparkersquare changed the title backport traverse functions that arrow deprecated backport traverse functions on NonEmptyList Sep 7, 2023
@aparkersquare aparkersquare force-pushed the aparker/20230905-backport-deprecated-traverse-functions branch from 84c394b to 0469b9f Compare September 7, 2023 06:16
@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.

Copy link
Collaborator

@hugomd hugomd left a comment

Choose a reason for hiding this comment

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

Very nice!

@aparkersquare aparkersquare force-pushed the aparker/20230905-backport-deprecated-traverse-functions branch from 0469b9f to de17cff Compare September 7, 2023 07:54
@aparkersquare aparkersquare merged commit e728c39 into main Sep 7, 2023
2 checks passed
@aparkersquare aparkersquare deleted the aparker/20230905-backport-deprecated-traverse-functions branch September 7, 2023 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants