Skip to content

Commit

Permalink
Add ZIO/RIO/IO/Task.concurrentEffect & concurrentEffectWith helpers (z…
Browse files Browse the repository at this point in the history
…io#111)

* Add ZIO/RIO/IO/Task.concurrentEffect & concurrentEffectWith helpers

* silence unused warnings via deprecated annotation
  • Loading branch information
neko-kai authored Nov 17, 2019
1 parent b6930e1 commit 1d7c4b8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ def getCE = {
}
```

`Task.concurrentEffectWith` method can automate this pattern:

```scala
import cats.effect._
import zio._
import zio.interop.catz._

def fork = {
Task.concurrentEffectWith { implicit CE =>
CE.start(Task(println("Started task")))
}
}
```

### Timer

In order to get a `cats.effect.Timer[Task]` instance we need an extra import:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package zio.interop

import cats.effect.ConcurrentEffect
import zio.{ IO, RIO, Runtime, Task, ZIO }

import scala.language.implicitConversions

trait CatsConcurrentEffectSyntax {
implicit final def ZIOConcurrentEffectOps(@deprecated("", "") zio: ZIO.type): CatsConcurrentEffectSyntax.zioOps.type =
CatsConcurrentEffectSyntax.zioOps
implicit final def RIOConcurrentEffectOps(@deprecated("", "") rio: RIO.type): CatsConcurrentEffectSyntax.zioOps.type =
CatsConcurrentEffectSyntax.zioOps

implicit final def IOConcurrentEffectOps(@deprecated("", "") io: IO.type): CatsConcurrentEffectSyntax.ioOps.type =
CatsConcurrentEffectSyntax.ioOps
implicit final def TaskConcurrentEffectOps(@deprecated("", "") io: Task.type): CatsConcurrentEffectSyntax.ioOps.type =
CatsConcurrentEffectSyntax.ioOps
}

private[interop] object CatsConcurrentEffectSyntax {
object zioOps {
final def concurrentEffect[R]: ZIO[R, Nothing, ConcurrentEffect[RIO[R, *]]] =
ZIO.runtime.map(catz.taskEffectInstance(_: Runtime[R]))
final def concurrentEffectWith[R, E, A](f: ConcurrentEffect[RIO[R, *]] => ZIO[R, E, A]): ZIO[R, E, A] =
ZIO.runtime.flatMap(f apply catz.taskEffectInstance(_: Runtime[R]))
}
object ioOps {
final def concurrentEffect: ZIO[Any, Nothing, ConcurrentEffect[RIO[Any, *]]] =
ZIO.runtime.map(catz.taskEffectInstance(_: Runtime[Any]))
def concurrentEffectWith[E, A](f: ConcurrentEffect[RIO[Any, *]] => ZIO[Any, E, A]): ZIO[Any, E, A] =
ZIO.runtime.flatMap(f apply catz.taskEffectInstance(_: Runtime[Any]))
}
}
5 changes: 4 additions & 1 deletion interop-cats/shared/src/main/scala/zio/interop/cats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ abstract class CatsEffectPlatform
extends CatsEffectInstances
with CatsEffectZManagedInstances
with CatsZManagedInstances
with CatsZManagedSyntax {
with CatsZManagedSyntax
with CatsConcurrentEffectSyntax {

val console: interop.console.cats.type = interop.console.cats

trait CatsApp extends App {
Expand All @@ -58,6 +60,7 @@ abstract class CatsEffectPlatform
Clock.Live.clock.sleep(zio.duration.Duration.fromNanos(duration.toNanos))
}
}

}

abstract class CatsPlatform extends CatsInstances with CatsZManagedInstances
Expand Down
10 changes: 10 additions & 0 deletions interop-cats/shared/src/test/scala/zio/interop/catzSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ class catzSpec extends catzSpecZIOBase {
Monad[UIO]
}

object concurrentEffectSyntaxTest {
import cats.effect.syntax.all._

Task.concurrentEffectWith { implicit CE =>
Task(List(1, 2).parTraverseN(5L) { _ =>
Task(())
}).start
}
}

object syntaxTest {
def rioDimap(rio: RIO[Int, String]): RIO[String, Int] = rio.dimap[String, Int](_.length)(_.length)
def rioBimap(rio: RIO[Int, String]): ZIO[Int, String, Int] = rio.bimap(_.getMessage, _.length)
Expand Down

0 comments on commit 1d7c4b8

Please sign in to comment.