Skip to content

Commit

Permalink
avoid package object if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Apr 17, 2022
1 parent ce672f9 commit 8f2815b
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@ import cats.effect.LiftIO
import cats.effect.unsafe.IORuntime
import org.atnos.eff._
import org.atnos.eff.addon.cats.effect.IOEffect
import org.atnos.eff.addon.cats.effect.IOInterpretation
import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration
import org.atnos.eff.Eff

package object effect {

implicit final def toIOOps[A](e: Eff[Fx1[IO], A]): IOOps[A] = new IOOps[A](e)
implicit final def toIOOps2[R, A](e: Eff[R, A]): IOOps2[R, A] = new IOOps2[R, A](e)

}

final class IOOps[A](private val e: Eff[Fx1[IO], A]) extends AnyVal {

def unsafeRunAsync(cb: Either[Throwable, A] => Unit)(implicit i: IORuntime): Unit =
Expand All @@ -35,16 +27,3 @@ final class IOOps[A](private val e: Eff[Fx1[IO], A]) extends AnyVal {
IOEffect.to(e)

}

final class IOOps2[R, A](private val e: Eff[R, A]) extends AnyVal {

def ioAttempt(implicit m: MemberInOut[IO, R]): Eff[R, Throwable Either A] =
IOEffect.ioAttempt(e)

def runIoMemo[U](cache: Cache)(implicit m: Member.Aux[Memoized, R, U], task: IO |= U): Eff[U, A] =
IOEffect.runIoMemo(cache)(e)

def ioMemo(key: AnyRef, cache: Cache)(implicit task: IO /= R): Eff[R, A] =
IOInterpretation.ioMemo(key, cache, e)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.atnos.eff.syntax.addon.cats

import cats.effect.IO
import org.atnos.eff._
import org.atnos.eff.addon.cats.effect.IOEffect
import org.atnos.eff.addon.cats.effect.IOInterpretation

final class IOOps2[R, A](private val e: Eff[R, A]) extends AnyVal {

def ioAttempt(implicit m: MemberInOut[IO, R]): Eff[R, Throwable Either A] =
IOEffect.ioAttempt(e)

def runIoMemo[U](cache: Cache)(implicit m: Member.Aux[Memoized, R, U], task: IO |= U): Eff[U, A] =
IOEffect.runIoMemo(cache)(e)

def ioMemo(key: AnyRef, cache: Cache)(implicit task: IO /= R): Eff[R, A] =
IOInterpretation.ioMemo(key, cache, e)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.atnos.eff.syntax.addon.cats

import cats.effect.IO
import org.atnos.eff._

package object effect {

implicit final def toIOOps[A](e: Eff[Fx1[IO], A]): IOOps[A] = new IOOps[A](e)
implicit final def toIOOps2[R, A](e: Eff[R, A]): IOOps2[R, A] = new IOOps2[R, A](e)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.atnos.eff.addon.doobie

object connectionio extends DoobieConnectionIOEffect

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.atnos.eff.addon.monix

object task extends TaskEffect
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.atnos.eff
package addon
package scalaz

import _root_.scalaz._
import _root_.scalaz.syntax.functor._
import _root_.scalaz.std.vector._

object EffScalaz {

def traverseA[R, F[_]: Traverse, A, B](fs: F[A])(f: A => Eff[R, B]): Eff[R, F[B]] =
Traverse[F].traverse(fs)(f)(EffScalazApplicative[R])

def sequenceA[R, F[_]: Traverse, A](fs: F[Eff[R, A]]): Eff[R, F[A]] =
Traverse[F].sequence(fs)(EffScalazApplicative[R])

def flatTraverseA[R, F[_], A, B](fs: F[A])(
f: A => Eff[R, F[B]]
)(implicit FT: Traverse[F], FM: Bind[F]): Eff[R, F[B]] =
FT.traverseM[A, Eff[R, *], B](fs)(f)(EffScalazApplicative[R], FM)

/** use the applicative instance of Eff to sequence a list of values, then flatten it */
def flatSequenceA[R, F[_], A](fs: F[Eff[R, F[A]]])(implicit FT: Traverse[F], FM: Bind[F]): Eff[R, F[A]] =
FT.traverseM[Eff[R, F[A]], Eff[R, *], A](fs)(identity)(EffScalazApplicative[R], FM)

def detach[M[_], A](eff: Eff[Fx1[M], A])(implicit m: Monad[M], b: BindRec[M]): M[A] =
BindRec[M].tailrecM[Eff[Fx1[M], A], A](eff) {
case Pure(a, Last(Some(l))) => Monad[M].point(-\/(l.value.as(a)))
case Pure(a, Last(None)) => Monad[M].point(\/-(a))

case Impure(NoEffect(a), continuation, last) =>
Monad[M].point(-\/(continuation(a).addLast(last)))

case Impure(u, continuation, last) =>
u match {
case NoEffect(a) => Monad[M].point(-\/(continuation(a).addLast(last)))
case UnionTagged(ta: M[Nothing] @unchecked, _) =>
last match {
case Last(Some(l)) => Monad[M].map(ta)(x => -\/(continuation(x).addLast(last)))
case Last(None) => Monad[M].map(ta)(x => -\/(continuation(x)))
}
}

case ap @ ImpureAp(_, _, _) =>
Monad[M].point(-\/(ap.toMonadic))
}

def detachA[M[_], A](
eff: Eff[Fx1[M], A]
)(implicit monad: Monad[M], bindRec: BindRec[M], applicative: Applicative[M]): M[A] =
BindRec[M].tailrecM[Eff[Fx1[M], A], A](eff) {
case Pure(a, Last(Some(l))) => monad.point(-\/(l.value.as(a)))
case Pure(a, Last(None)) => monad.point(\/-(a))

case Impure(NoEffect(a), continuation, last) =>
monad.point(-\/(continuation(a).addLast(last)))

case Impure(u, continuation, last) =>
u match {
case NoEffect(a) => Monad[M].point(-\/(continuation(a).addLast(last)))
case UnionTagged(ta: M[Nothing] @unchecked, _) =>
last match {
case Last(Some(l)) => Monad[M].map(ta)(x => -\/(continuation(x).addLast(last)))
case Last(None) => Monad[M].map(ta)(x => -\/(continuation(x)))
}
}

case ap @ ImpureAp(unions, continuation, last) =>
val effects = unions.unions.collect { case UnionTagged(mx: M[Nothing] @unchecked, _) => mx }
val sequenced = applicative.sequence[Nothing, Vector](effects)

last match {
case Last(Some(l)) => Monad[M].map(sequenced)(x => -\/(continuation(x).addLast(last)))
case Last(None) => Monad[M].map(sequenced)(x => -\/(continuation(x)))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.atnos.eff.addon.scalaz

object all extends either with eval with safe with validate
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package org.atnos.eff
package addon

import _root_.scalaz._
import Scalaz._

package object scalaz {

object all extends either with eval with safe with validate

/**
* Monad implementation for the Eff[R, *] type
*/
Expand Down Expand Up @@ -37,70 +34,4 @@ package object scalaz {
def combine(x: A, y: A): A = s.append(x, y)
}

object EffScalaz {

def traverseA[R, F[_]: Traverse, A, B](fs: F[A])(f: A => Eff[R, B]): Eff[R, F[B]] =
Traverse[F].traverse(fs)(f)(EffScalazApplicative[R])

def sequenceA[R, F[_]: Traverse, A](fs: F[Eff[R, A]]): Eff[R, F[A]] =
Traverse[F].sequence(fs)(EffScalazApplicative[R])

def flatTraverseA[R, F[_], A, B](fs: F[A])(f: A => Eff[R, F[B]])(implicit FT: Traverse[F], FM: Bind[F]): Eff[R, F[B]] =
FT.traverseM[A, Eff[R, *], B](fs)(f)(EffScalazApplicative[R], FM)

/** use the applicative instance of Eff to sequence a list of values, then flatten it */
def flatSequenceA[R, F[_], A](fs: F[Eff[R, F[A]]])(implicit FT: Traverse[F], FM: Bind[F]): Eff[R, F[A]] =
FT.traverseM[Eff[R, F[A]], Eff[R, *], A](fs)(identity)(EffScalazApplicative[R], FM)

def detach[M[_], A](eff: Eff[Fx1[M], A])(implicit m: Monad[M], b: BindRec[M]): M[A] =
BindRec[M].tailrecM[Eff[Fx1[M], A], A](eff) {
case Pure(a, Last(Some(l))) => Monad[M].point(-\/(l.value.as(a)))
case Pure(a, Last(None)) => Monad[M].point(\/-(a))

case Impure(NoEffect(a), continuation, last) =>
Monad[M].point(-\/(continuation(a).addLast(last)))

case Impure(u, continuation, last) =>
u match {
case NoEffect(a) => Monad[M].point(-\/(continuation(a).addLast(last)))
case UnionTagged(ta: M[Nothing] @unchecked, _) =>
last match {
case Last(Some(l)) => Monad[M].map(ta)(x => -\/(continuation(x).addLast(last)))
case Last(None) => Monad[M].map(ta)(x => -\/(continuation(x)))
}
}

case ap @ ImpureAp(_, _, _) =>
Monad[M].point(-\/(ap.toMonadic))
}

def detachA[M[_], A](eff: Eff[Fx1[M], A])(implicit monad: Monad[M], bindRec: BindRec[M], applicative: Applicative[M]): M[A] =
BindRec[M].tailrecM[Eff[Fx1[M], A], A](eff) {
case Pure(a, Last(Some(l))) => monad.point(-\/(l.value.as(a)))
case Pure(a, Last(None)) => monad.point(\/-(a))

case Impure(NoEffect(a), continuation, last) =>
monad.point(-\/(continuation(a).addLast(last)))

case Impure(u, continuation, last) =>
u match {
case NoEffect(a) => Monad[M].point(-\/(continuation(a).addLast(last)))
case UnionTagged(ta: M[Nothing] @unchecked, _) =>
last match {
case Last(Some(l)) => Monad[M].map(ta)(x => -\/(continuation(x).addLast(last)))
case Last(None) => Monad[M].map(ta)(x => -\/(continuation(x)))
}
}

case ap @ ImpureAp(unions, continuation, last) =>
val effects = unions.unions.collect { case UnionTagged(mx: M[Nothing] @unchecked, _) => mx }
val sequenced = applicative.sequence[Nothing, Vector](effects)

last match {
case Last(Some(l)) => Monad[M].map(sequenced)(x => -\/(continuation(x).addLast(last)))
case Last(None) => Monad[M].map(sequenced)(x => -\/(continuation(x)))
}
}
}

}
43 changes: 43 additions & 0 deletions shared/src/main/scala/org/atnos/eff/all.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.atnos.eff

object reader extends ReaderCreation with ReaderInterpretation
object writer extends WriterCreation with WriterInterpretation
object state extends StateCreation with StateInterpretation with StateImplicits
object eval extends EvalCreation with EvalInterpretation
object option extends OptionCreation with OptionInterpretation
object list extends ListCreation with ListInterpretation
object either extends EitherCreation with EitherInterpretation with EitherImplicits
object future extends FutureCreation with FutureInterpretation
object memo extends MemoCreation with MemoInterpretation

object create
extends ReaderCreation
with WriterCreation
with StateCreation
with EvalCreation
with OptionCreation
with ListCreation
with EitherCreation
with ValidateCreation
with ChooseCreation
with FutureCreation
with MemoCreation
with EffCreation
with SafeCreation

object all
extends ReaderEffect
with WriterEffect
with StateEffect
with EvalEffect
with OptionEffect
with ListEffect
with EitherEffect
with ValidateEffect
with ChooseEffect
with SafeEffect
with MemoEffect
with Batch
with EffInterpretation
with EffCreation
with EffImplicits
42 changes: 0 additions & 42 deletions shared/src/main/scala/org/atnos/eff/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,10 @@ package object eff {
type |=[M[_], R] = MemberIn.|=[M, R]

object eff extends EffCreation with EffInterpretation
object reader extends ReaderCreation with ReaderInterpretation
object writer extends WriterCreation with WriterInterpretation
object state extends StateCreation with StateInterpretation with StateImplicits
object eval extends EvalCreation with EvalInterpretation
object option extends OptionCreation with OptionInterpretation
object list extends ListCreation with ListInterpretation
object either extends EitherCreation with EitherInterpretation with EitherImplicits
object validate extends ValidateCreation with ValidateInterpretation
object choose extends ChooseCreation with ChooseInterpretation
object safe extends SafeCreation with SafeInterpretation
object future extends FutureCreation with FutureInterpretation
object memo extends MemoCreation with MemoInterpretation
object batch extends Batch

object create
extends ReaderCreation
with WriterCreation
with StateCreation
with EvalCreation
with OptionCreation
with ListCreation
with EitherCreation
with ValidateCreation
with ChooseCreation
with FutureCreation
with MemoCreation
with EffCreation
with SafeCreation

object all
extends ReaderEffect
with WriterEffect
with StateEffect
with EvalEffect
with OptionEffect
with ListEffect
with EitherEffect
with ValidateEffect
with ChooseEffect
with SafeEffect
with MemoEffect
with Batch
with EffInterpretation
with EffCreation
with EffImplicits

object interpret extends Interpret with Batch

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.atnos.eff.addon.twitter

object future extends TwitterFutureEffect

This file was deleted.

0 comments on commit 8f2815b

Please sign in to comment.