Skip to content

Commit

Permalink
Add right family methods to ZPure (#375) (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
atrianac authored Nov 26, 2020
1 parent 17ec7a0 commit c817ec9
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
39 changes: 39 additions & 0 deletions core/shared/src/main/scala/zio/prelude/fx/ZPure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,45 @@ sealed trait ZPure[-S1, +S2, -R, +E, +A] { self =>
that: ZPure[S2, S3, R1, E1, B]
)(f: (A, B) => C): ZPure[S1, S3, R1, E1, C] =
self.flatMap(a => that.flatMap(b => State.succeed(f(a, b))))

/**
* Returns a successful computation if the value is `Right`, or fails with error `None`.
*/
final def right[B, C](implicit ev: A <:< Either[B, C]): ZPure[S1, S2, R, Option[E], C] =
self.foldM(
e => ZPure.fail(Some(e)),
a => ev(a).fold(_ => ZPure.fail(None), ZPure.succeed)
)

/*
Returns a successful computation if the value is `Right`, or fails with error `e`.
*/
final def rightOrFail[B, C, E1 >: E](e: => E1)(implicit ev: A <:< Either[B, C]): ZPure[S1, S2, R, E1, C] =
self.flatMap(ev(_) match {
case Right(value) => ZPure.succeed(value)
case Left(_) => ZPure.fail(e)
})

/*
Returns a successful computation if the value is `Right`, or fails with error function `e`.
*/
final def rightOrFailWith[B, C, E1 >: E](e: B => E1)(implicit ev: A <:< Either[B, C]): ZPure[S1, S2, R, E1, C] =
self.flatMap(ev(_) match {
case Right(value) => ZPure.succeed(value)
case Left(err) => ZPure.fail(e(err))
})

/*
Returns a successful computation if the value is `Right`, or fails with a [[java.util.NoSuchElementException]].
*/
final def rightOrFailWithException[B, C, E1 >: NoSuchElementException](implicit
ev: A <:< Either[B, C],
ev2: E <:< E1
): ZPure[S1, S2, R, E1, C] =
self.foldM(
e => ZPure.fail(ev2(e)),
a => ev(a).fold(_ => ZPure.fail(new NoSuchElementException("Either.right.get on Left")), ZPure.succeed)
)
}

object ZPure {
Expand Down
62 changes: 62 additions & 0 deletions core/shared/src/test/scala/zio/prelude/fx/ZPureSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,68 @@ object ZPureSpec extends DefaultRunnableSpec {
}
)
),
suite("right methods")(
suite("right")(
test("failure") {
val result = ZPure.fail("fail").right
assert(result.runEither(0))(isLeft(isSome(equalTo("fail"))))
},
test("right") {
val result = ZPure.succeed[Int, Either[Nothing, String]](Right("Right")).right
assert(result.runEither(0))(isRight(equalTo((0, "Right"))))
},
test("left") {
val result = ZPure.succeed[Int, Either[Int, Nothing]](Left(1)).right
assert(result.runEither(0))(isLeft(isNone))
}
),
suite("rightOrFail")(
test("failure") {
val result = ZPure.fail("fail").rightOrFail("oh crap")
assert(result.runEither(0))(isLeft(equalTo("fail")))
},
test("right") {
val result = ZPure
.succeed[Int, Either[Nothing, Int]](Right(1))
.rightOrFail("oh crap")
assert(result.runEither(0))(isRight(equalTo((0, 1))))
},
test("left") {
val result = ZPure.succeed[Int, Either[String, Int]](Left("Left")).rightOrFail("oh crap")
assert(result.runEither(0))(isLeft(equalTo("oh crap")))
}
),
suite("rightOrFailWith")(
test("failure") {
val result = ZPure.fail("fail").rightOrFailWith[Any, Any, String](_ => "Oh crap")
assert(result.runEither(0))(isLeft(equalTo("fail")))
},
test("right") {
val result = ZPure
.succeed[Int, Either[Nothing, Int]](Right(1))
.rightOrFailWith[Any, Int, String](_ => "oh crap")
assert(result.runEither(0))(isRight(equalTo((0, 1))))
},
test("left") {
val result = ZPure.succeed[Int, Either[String, Int]](Left("Left")).rightOrFail("oh crap")
assert(result.runEither(0))(isLeft(equalTo("oh crap")))
}
),
suite("rightOrFailWithException")(
test("failure") {
val result = ZPure.fail(new NoSuchElementException()).rightOrFailWithException
assert(result.runEither(0))(isLeft(isSubtype[NoSuchElementException](anything)))
},
test("right") {
val result = ZPure.succeed[Int, Either[Nothing, Int]](Right(1)).rightOrFailWithException
assert(result.runEither(0))(isRight(equalTo((0, 1))))
},
test("left") {
val result = ZPure.succeed[Int, Either[String, Int]](Left("Left")).rightOrFailWithException
assert(result.runEither(0))(isLeft(isSubtype[NoSuchElementException](anything)))
}
)
),
suite("some")(
testM("success (Some)") {
check(genInt, genInt, genInt) { (s1, s2, a) =>
Expand Down

0 comments on commit c817ec9

Please sign in to comment.