Skip to content

Commit

Permalink
Fixing *semi*.
Browse files Browse the repository at this point in the history
  • Loading branch information
luksow committed Dec 6, 2023
1 parent 9ca8931 commit a9627fc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/main/scala/pl/iterators/sealedmonad/Sealed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ sealed trait Sealed[F[_], +A, +ADT] {
*/
final def semiflatMap[B](f: A => F[B]): Sealed[F, B, ADT] = flatMap(a => Sealed.IntermediateF(Eval.later(f(a))))

final def leftSemiflatMap[ADT1](f: ADT => F[ADT1]): Sealed[F, A, ADT1] =
final def leftSemiflatMap[ADT1 >: ADT](f: ADT => F[ADT1]): Sealed[F, A, ADT1] =
foldM[A, ADT]((adt: ADT) => ResultF(Eval.later(f(adt))).asInstanceOf[Sealed[F, A, ADT]], a => Intermediate(a))
.asInstanceOf[Sealed[F, A, ADT1]]

/** Executes a side effect if ADT has been reached, and returns unchanged `Sealed[F, A, ADT]`.
*
Expand All @@ -53,13 +52,13 @@ sealed trait Sealed[F[_], +A, +ADT] {
*/
final def leftSemiflatTap[C](f: ADT => F[C]): Sealed[F, A, ADT] =
foldM[A, ADT](
(adt: ADT) => ResultF(Eval.later(f(adt))).flatMap(_ => Result(adt)).asInstanceOf[Sealed[F, A, ADT]],
(adt: ADT) => IntermediateF(Eval.later(f(adt))).flatMap(_ => Result(adt)),
a => Intermediate(a)
)

/** Combine leftSemiflatMap and semiflatMap together.
*/
final def biSemiflatMap[B, ADT1](fa: ADT => F[ADT1], fb: A => F[B]): Sealed[F, B, ADT1] =
final def biSemiflatMap[B, ADT1 >: ADT](fa: ADT => F[ADT1], fb: A => F[B]): Sealed[F, B, ADT1] =
leftSemiflatMap(fa).semiflatMap(fb)

/** Executes appropriate side effect depending on whether `A` or `ADT` has been reached, and returns unchanged `Sealed[F, A, ADT]`.
Expand Down Expand Up @@ -192,8 +191,8 @@ sealed trait Sealed[F[_], +A, +ADT] {
* res1: cats.Id[Response] = Reached
* }}}
*/
final def foldM[B, ADT1 >: ADT](left: ADT1 => Sealed[F, B, ADT1], right: A => Sealed[F, B, ADT1]): Sealed[F, B, ADT1] =
Fold(this, right, left)
final def foldM[B, ADT1 >: ADT](left: ADT => Sealed[F, B, ADT1], right: A => Sealed[F, B, ADT1]): Sealed[F, B, ADT1] =
Fold(this, right, left.asInstanceOf[ADT1 => Sealed[F, B, ADT1]])

/** Converts `A` into `Either[ADT, A]`. Usually paired with `rethrow`.
*
Expand Down
1 change: 0 additions & 1 deletion src/test/scala/pl/iterators/sealedmonad/SealedTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ trait SealedTests[F[_]] extends Laws with SealedTestInstances {
"result flatMap short-circuits" -> forAll(laws.resultFlatMapElimination[A, ADT] _),
"semiflatMap consistent with flatMap" -> forAll(laws.valueSemiflatMapReduction[A, B] _),
"result semiflatMap short-circuits" -> forAll(laws.resultSemiflatMapElimination[A, ADT] _),
"leftSemiflatMap identity" -> forAll(laws.resultLeftSemiflatMapIdentity[A, ADT] _),
"leftSemiflatMap elimination" -> forAll(laws.valueLeftSemiflatMapElimination[A, ADT] _),
"result biSemiflatMap coherent with result leftsemiFlatMap + semiflatMap" -> forAll(laws.resultBiSemiflatMapCoherence[A, ADT, B] _),
"value biSemiflatMap coherent with value leftsemiFlatMap + semiflatMap" -> forAll(laws.valueBiSemiflatMapCoherence[A, ADT, B] _),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ trait SealedLaws[F[_]] {
def valueSemiflatMapReduction[A, B](fa: F[A], f: A => F[B]) = Sealed(fa).semiflatMap(f) <-> Sealed(fa >>= f)
def resultSemiflatMapElimination[A, B](fb: F[B], f: A => F[B]) = Sealed.result(fb).semiflatMap(f) <-> Sealed.result(fb)

def resultLeftSemiflatMapIdentity[A, B](fa: F[A], fab: A => F[B]) =
Sealed.result(fa).leftSemiflatMap(fab) <-> Sealed.result(fa >>= fab)

def valueLeftSemiflatMapElimination[A, B](fa: F[A], fab: A => F[B]) =
Sealed(fa).leftSemiflatMap(fab) <-> Sealed(fa)

def resultBiSemiflatMapCoherence[A, B, C](fa: F[A], fab: A => F[B], fcd: A => F[C]) =
Sealed.result(fa).biSemiflatMap(fab, fcd) <-> Sealed.result(fa).leftSemiflatMap(fab).semiflatMap(fcd)
Sealed.apply(fa).biSemiflatMap(fab, fcd) <-> Sealed.apply(fa).leftSemiflatMap(fab).semiflatMap(fcd)

def valueBiSemiflatMapCoherence[A, B, C](fa: F[A], fab: A => F[B], fcd: A => F[C]) =
Sealed(fa).biSemiflatMap(fab, fcd) <-> Sealed(fa).leftSemiflatMap(fab).semiflatMap(fcd)
Expand Down

0 comments on commit a9627fc

Please sign in to comment.