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 doubleFlatTap,asIn,asF into EitherFOps #1250

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions modules/core/ce2/src/test/scala/tofu/syntax/FEitherSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,40 @@ class FEitherSuite extends AnyWordSpec with Matchers {
}
}

"EitherFOps#asIn" should {

"return valid value in case of Right" in {
defaultRight.asIn(1) mustBe Some(Right(1))
}

"return Left value without changes" in {
defaultLeft.asIn(1) mustBe Some(Left(4))
}

"return valid value in case of base None" in {
none[Either[String, Int]].asIn(1) mustBe None
}
}

"EitherFOps#asF" should {

"return valid value in case of Right" in {
defaultRight.asF(Some(1)) mustBe Some(Right(1))
}

"return None if inner value is None" in {
defaultRight.asF(None) mustBe None
}

"return Left value without changes" in {
defaultLeft.asF(Some(1)) mustBe Some(Left(4))
}

"return valid value in case of base None" in {
none[Either[String, Int]].asIn(Some(1)) mustBe None
}
}

"EitherFOps#leftMapF" should {

"return valid value in case of Left" in {
Expand Down Expand Up @@ -406,6 +440,26 @@ class FEitherSuite extends AnyWordSpec with Matchers {
}
}

"EitherFOps#doubleFlatTap" should {

"return None" in {
none[Either[String, Int]].doubleFlatTap(_ => None) mustBe None
defaultRight.doubleFlatTap(_ => None) mustBe None
}

"return Right without changes" in {
defaultRight.doubleFlatTap(i => Some(Right(i + 1))) mustBe defaultRight
}

"return Left" in {
defaultRight.doubleFlatTap(i => Some(Left(i.toString))) mustBe Some(Left("4"))

defaultLeft.doubleFlatTap(i => Some(Left(i.length))) mustBe Some(Left(4))
defaultLeft.doubleFlatTap(i => Some(Right(i.length))) mustBe Some(Left(4))
defaultLeft.doubleFlatTap(_ => None) mustBe Some(Left(4))
}
}

"EitherFOps#foldIn" should {
"map left or right to C" in {
defaultRight.foldIn(_.length, c => c * c) mustBe Some(16)
Expand Down
9 changes: 9 additions & 0 deletions modules/kernel/src/main/scala/tofu/syntax/feither.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ object feither {
def mapIn[B](f: R => B)(implicit F: Functor[F]): F[Either[L, B]] =
e.map(_.map(f))

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

def asF[B](b: F[B])(implicit F: Monad[F]): F[Either[L, B]] =
mapF(_ => b)

def leftMapF[L1](f: L => F[L1])(implicit F: Monad[F]): F[Either[L1, R]] = {
e.flatMap {
case Left(left) => f(left).map(_.asLeft)
Expand Down Expand Up @@ -104,6 +110,9 @@ object feither {
e.flatMap(_.wideLeft[L1].flatTraverse(f))
}

def doubleFlatTap[L1 >: L, R1](f: R => F[Either[L1, R1]])(implicit F: Monad[F]): F[Either[L1, R]] =
e.doubleFlatMap(r => f(r).asIn(r))

def foldIn[C](lMap: L => C, rMap: R => C)(implicit F: Functor[F]): F[C] = {
e.map(_.fold(lMap, rMap))
}
Expand Down
Loading