Skip to content

Commit

Permalink
Merge pull request #268 from Kevin-Lee/task/265/Fx-is-no-longer-monad
Browse files Browse the repository at this point in the history
Close #265 - Fx should no longer be Monad
  • Loading branch information
kevin-lee authored Aug 11, 2021
2 parents 06e5a39 + e6a19c6 commit 6920f60
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 259 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ lazy val props =
final val Scala3Versions = List("3.0.0")
final val Scala3Version = Scala3Versions.head

// final val ProjectScalaVersion = Scala2Version
final val ProjectScalaVersion = Scala3Version
final val ProjectScalaVersion = Scala2Version
// final val ProjectScalaVersion = Scala3Version

lazy val licenses = List("MIT" -> url("http://opensource.org/licenses/MIT"))

Expand Down
39 changes: 3 additions & 36 deletions cats-effect/src/main/scala-2/effectie/cats/Fx.scala
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
package effectie.cats

import cats.effect.IO
import cats.{Id, Monad}
import cats.Id
import effectie.{CommonFx, OldEffectConstructor}

import scala.concurrent.{ExecutionContext, Future}

trait Fx[F[_]] extends EffectConstructor[F] with FxCtor[F] with CommonFx[F] with OldEffectConstructor[F] with Monad[F]
trait Fx[F[_]] extends EffectConstructor[F] with FxCtor[F] with CommonFx[F] with OldEffectConstructor[F]

