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 EitherOps#leftMapOrKeep and EitherOps#leftFlatMapOrKeep #4638

Merged
merged 4 commits into from
Jul 23, 2024

Conversation

danicheg
Copy link
Member

These are companions to Functor#mapOrKeep and cats.Monad#flatMapOrKeep, but for the left-hand side of Either[A, B].

satorg
satorg previously approved these changes Jul 20, 2024
Copy link
Contributor

@satorg satorg left a comment

Choose a reason for hiding this comment

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

Thank you!

def leftMapOrKeep[AA >: A](pf: PartialFunction[A, AA]): Either[AA, B] =
eab match {
case Left(a) => Left(pf.applyOrElse(a, identity[AA]))
case r @ Right(_) => r
Copy link
Contributor

@satorg satorg Jul 20, 2024

Choose a reason for hiding this comment

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

I wonder if on a byte-code level this could be more performant:

case r : Right[B] => r

because there's no extractor to be called nor intermediate Option value to be created in opposite to the former case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Interestingly, I never thought that x @ Foo(_) might have any significant distinction from x: Foo.

@satorg
Copy link
Contributor

satorg commented Jul 20, 2024

It totally makes sense, thank you!

I've realized - perhaps it makes sense to add leftMapOrKeep to cats.Bifunctor. That way we could get it for many other bi-parameter types, not just Either.

Things seem more complicated for leftFlatMapOrKeep though. Looks like there's no appropriate type class where leftFlatMapOrKeep could be added.

In fact, there's Bimonad in Cats, but it has a completely different meaning there, which I find quite confusing.

@danicheg
Copy link
Member Author

I've realized - perhaps it makes sense to add leftMapOrKeep to cats.Bifunctor. That way we could get it for many other bi-parameter types, not just Either.

Agreed. Are you arguing we should do it as a drop-in replacement instead of adding EitherOps#leftMapOrKeep? Apart from the existence of the Bifunctor[Either] instance, there are also EitherOps#leftMap and its companion EitherOps#leftFlatMap.

@satorg
Copy link
Contributor

satorg commented Jul 21, 2024

I think it wouldn't hurt if there were leftMapOrKeep in both Either syntax and Bifunctor, similar to leftMap.
Moreover, calling to leftMap through Bifunctor on Either incurs quite a bit of overhead currently. It could be alleviated a little if we had a specialized version of leftMap in the Bifunctor[Either] implementation. But for now it is not there and it is a completely different story.

Also I feel this PR would be better off if it stays focused on the Either syntax only. Bifunctor could be addressed in a separate work.

@danicheg
Copy link
Member Author

@satorg wouldn't you mind submitting yet another review?

Copy link
Contributor

@satorg satorg left a comment

Choose a reason for hiding this comment

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

Thank you!

@danicheg
Copy link
Member Author

@satorg thanks!

@danicheg danicheg merged commit aee3702 into typelevel:main Jul 23, 2024
16 checks passed
@danicheg danicheg deleted the either-syntax branch July 23, 2024 05:20
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