Skip to content

Commit

Permalink
Add asRightIn, asLeftIn and asSomeIn for anyF (#460)
Browse files Browse the repository at this point in the history
* Add asRightIn, asLeftIn and asSomeIn for anyF

* Rename methods
  • Loading branch information
geirolz authored Feb 22, 2024
1 parent 84ebcc6 commit b0da564
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions shared/src/main/scala/mouse/anyf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ final class AnyFOps[F[_], A](private val fa: F[A]) extends AnyVal {
@inline def ||>[G[_]](f: F ~> G): G[A] = f(fa)
@inline def thrushK[G[_]](f: F ~> G): G[A] = f(fa)

def mapAsRight[L](implicit F: Functor[F]): F[Either[L, A]] =
Functor[F].map(fa)(Right(_))

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

def mapAsSome(implicit F: Functor[F]): F[Option[A]] =
Functor[F].map(fa)(Some(_))

def liftEitherT[E](implicit F: Functor[F]): EitherT[F, E, A] =
EitherT.right[E](fa)

Expand Down
12 changes: 12 additions & 0 deletions shared/src/test/scala/mouse/AnyFSyntaxTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ class AnyFSyntaxTest extends MouseSuite {
)
}

test("AnyFSyntax.asRightIn") {
assertEquals(List(1).mapAsRight[String], List(1.asRight))
}

test("AnyFSyntax.asLeftIn") {
assertEquals(List(1).mapAsLeft[String], List(1.asLeft))
}

test("AnyFSyntax.asSomeIn") {
assertEquals(List(1).mapAsSome, List(1.some))
}

test("AnyFSyntax.liftEitherT") {
assertEquals(List(1).liftEitherT[String], EitherT(List(1.asRight[String])))
}
Expand Down

0 comments on commit b0da564

Please sign in to comment.