object Fx {
def apply[F[_]: Fx]: Fx[F] = implicitly[Fx[F]]

implicit object IoFx extends Fx[IO] {

@inline private val syncIo: Monad[IO] = cats.effect.Sync[IO]

@inline override def pure[A](x: A): IO[A] = pureOf(x)

@inline override def flatMap[A, B](fa: IO[A])(f: A => IO[B]): IO[B] =
syncIo.flatMap(fa)(f)

@inline override def tailRecM[A, B](a: A)(f: A => IO[Either[A, B]]): IO[B] =
syncIo.tailRecM(a)(f)

@inline override def effectOf[A](a: => A): IO[A] = IO(a)

@inline override def pureOf[A](a: A): IO[A] = IO.pure(a)
Expand All @@ -38,33 +28,10 @@ object Fx {
with EffectConstructor[Future]
with FxCtor[Future]
with CommonFx.CommonFutureFx
with OldEffectConstructor.OldFutureEffectConstructor {

@inline private val futureInstance: Monad[Future] = cats.instances.future.catsStdInstancesForFuture

@inline override def pure[A](x: A): Future[A] =
pureOf(x)

@inline override def flatMap[A, B](fa: Future[A])(f: A => Future[B]): Future[B] =
futureInstance.flatMap(fa)(f)

@inline override def tailRecM[A, B](a: A)(f: A => Future[Either[A, B]]): Future[B] =
futureInstance.tailRecM(a)(f)

}
with OldEffectConstructor.OldFutureEffectConstructor

implicit object IdFx extends Fx[Id] {

@inline private val idInstance: Monad[Id] = cats.catsInstancesForId

@inline override def pure[A](x: A): Id[A] = pureOf(x)

@inline override def flatMap[A, B](fa: Id[A])(f: A => Id[B]): Id[B] =
idInstance.flatMap(fa)(f)

@inline override def tailRecM[A, B](a: A)(f: A => Id[Either[A, B]]): Id[B] =
idInstance.tailRecM(a)(f)

@inline override def effectOf[A](a: => A): Id[A] = a

@inline override def pureOf[A](a: A): Id[A] = a
Expand Down
36 changes: 2 additions & 34 deletions cats-effect/src/main/scala-3/effectie/cats/Fx.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,13 @@ import effectie.{CommonFx, OldEffectConstructor}

import scala.concurrent.{ExecutionContext, Future}

trait Fx[F[_]] extends EffectConstructor[F] with FxCtor[F] with CommonFx[F] with OldEffectConstructor[F] with Monad[F]
trait Fx[F[_]] extends EffectConstructor[F] with FxCtor[F] with CommonFx[F] with OldEffectConstructor[F]

object Fx {
def apply[F[_]: Fx]: Fx[F] = summon[Fx[F]]

given ioFx: Fx[IO] with {

private val syncIo: Monad[IO] = Sync[IO]

inline override def pure[A](x: A): IO[A] = pureOf(x)

inline override def flatMap[A, B](fa: IO[A])(f: A => IO[B]): IO[B] =
syncIo.flatMap(fa)(f)

inline override def tailRecM[A, B](a: A)(f: A => IO[Either[A, B]]): IO[B] =
syncIo.tailRecM(a)(f)

inline override def effectOf[A](a: => A): IO[A] = IO(a)

inline override def pureOf[A](a: A): IO[A] = IO.pure(a)
Expand All @@ -38,32 +28,10 @@ object Fx {
with EffectConstructor[Future]
with FxCtor[Future]
with CommonFx.CommonFutureFx
with OldEffectConstructor.OldFutureEffectConstructor {

private val futureInstance: Monad[Future] = cats.instances.future.catsStdInstancesForFuture

inline override def pure[A](x: A): Future[A] =
pureOf(x)

inline override def flatMap[A, B](fa: Future[A])(f: A => Future[B]): Future[B] =
futureInstance.flatMap(fa)(f)

inline override def tailRecM[A, B](a: A)(f: A => Future[Either[A, B]]): Future[B] =
futureInstance.tailRecM(a)(f)
}
with OldEffectConstructor.OldFutureEffectConstructor

given idFx: Fx[Id] with {

private val idInstance: Monad[Id] = cats.catsInstancesForId

inline override def pure[A](x: A): Id[A] = pureOf(x)

inline override def flatMap[A, B](fa: Id[A])(f: A => Id[B]): Id[B] =
idInstance.flatMap(fa)(f)

inline override def tailRecM[A, B](a: A)(f: A => Id[Either[A, B]]): Id[B] =
idInstance.tailRecM(a)(f)

inline override def effectOf[A](a: => A): Id[A] = a

inline override def pureOf[A](a: A): Id[A] = a
Expand Down
11 changes: 7 additions & 4 deletions cats-effect/src/test/scala-2/effectie/cats/FxSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package effectie.cats

import cats.effect._
import cats.{Eq, Id}
import cats.{Eq, Id, Monad}
import effectie.ConcurrentSupport
import hedgehog._
import hedgehog.runner._
Expand Down Expand Up @@ -88,7 +88,7 @@ object FxSpec extends Properties {

implicit val ioFx: Fx[IO] = Fx.IoFx

MonadSpec.testMonadLaws[IO]
MonadSpec.testMonadLaws[IO]("IO")
}

}
Expand Down Expand Up @@ -165,7 +165,7 @@ object FxSpec extends Properties {
Await.result(future, waitFor)
}

MonadSpec.testMonadLaws[Future]
MonadSpec.testMonadLaws[Future]("Future")
}

}
Expand Down Expand Up @@ -207,7 +207,10 @@ object FxSpec extends Properties {
actual ==== expected
}

def testMonadLaws: List[Test] = MonadSpec.testMonadLaws[Id]
def testMonadLaws: List[Test] = {
val idInstance: Monad[Id] = cats.catsInstancesForId
MonadSpec.testMonadLaws[Id]("Id")
}

}

Expand Down
11 changes: 7 additions & 4 deletions cats-effect/src/test/scala-3/effectie/cats/FxSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package effectie.cats

import cats.effect.*
import cats.{Eq, Id}
import cats.{Eq, Id, Monad}
import effectie.ConcurrentSupport
import hedgehog.*
import hedgehog.runner.*
Expand Down Expand Up @@ -86,7 +86,7 @@ object FxSpec extends Properties {

implicit val ioFx: Fx[IO] = Fx.ioFx

MonadSpec.testMonadLaws[IO]
MonadSpec.testMonadLaws[IO]("IO")
}

}
Expand Down Expand Up @@ -161,7 +161,7 @@ object FxSpec extends Properties {
Await.result(future, waitFor)
}

MonadSpec.testMonadLaws[Future]
MonadSpec.testMonadLaws[Future]("Future")
}

}
Expand Down Expand Up @@ -201,7 +201,10 @@ object FxSpec extends Properties {
actual ==== expected
}

def testMonadLaws: List[Test] = MonadSpec.testMonadLaws[Id]
def testMonadLaws: List[Test] = {
given idMonad: Monad[Id] = cats.catsInstancesForId
MonadSpec.testMonadLaws[Id]("Id")
}

}

