Skip to content

Commit

Permalink
Merge pull request #7 from hejfelix/main
Browse files Browse the repository at this point in the history
Add support for cats-effect (mono-functor effects)
  • Loading branch information
kitlangton authored Feb 7, 2022
2 parents 968b609 + 34f7b70 commit 9355702
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
19 changes: 16 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ val sharedSettings = Seq(
}
)

val zio1Version = "1.0.13"
val zio2Version = "2.0.0-RC2"
val zioQueryVersion = "0.3.0-RC2"
val zio1Version = "1.0.13"
val zio2Version = "2.0.0-RC2"
val zioQueryVersion = "0.3.0-RC2"
val catsEffect3Version = "3.3.5"

lazy val root = (project in file("."))
.aggregate(core, zio)
Expand Down Expand Up @@ -96,4 +97,16 @@ lazy val zioQuery = (project in file("zio-query"))
)
.dependsOn(core)

lazy val catsEffect3 = (project in file("cats-effect-3"))
.settings(
name := "parallel-for-cats-effect-3",
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-effect" % catsEffect3Version,
"com.disneystreaming" %% "weaver-cats" % "0.7.9" % Test
),
testFrameworks += new TestFramework("weaver.framework.CatsEffect"),
sharedSettings
)
.dependsOn(core)

testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package parallelfor.interop

import _root_.cats.effect.IO
import parallelfor.{Parallelizable, Parallelizable1}

package object cats {

implicit val ioPar1: Parallelizable1[IO] = new Parallelizable1[IO] {
override def zipPar[A, B](left: IO[A], right: IO[B]): IO[(A, B)] = left.both(right)
override def flatMap[A, B](fa: IO[A], f: A => IO[B]): IO[B] = fa.flatMap(f)
override def map[A, B](fa: IO[A], f: A => B): IO[B] = fa.map(f)
}

type IOP[-_, +_, +A] = IO[A]

implicit def convertIO(implicit p1: Parallelizable1[IO]): Parallelizable[IOP] = new Parallelizable[IOP] {
override def zipPar[R, E, A, B](left: IOP[R, E, A], right: IOP[R, E, B]): IOP[R, E, (A, B)] = p1.zipPar(left, right)
override def flatMap[R, E, A, B](fa: IOP[R, E, A], f: A => IOP[R, E, B]): IOP[R, E, B] = p1.flatMap(fa, f)
override def map[R, E, A, B](fa: IOP[R, E, A], f: A => B): IOP[R, E, B] = p1.map(fa, f)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package parallelfor.interop.cats

import cats.effect.{ExitCode, IO, IOApp, Ref}
import cats.effect.std.Console
import parallelfor.interop.cats._
import parallelfor.par1
import weaver.SimpleIOSuite

import scala.concurrent.duration.DurationInt

object ParallelCatsSpec extends SimpleIOSuite {

def example4(ref: Ref[IO, List[String]]) =
par1 {
for {
_ <- write(ref, "a")
b <- write(ref, "b")
_ <- write(ref, b)
_ <- write(ref, "c")
} yield ()
}

test("b is last") {
for {
ref <- Ref[IO].of(List.empty[String])
_ <- example4(ref)
result <- ref.get
} yield {
val last6 = result.drop(2)
expect(result.take(2) == List("end-b", "start-b")) and
forEach(last6.takeRight(3))(x => expect(x.startsWith("start"))) and
forEach(last6.take(3))(x => expect(x.startsWith("end")))
}
}

def write(r: Ref[IO, List[String]], s: String): IO[String] =
for {
_ <- r.update(_.prepended(s"start-$s"))
_ <- IO.sleep(1.seconds)
_ <- r.update(_.prepended(s"end-$s"))
res <- r.get
} yield s

}
6 changes: 6 additions & 0 deletions core/src/main/scala/parallelfor/Parallelizable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ trait Parallelizable[F[-_, +_, +_]] {
def map[R, E, A, B](fa: F[R, E, A], f: A => B): F[R, E, B]
}

trait Parallelizable1[F[+_]] {
def zipPar[A, B](left: F[A], right: F[B]): F[(A, B)]
def flatMap[A, B](fa: F[A], f: A => F[B]): F[B]
def map[A, B](fa: F[A], f: A => B): F[B]
}

object Parallelizable {

implicit final class ParallelizableOps[F[-_, +_, +_], -R, +E, +A](private val self: F[R, E, A])(implicit
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/parallelfor/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ import scala.language.experimental.macros
package object parallelfor {
def par[F[-_, +_, +_], R, E, A](effect: F[R, E, A])(implicit parallelizable: Parallelizable[F]): F[R, E, A] =
macro Macro.parallelizeImpl[F, R, E, A]

def par1[F[-_, +_, +_], G[+_], A](effect: G[A])(implicit
parallelizable: Parallelizable[F]
): F[Any, Nothing, A] =
macro Macro.parallelizeImpl[F, Any, Nothing, A]
}

0 comments on commit 9355702

Please sign in to comment.