-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from hejfelix/main
Add support for cats-effect (mono-functor effects)
- Loading branch information
Showing
5 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
cats-effect-3/src/main/scala/parallelfor/interop/cats/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
cats-effect-3/src/test/scala/parallelfor/interop/cats/ParallelCatsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters