Skip to content

Add EitherOps#leftMapOrKeep and EitherOps#leftFlatMapOrKeep#4638

Merged
danicheg merged 4 commits into
typelevel:mainfrom
danicheg:either-syntax
Jul 23, 2024
Merged

Add EitherOps#leftMapOrKeep and EitherOps#leftFlatMapOrKeep#4638
danicheg merged 4 commits into
typelevel:mainfrom
danicheg:either-syntax

Conversation

@danicheg

Copy link
Copy Markdown
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

@satorg satorg left a comment

Copy link
Copy Markdown
Contributor

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

@satorg satorg Jul 20, 2024

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

satorg commented Jul 20, 2024

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

satorg commented Jul 21, 2024

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Member Author

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

@satorg satorg left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Member Author

@satorg thanks!

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