Skip to content

Commit

Permalink
CLose #261 - Add FxCtor as an alternative to EffectConstructor
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-lee committed Aug 8, 2021
1 parent c3baaa6 commit 85b07a0
Show file tree
Hide file tree
Showing 77 changed files with 1,822 additions and 479 deletions.
8 changes: 4 additions & 4 deletions cats-effect/src/main/scala-2/effectie/cats/Catching.scala
Expand Up @@ -56,8 +56,8 @@ object Catching extends Catching {
private[Catching] final class CurriedCanCatchF2[F[_], B](
private val b: () => B
) extends AnyVal {
def apply[A](f: Throwable => A)(implicit EC: Fx[F], CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatal(Fx[F].effectOf(b()))(f)
def apply[A](f: Throwable => A)(implicit EC: FxCtor[F], CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatal(FxCtor[F].effectOf(b()))(f)
}

private[Catching] final class CurriedCanCatchEither1[F[_]](
Expand Down Expand Up @@ -85,8 +85,8 @@ object Catching extends Catching {
private[Catching] final class CurriedCanCatchEitherF2[F[_], A, B](
private val ab: () => Either[A, B]
) extends AnyVal {
def apply(f: Throwable => A)(implicit EC: Fx[F], CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatalEither(Fx[F].effectOf(ab()))(f)
def apply(f: Throwable => A)(implicit EC: FxCtor[F], CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatalEither(FxCtor[F].effectOf(ab()))(f)
}

private[Catching] final class CurriedCanCatchEitherT1[F[_]](
Expand Down
Expand Up @@ -11,20 +11,20 @@ object ConsoleEffect {
def apply[F[_]: ConsoleEffect]: ConsoleEffect[F] =
implicitly[ConsoleEffect[F]]

implicit def consoleEffectF[F[_]: Fx: FlatMap]: ConsoleEffect[F] =
implicit def consoleEffectF[F[_]: FxCtor: FlatMap]: ConsoleEffect[F] =
new ConsoleEffectF[F]

final class ConsoleEffectF[F[_]: Fx: FlatMap] extends ConsoleEffectWithoutFlatMap[F] with ConsoleEffect[F] {
final class ConsoleEffectF[F[_]: FxCtor: FlatMap] extends ConsoleEffectWithoutFlatMap[F] with ConsoleEffect[F] {

@SuppressWarnings(Array("org.wartremover.warts.Recursion"))
override def readYesNo(prompt: String): F[YesNo] = for {
_ <- putStrLn(prompt)
answer <- readLn
yesOrN <- answer match {
case "y" | "Y" =>
Fx[F].effectOf(YesNo.yes)
FxCtor[F].effectOf(YesNo.yes)
case "n" | "N" =>
Fx[F].effectOf(YesNo.no)
FxCtor[F].effectOf(YesNo.no)
case _ =>
readYesNo(prompt)
}
Expand Down
28 changes: 4 additions & 24 deletions cats-effect/src/main/scala-2/effectie/cats/EffectConstructor.scala
Expand Up @@ -6,37 +6,17 @@ import effectie.{CommonFx, OldEffectConstructor}

import scala.concurrent.{ExecutionContext, Future}

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

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

implicit final val ioEffectConstructor: EffectConstructor[IO] = new EffectConstructor[IO] {

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

override def pureOf[A](a: A): IO[A] = IO.pure(a)

override val unitOf: IO[Unit] = IO.unit
}
implicit final val ioEffectConstructor: EffectConstructor[IO] = Fx.IoFx

@SuppressWarnings(Array("org.wartremover.warts.ImplicitParameter"))
implicit def futureEffectConstructor(implicit EC: ExecutionContext): EffectConstructor[Future] =
new FutureEffectConstructor(EC)

final class FutureEffectConstructor(override val EC0: ExecutionContext)
extends EffectConstructor[Future]
with Fx[Future]
with CommonFx.CommonFutureFx
with OldEffectConstructor.OldFutureEffectConstructor

implicit final val idEffectConstructor: EffectConstructor[Id] = new EffectConstructor[Id] {

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

@inline override def pureOf[A](a: A): Id[A] = effectOf(a)
Fx.futureFx

@inline override def unitOf: Id[Unit] = ()
}
implicit final val idEffectConstructor: EffectConstructor[Id] = Fx.IdFx

}
12 changes: 6 additions & 6 deletions cats-effect/src/main/scala-2/effectie/cats/Effectful.scala
Expand Up @@ -11,10 +11,10 @@ trait Effectful {
@deprecated(message = "Use pureOf instead.", since = "1.4.0")
@inline def effectOfPure[F[_]]: CurriedEffectOfPure[F] = pureOf[F]

def unitOf[F[_]: Fx]: F[Unit] = Fx[F].unitOf
def unitOf[F[_]: FxCtor]: F[Unit] = FxCtor[F].unitOf

@deprecated(message = "Use unitOf instead", since = "1.4.0")
@inline def effectOfUnit[F[_]: Fx]: F[Unit] = unitOf[F]
@inline def effectOfUnit[F[_]: FxCtor]: F[Unit] = unitOf[F]

}

Expand All @@ -24,15 +24,15 @@ object Effectful extends Effectful {
private[Effectful] final class CurriedEffectOf[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[A](a: => A)(implicit EF: Fx[F]): F[A] =
Fx[F].effectOf(a)
def apply[A](a: => A)(implicit EF: FxCtor[F]): F[A] =
FxCtor[F].effectOf(a)
}

private[Effectful] final class CurriedEffectOfPure[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[A](a: A)(implicit EF: Fx[F]): F[A] =
Fx[F].pureOf(a)
def apply[A](a: A)(implicit EF: FxCtor[F]): F[A] =
FxCtor[F].pureOf(a)
}

}
24 changes: 12 additions & 12 deletions cats-effect/src/main/scala-2/effectie/cats/EitherTSupport.scala
Expand Up @@ -40,43 +40,43 @@ object EitherTSupport extends EitherTSupport {
private[EitherTSupport] final class PartiallyAppliedEitherTEffectOf[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[A, B](ab: => Either[A, B])(implicit EF: Fx[F]): EitherT[F, A, B] =
EitherT(Fx[F].effectOf(ab))
def apply[A, B](ab: => Either[A, B])(implicit EF: FxCtor[F]): EitherT[F, A, B] =
EitherT(FxCtor[F].effectOf(ab))
}

private[EitherTSupport] final class PartiallyAppliedEitherTEffectOfPure[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[A, B](ab: Either[A, B])(implicit EF: Fx[F]): EitherT[F, A, B] =
EitherT(Fx[F].pureOf(ab))
def apply[A, B](ab: Either[A, B])(implicit EF: FxCtor[F]): EitherT[F, A, B] =
EitherT(FxCtor[F].pureOf(ab))
}

private[EitherTSupport] final class PartiallyAppliedEitherTRightEffectOf[A](
private val dummy: Boolean = true
) extends AnyVal {
def apply[F[_], B](b: => B)(implicit EC: Fx[F], FT: Functor[F]): EitherT[F, A, B] =
EitherT.liftF(Fx[F].effectOf(b))
def apply[F[_], B](b: => B)(implicit EC: FxCtor[F], FT: Functor[F]): EitherT[F, A, B] =
EitherT.liftF(FxCtor[F].effectOf(b))
}

private[EitherTSupport] final class PartiallyAppliedEitherTRightEffectOfPure[A](
private val dummy: Boolean = true
) extends AnyVal {
def apply[F[_], B](b: B)(implicit EC: Fx[F], FT: Functor[F]): EitherT[F, A, B] =
EitherT.liftF(Fx[F].pureOf(b))
def apply[F[_], B](b: B)(implicit EC: FxCtor[F], FT: Functor[F]): EitherT[F, A, B] =
EitherT.liftF(FxCtor[F].pureOf(b))
}

private[EitherTSupport] final class PartiallyAppliedEitherTLeftEffectOf[B](
private val dummy: Boolean = true
) extends AnyVal {
def apply[F[_], A](a: => A)(implicit EC: Fx[F], FT: Functor[F]): EitherT[F, A, B] =
EitherT(Fx[F].effectOf(a).map(_.asLeft[B]))
def apply[F[_], A](a: => A)(implicit EC: FxCtor[F], FT: Functor[F]): EitherT[F, A, B] =
EitherT(FxCtor[F].effectOf(a).map(_.asLeft[B]))
}

private[EitherTSupport] final class PartiallyAppliedEitherTLeftEffectOfPure[B](
private val dummy: Boolean = true
) extends AnyVal {
def apply[F[_], A](a: A)(implicit EC: Fx[F], FT: Functor[F]): EitherT[F, A, B] =
EitherT(Fx[F].pureOf(a).map(_.asLeft[B]))
def apply[F[_], A](a: A)(implicit EC: FxCtor[F], FT: Functor[F]): EitherT[F, A, B] =
EitherT(FxCtor[F].pureOf(a).map(_.asLeft[B]))
}

private[EitherTSupport] final class PartiallyAppliedEitherTRightF[A](
Expand Down
32 changes: 26 additions & 6 deletions cats-effect/src/main/scala-2/effectie/cats/Fx.scala
@@ -1,22 +1,42 @@
package effectie.cats

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

import scala.concurrent.{ExecutionContext, Future}

trait Fx[F[_]] extends CommonFx[F] with OldEffectConstructor[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 final val ioFx: Fx[IO] = EffectConstructor.ioEffectConstructor
implicit object IoFx extends Fx[IO] {

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

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

@inline override val unitOf: IO[Unit] = IO.unit
}

@SuppressWarnings(Array("org.wartremover.warts.ImplicitParameter"))
implicit def futureFx(implicit EC: ExecutionContext): Fx[Future] =
EffectConstructor.futureEffectConstructor(EC)
implicit def futureFx(implicit EC: ExecutionContext): Fx[Future] = new FutureFx

final class FutureFx(implicit override val EC0: ExecutionContext)
extends Fx[Future]
with EffectConstructor[Future]
with FxCtor[Future]
with CommonFx.CommonFutureFx
with OldEffectConstructor.OldFutureEffectConstructor

implicit object IdFx extends Fx[Id] {

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

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

implicit final val idFx: Fx[Id] = EffectConstructor.idEffectConstructor
@inline override val unitOf: Id[Unit] = ()
}

}
22 changes: 22 additions & 0 deletions cats-effect/src/main/scala-2/effectie/cats/FxCtor.scala
@@ -0,0 +1,22 @@
package effectie.cats

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

import scala.concurrent.{ExecutionContext, Future}

trait FxCtor[F[_]] extends CommonFx[F] with OldEffectConstructor[F]

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

implicit final val ioFxCtor: FxCtor[IO] = Fx.IoFx

@SuppressWarnings(Array("org.wartremover.warts.ImplicitParameter"))
implicit def futureFxCtor(implicit EC: ExecutionContext): FxCtor[Future] =
Fx.futureFx

implicit final val idFxCtor: FxCtor[Id] = Fx.IdFx

}
12 changes: 6 additions & 6 deletions cats-effect/src/main/scala-2/effectie/cats/OptionTSupport.scala
Expand Up @@ -36,28 +36,28 @@ object OptionTSupport extends OptionTSupport {
private[OptionTSupport] final class PartiallyAppliedOptionTOf[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[A](a: => Option[A])(implicit EF: Fx[F]): OptionT[F, A] =
OptionT(Fx[F].effectOf(a))
def apply[A](a: => Option[A])(implicit EF: FxCtor[F]): OptionT[F, A] =
OptionT(FxCtor[F].effectOf(a))
}

private[OptionTSupport] final class PartiallyAppliedOptionTOfPure[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[A](a: Option[A])(implicit EF: Fx[F]): OptionT[F, A] =
OptionT(Fx[F].pureOf(a))
def apply[A](a: Option[A])(implicit EF: FxCtor[F]): OptionT[F, A] =
OptionT(FxCtor[F].pureOf(a))
}

private[OptionTSupport] final class PartiallyAppliedOptionTSome[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[A](a: => A)(implicit EC: Fx[F], FT: Functor[F]): OptionT[F, A] =
def apply[A](a: => A)(implicit EC: FxCtor[F], FT: Functor[F]): OptionT[F, A] =
OptionT.liftF(EC.effectOf(a))
}

private[OptionTSupport] final class PartiallyAppliedOptionTSomePure[F[_]](
private val dummy: Boolean = true
) extends AnyVal {
def apply[A](a: A)(implicit EC: Fx[F], FT: Functor[F]): OptionT[F, A] =
def apply[A](a: A)(implicit EC: FxCtor[F], FT: Functor[F]): OptionT[F, A] =
OptionT.liftF(EC.pureOf(a))
}

Expand Down
8 changes: 4 additions & 4 deletions cats-effect/src/main/scala-3/effectie/cats/Catching.scala
Expand Up @@ -52,8 +52,8 @@ object Catching extends Catching {
private[Catching] final class CurriedCanCatchF2[F[_], B](
private val b: () => B
) extends AnyVal {
def apply[A](f: Throwable => A)(using EC: Fx[F], CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatal(Fx[F].effectOf(b()))(f)
def apply[A](f: Throwable => A)(using EC: FxCtor[F], CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatal(FxCtor[F].effectOf(b()))(f)
}

private[Catching] final class CurriedCanCatchEither1[F[_]](
Expand All @@ -80,8 +80,8 @@ object Catching extends Catching {
private[Catching] final class CurriedCanCatchEitherF2[F[_], A, B](
private val ab: () => Either[A, B]
) extends AnyVal {
def apply(f: Throwable => A)(using EC: Fx[F], CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatalEither(Fx[F].effectOf(ab()))(f)
def apply(f: Throwable => A)(using EC: FxCtor[F], CC: CanCatch[F]): F[Either[A, B]] =
CanCatch[F].catchNonFatalEither(FxCtor[F].effectOf(ab()))(f)
}

private[Catching] final class CurriedCanCatchEitherT1[F[_]](
Expand Down
Expand Up @@ -10,19 +10,19 @@ trait ConsoleEffect[F[_]] extends effectie.ConsoleEffect[F]
object ConsoleEffect {
def apply[F[_]: ConsoleEffect]: ConsoleEffect[F] = summon[ConsoleEffect[F]]

given consoleEffectF[F[_]: Fx: FlatMap]: ConsoleEffect[F] =
given consoleEffectF[F[_]: FxCtor: FlatMap]: ConsoleEffect[F] =
new ConsoleEffectF[F]

final class ConsoleEffectF[F[_]: Fx: FlatMap] extends ConsoleEffectWithoutFlatMap[F] with ConsoleEffect[F] {
final class ConsoleEffectF[F[_]: FxCtor: FlatMap] extends ConsoleEffectWithoutFlatMap[F] with ConsoleEffect[F] {

override def readYesNo(prompt: String): F[YesNo] = for {
_ <- putStrLn(prompt)
answer <- readLn
yesOrN <- answer match {
case "y" | "Y" =>
Fx[F].effectOf(YesNo.yes)
FxCtor[F].effectOf(YesNo.yes)
case "n" | "N" =>
Fx[F].effectOf(YesNo.no)
FxCtor[F].effectOf(YesNo.no)
case _ =>
readYesNo(prompt)
}
Expand Down
29 changes: 4 additions & 25 deletions cats-effect/src/main/scala-3/effectie/cats/EffectConstructor.scala
Expand Up @@ -6,36 +6,15 @@ import effectie.{CommonFx, OldEffectConstructor}

import scala.concurrent.{ExecutionContext, Future}

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

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

given ioEffectConstructor: EffectConstructor[IO] with {
given ioEffectConstructor: EffectConstructor[IO] = Fx.ioFx

override def effectOf[A](a: => A): IO[A] = IO(a)
given futureEffectConstructor(using EC: ExecutionContext): EffectConstructor[Future] = Fx.futureFx

override def pureOf[A](a: A): IO[A] = IO.pure(a)

override val unitOf: IO[Unit] = IO.unit
}

given futureEffectConstructor(using EC: ExecutionContext): EffectConstructor[Future] =
new FutureEffectConstructor(EC)

final class FutureEffectConstructor(override val EC0: ExecutionContext)
extends EffectConstructor[Future]
with Fx[Future]
with CommonFx.CommonFutureFx
with OldEffectConstructor.OldFutureEffectConstructor

given idEffectConstructor: EffectConstructor[Id] with {

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

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

@inline override def unitOf: Id[Unit] = ()
}
given idEffectConstructor: EffectConstructor[Id] = Fx.idFx

}

0 comments on commit 85b07a0

Please sign in to comment.