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 asRightIn, asLeftIn and asSomeIn for anyF #460

Merged
merged 2 commits into from
Feb 22, 2024

Conversation

geirolz
Copy link
Contributor

@geirolz geirolz commented Oct 9, 2023

Hi guys! This PR adds some methods to map an F[A] into F[Either[L, A]], F[Either[A, R]] or F[Option[A]].

Example

val fa: IO[Int] = IO.pure(1)

val faRight: IO[Either[String, Int]] = fa.asRightIn[String]
val faLeft: IO[Either[Int, String]] = fa.asLeftIn[String]
val faOption: IO[Option[Int]] = fa.asSomeIn

And for the naming, here a recap

val a: Int = 1 
val fa: IO[Int] = IO.pure(a)

val right: Either[String, Int] = a.asRight[String]
val faRightFromA: IO[Either[String, Int]] = a.asRightF[IO, String]
val faRightFromFA: IO[Either[String, Int]] = fa.mapAsRight[String]

What do you think ? Is there already something in place that I didn't found ? If not, do you think the name is coherent ?

@benhutchison
Copy link
Member

Similar questions about how much use these would see as here

Comment on lines 37 to 43
def asRightIn[L](implicit F: Functor[F]): F[Either[L, A]] =
Functor[F].map(fa)(Right(_))

def asLeftIn[R](implicit F: Functor[F]): F[Either[A, R]] =
Functor[F].map(fa)(Left(_))

def asSomeIn(implicit F: Functor[F]): F[Option[A]] =
Copy link
Member

Choose a reason for hiding this comment

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

I was the one who added this In postfix in the naming within FEither / FOption syntaxes, and I don't think it should be reused any-elsewhere. In the context of the mentioned syntaxes, it emphasizes the intention of applying the provided function to the value of the inner context (Either / Option). But seeing that postfix is used that way is a little bit confusing.

Copy link
Contributor Author

@geirolz geirolz Oct 13, 2023

Choose a reason for hiding this comment

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

Thinking more on this I agree with you. I still feeling that we need these methods to "map and wrap" an effect F.

What about something like mapAsRight, mapAsLeft and mapAsSome.
At the end they are something like this

IO.pure(1).map(_.asRight[String])// IO[Either[String, Int]]

// with the new operator
IO.pure(1).mapAsRight[String]// IO[Either[String, Int]]

I don't know, I don't have many ideas at the moment

Choose a reason for hiding this comment

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

Hi! I was just checking issues/PRs before proposing exactly the same thing, with the originally proposed names: asRightIn/asLeftIn/asSomeIn.

Maybe you'll find my thoughts on the naming helpful. To me, these operations are performed on the value in the context of an F, which is quite similar to mapIn, flatMapIn. Somehow, asRightIn/asLeftIn/asSomeIn felt natural to me and I actually was a bit surprised to find out they’re missing.

So, any plans of settling on the naming and merging the PR? 😉

Copy link
Member

Choose a reason for hiding this comment

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

OK. Lets get this merged, it's sat long enough, and a 2nd user wanting same thing is evidence for usefulness.

👍 from me, are you ok to merge in current state @danicheg ?

@danicheg
Copy link
Member

@benhutchison it's good to go!

@benhutchison benhutchison merged commit b0da564 into typelevel:main Feb 22, 2024
13 checks passed
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