Expand Down
6 changes: 3 additions & 3 deletions cats-effect/src/test/scala/effectie/cats/MonadSpec.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package effectie.cats

import cats.Eq
import cats.{Eq, Monad}
import hedgehog.runner.Test

object MonadSpec {
def testMonadLaws[F[_]: Fx](implicit eqF: Eq[F[Int]]): List[Test] =
effectie.testing.cats.MonadSpec.testAllLaws[F]
def testMonadLaws[F[_]: Fx: Monad](name: String)(implicit eqF: Eq[F[Int]]): List[Test] =
effectie.testing.cats.MonadSpec.testAllLaws[F](s"Fx[$name]")
}
39 changes: 3 additions & 36 deletions cats-effect3/src/main/scala-2/effectie/cats/Fx.scala
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
package effectie.cats

import cats.effect.IO
import cats.{Id, Monad}
import cats.Id
import effectie.{CommonFx, OldEffectConstructor}

import scala.concurrent.{ExecutionContext, Future}

trait Fx[F[_]] extends EffectConstructor[F] with FxCtor[F] with CommonFx[F] with OldEffectConstructor[F] with Monad[F]
trait Fx[F[_]] extends EffectConstructor[F] with FxCtor[F] with CommonFx[F] with OldEffectConstructor[F]

