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

Add traverse, zip and orEmpty to Option #53

Closed
wants to merge 1 commit into from

Conversation

millyrowboat
Copy link
Collaborator

This PR adds three new extension methods to Option:

  • traverseOp
    • traverse is currently shadowed in Arrow, so I had to rename it
    • It is a function that allows you to iterate over an Monad Option with in the context of an Either, and turn your Option<A> into an Either<None, Option<B>>
  • zipOp
    • We were umming and ahhing about whether to include this in Quiver. I'm not attached to it since you can achieve similar results with just map
    • This is a function that will combine your Option<A> with a Option<B> and return an Option<C>
  • orEmpty is a convenience function that we've been using in our codebases, that will return an empty string if your Option is None
    • Wondering now if that should only be allowed to be called on a Option<String> context. Thoughts?

@millyrowboat millyrowboat force-pushed the milly/20230904-make-option-better branch 2 times, most recently from f14708e to c74d05e Compare September 6, 2023 05:06
@millyrowboat millyrowboat force-pushed the milly/20230904-make-option-better branch from c74d05e to 71ae47f Compare September 6, 2023 05:16
5.some().zipOp("apple".some()) { a: Int, b: String ->
b.length * a
} shouldBeSome 25
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}
}

Copy link
Collaborator

@aparkersquare aparkersquare Sep 7, 2023

Choose a reason for hiding this comment

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

I think the indent on line 46 is actually correct (2).

But the following two tests have incorrect indent (4).

inline fun <E, A, B> Option<A>.traverseOp(fa: (A) -> Either<E, B>): Either<E, Option<B>> = fold(
{ None.right() },
{ fa(it).map(::Some) }
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's unfortunate we have to use traverseOp and zipOp, that really reduces the value of back-porting these.

Copy link
Collaborator

@aparkersquare aparkersquare Sep 7, 2023

Choose a reason for hiding this comment

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

traverseEither and zipOption would be more consistent with the variants on NonEmptyList (which have recently been back-ported to quiver).

We could then add traverse and zip when arrow finally removes them?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like using straight traverse and zip is worth the wait. Let's lean on Arrow to fully remove them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed. I'll open a new PR with just orEmpty then if you're happy with it

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.

4 participants