Skip to content

Commit

Permalink
Add asIn, voidIn, leftAsIn, leftVoidIn into foption and feither.
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz committed Oct 9, 2023
1 parent 4d464ff commit d1b4589
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
12 changes: 12 additions & 0 deletions shared/src/main/scala/mouse/feither.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ final class FEitherOps[F[_], L, R](private val felr: F[Either[L, R]]) extends An
case r @ Right(_) => r.asInstanceOf[Right[A, R]]
}

def leftAsIn[B](b: => B)(implicit F: Functor[F]): F[Either[B, R]] =
leftMapIn(_ => b)

def leftVoidIn(implicit F: Functor[F]): F[Either[Unit, R]] =
leftAsIn(())

def leftWidenIn[A >: L](implicit F: Functor[F]): F[Either[A, R]] =
F.map(felr) {
case l @ Left(_) => l.asInstanceOf[Left[A, R]]
Expand All @@ -111,6 +117,12 @@ final class FEitherOps[F[_], L, R](private val felr: F[Either[L, R]]) extends An
def mapIn[A](f: R => A)(implicit F: Functor[F]): F[Either[L, A]] =
F.map(felr)(_.map(f))

def asIn[B](b: => B)(implicit F: Functor[F]): F[Either[L, B]] =
mapIn(_ => b)

def voidIn(implicit F: Functor[F]): F[Either[L, Unit]] =
asIn(())

def bimapIn[A, B](left: L => A, right: R => B)(implicit F: Functor[F]): F[Either[A, B]] =
F.map(felr) {
case Left(value) => Left(left(value))
Expand Down
6 changes: 6 additions & 0 deletions shared/src/main/scala/mouse/foption.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ final class FOptionOps[F[_], A](private val foa: F[Option[A]]) extends AnyVal {
def mapIn[B](f: A => B)(implicit F: Functor[F]): F[Option[B]] =
F.map(foa)(_.map(f))

def asIn[B](b: => B)(implicit F: Functor[F]): F[Option[B]] =
mapIn(_ => b)

def voidIn(implicit F: Functor[F]): F[Option[Unit]] =
asIn(())

def orElseIn(default: Option[A])(implicit F: Functor[F]): F[Option[A]] =
F.map(foa) {
case None => default
Expand Down
24 changes: 22 additions & 2 deletions shared/src/test/scala/mouse/FEitherSyntaxTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import cats.syntax.either._
import scala.util.{Failure, Success, Try}

class FEitherSyntaxTest extends MouseSuite {
private val rightValue = List(42.asRight[String])
private val leftValue = List("42".asLeft[Int])
private val rightValue: Seq[Either[String, Int]] = List(42.asRight[String])
private val leftValue: Seq[Either[String, Int]] = List("42".asLeft[Int])

test("FEitherSyntax.cata") {
assertEquals(rightValue.cata(_ => 0, _ => 1), List(1))
Expand Down Expand Up @@ -108,6 +108,16 @@ class FEitherSyntaxTest extends MouseSuite {
assertEquals(leftValue.leftMapIn(_ => ""), List("".asLeft[Int]))
}

test("FEitherSyntax.leftAsIn") {
assertEquals(rightValue.leftAsIn(""), rightValue)
assertEquals(leftValue.leftAsIn(""), List("".asLeft[Int]))
}

test("FEitherSyntax.leftVoidIn") {
assertEquals(rightValue.leftVoidIn, rightValue.leftMapIn(_ => ()))
assertEquals(leftValue.leftVoidIn, List(().asLeft[Int]))
}

test("FEitherSyntax.leftTraverseIn") {
assertEquals(rightValue.leftTraverseIn(_ => Option("")), List(Option(42.asRight[String])))
assertEquals(leftValue.leftTraverseIn(_ => Option("")), List(Option("".asLeft[Int])))
Expand All @@ -123,6 +133,16 @@ class FEitherSyntaxTest extends MouseSuite {
assertEquals(leftValue.mapIn(_ * 2), leftValue)
}

test("FEitherSyntax.asIn") {
assertEquals(rightValue.asIn(2), List(2.asRight[String]))
assertEquals(leftValue.asIn(2), leftValue)
}

test("FEitherSyntax.voidIn") {
assertEquals(rightValue.voidIn, List(().asRight[String]))
assertEquals(leftValue.voidIn, leftValue.mapIn(_ => ()))
}

test("FEitherSyntax.orElseIn") {
assertEquals(rightValue.orElseIn(0.asRight[String]), rightValue)
assertEquals(leftValue.orElseIn(0.asRight[String]), List(0.asRight[String]))
Expand Down
10 changes: 10 additions & 0 deletions shared/src/test/scala/mouse/FOptionSyntaxTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ class FOptionSyntaxTest extends MouseSuite {
assertEquals(List(Option.empty[Int]).mapIn(_ + 1), List(Option.empty[Int]))
}

test("FOptionSyntax.asIn") {
assertEquals(List(Option(1)).asIn(1), List(Option(1)))
assertEquals(List(Option.empty[Int]).asIn(1), List(Option.empty[Int]))
}

test("FOptionSyntax.voidIn") {
assertEquals(List(Option(1)).voidIn, List(Option(())))
assertEquals(List(Option.empty[Int]).voidIn, List(Option.empty[Unit]))
}

test("FOptionSyntax.orElseIn") {
assertEquals(List(Option(1)).orElseIn(Option(0)), List(Option(1)))
assertEquals(List(Option.empty[Int]).orElseIn(Option(0)), List(Option(0)))
Expand Down

0 comments on commit d1b4589

Please sign in to comment.