object Fx {
def apply[F[_]: Fx]: Fx[F] = implicitly[Fx[F]]

implicit object IoFx extends Fx[IO] {

@inline private val syncIo: Monad[IO] = cats.effect.Sync[IO]

@inline override def pure[A](x: A): IO[A] = pureOf(x)

@inline override def flatMap[A, B](fa: IO[A])(f: A => IO[B]): IO[B] =
syncIo.flatMap(fa)(f)

@inline override def tailRecM[A, B](a: A)(f: A => IO[Either[A, B]]): IO[B] =
syncIo.tailRecM(a)(f)

@inline override def effectOf[A](a: => A): IO[A] = IO(a)

@inline override def pureOf[A](a: A): IO[A] = IO.pure(a)
Expand All @@ -38,33 +28,10 @@ object Fx {
with EffectConstructor[Future]
with FxCtor[Future]
with CommonFx.CommonFutureFx
with OldEffectConstructor.OldFutureEffectConstructor {

@inline private val futureInstance: Monad[Future] = cats.instances.future.catsStdInstancesForFuture

@inline override def pure[A](x: A): Future[A] =
pureOf(x)

@inline override def flatMap[A, B](fa: Future[A])(f: A => Future[B]): Future[B] =
futureInstance.flatMap(fa)(f)

@inline override def tailRecM[A, B](a: A)(f: A => Future[Either[A, B]]): Future[B] =
futureInstance.tailRecM(a)(f)

}
with OldEffectConstructor.OldFutureEffectConstructor

implicit object IdFx extends Fx[Id] {

@inline private val idInstance: Monad[Id] = cats.catsInstancesForId

@inline override def pure[A](x: A): Id[A] = pureOf(x)

@inline override def flatMap[A, B](fa: Id[A])(f: A => Id[B]): Id[B] =
idInstance.flatMap(fa)(f)

@inline override def tailRecM[A, B](a: A)(f: A => Id[Either[A, B]]): Id[B] =
idInstance.tailRecM(a)(f)

@inline override def effectOf[A](a: => A): Id[A] = a

@inline override def pureOf[A](a: A): Id[A] = a
Expand Down
36 changes: 2 additions & 34 deletions cats-effect3/src/main/scala-3/effectie/cats/Fx.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,13 @@ import effectie.{CommonFx, OldEffectConstructor}

import scala.concurrent.{ExecutionContext, Future}

trait Fx[F[_]] extends EffectConstructor[F] with FxCtor[F] with CommonFx[F] with OldEffectConstructor[F] with Monad[F]
trait Fx[F[_]] extends EffectConstructor[F] with FxCtor[F] with CommonFx[F] with OldEffectConstructor[F]

object Fx {
def apply[F[_]: Fx]: Fx[F] = summon[Fx[F]]

given ioFx: Fx[IO] with {

private val syncIo: Monad[IO] = Sync[IO]

inline override def pure[A](x: A): IO[A] = pureOf(x)

inline override def flatMap[A, B](fa: IO[A])(f: A => IO[B]): IO[B] =
syncIo.flatMap(fa)(f)

inline override def tailRecM[A, B](a: A)(f: A => IO[Either[A, B]]): IO[B] =
syncIo.tailRecM(a)(f)

inline override def effectOf[A](a: => A): IO[A] = IO(a)

inline override def pureOf[A](a: A): IO[A] = IO.pure(a)
Expand All @@ -38,32 +28,10 @@ object Fx {
with EffectConstructor[Future]
with FxCtor[Future]
with CommonFx.CommonFutureFx
with OldEffectConstructor.OldFutureEffectConstructor {

private val futureInstance: Monad[Future] = cats.instances.future.catsStdInstancesForFuture

inline override def pure[A](x: A): Future[A] =
pureOf(x)

inline override def flatMap[A, B](fa: Future[A])(f: A => Future[B]): Future[B] =
futureInstance.flatMap(fa)(f)

inline override def tailRecM[A, B](a: A)(f: A => Future[Either[A, B]]): Future[B] =
futureInstance.tailRecM(a)(f)
}
with OldEffectConstructor.OldFutureEffectConstructor

given idFx: Fx[Id] with {

private val idInstance: Monad[Id] = cats.catsInstancesForId

inline override def pure[A](x: A): Id[A] = pureOf(x)

inline override def flatMap[A, B](fa: Id[A])(f: A => Id[B]): Id[B] =
idInstance.flatMap(fa)(f)

inline override def tailRecM[A, B](a: A)(f: A => Id[Either[A, B]]): Id[B] =
idInstance.tailRecM(a)(f)

inline override def effectOf[A](a: => A): Id[A] = a

inline override def pureOf[A](a: A): Id[A] = a
Expand Down
4 changes: 3 additions & 1 deletion cats-effect3/src/test/scala-2/effectie/cats/FxSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package effectie.cats

import cats.effect._
import cats.effect.testkit.TestContext
import cats.{Eq, Id}
import cats.{Eq, Id, Monad}
import effectie.ConcurrentSupport
import hedgehog._
import hedgehog.runner._
Expand Down Expand Up @@ -388,6 +388,8 @@ object FxSpec extends Properties {
actual ==== expected
}

implicit val idInstance: Monad[Id] = cats.catsInstancesForId

def testMonadLaws1_Identity: Property =
MonadSpec.test1_Identity[Id]

Expand Down
4 changes: 3 additions & 1 deletion cats-effect3/src/test/scala-3/effectie/cats/FxSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package effectie.cats
import cats.effect.*
import cats.effect.testkit.TestContext
import cats.effect.unsafe.IORuntime
import cats.{Eq, Id}
import cats.{Eq, Id, Monad}
import effectie.ConcurrentSupport
import hedgehog.*
import hedgehog.runner.*
Expand Down Expand Up @@ -384,6 +384,8 @@ object FxSpec extends Properties {
actual ==== expected
}

given idInstance: Monad[Id] = cats.catsInstancesForId

def testMonadLaws1_Identity: Property =
MonadSpec.test1_Identity[Id]

Expand Down
Loading

0 comments on commit 6920f60

Please sign in to comment.