Giter VIP home page Giter VIP logo

interop-cats's Introduction

Interop Cats Effect

Project stage CI Releases Snapshots

This library provides instances required by Cats Effect.

Installation

libraryDependencies += "dev.zio" %% "zio-interop-cats" % "<check-badge-for-latest-version>"

ZIO Cats Effect 3 instances

ZIO integrates with Typelevel libraries by providing an instance of Concurrent, Temporal and Async for Task as required, for instance, by fs2, doobie and http4s.

For convenience, we have defined an alias as follows:

type Task[A] = ZIO[Any, Throwable, A]

Therefore, we provide Cats Effect instances based on this specific datatype.

Concurrent

In order to get a Concurrent[Task] or Concurrent[RIO[R, *]] (note * is kind-projector notation) we need an implicit Runtime[R] in scope. The easiest way to get it is using ZIO.runtime:

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

def ceConcurrentForTaskExample = 
  ZIO.runtime.flatMap { implicit r: Runtime[Any] =>
    // the presence of a runtime allows you to summon Cats Effect Typeclasses
    val F: cats.effect.Concurrent[Task] = implicitly
    F.racePair(F.unit, F.unit)
  }

Temporal

Temporal requires a Runtime with zio.clock.Clock.

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

def ceTemporal =
  ZIO.runtime.flatMap { implicit r: Runtime[Clock] =>
    val F: cats.effect.Temporal[Task] = implicitly
    F.sleep(1.second) *> F.unit
  }

Async

Similar to the other examples, we require a Runtime with the zio.clock.Clock and zio.blocking.Blocking layer.

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

def ceAsync =
  ZIO.runtime.flatMap { implicit r: Runtime[Clock with Blocking] =>
    val F: cats.effect.Async[Task] = implicitly
    F.racePair(F.unit, F.sleep(1.second) *> F.unit)
  }

Other typeclasses

There are many other typeclasses and useful conversions that this library provides implementations for:

  • See zio/interop/cats.scala file to see all available typeclass implementations for the Cats Effect 3 typeclasses
  • See zio/stream/interop/cats.scala for ZStream typeclass implementations
  • See zio/stream/interop/FS2StreamSyntax.scala for FS2 <-> ZStream conversions

Easier imports (at a cost)

In the examples above, we had to bring the Runtime[Clocking with Blocking] via the ZIO.runtime combinator. This may not be ideal since everywhere you use these typeclasses, you will now be required to feed in the Runtime. For example, with FS2:

def example(implicit rts: Runtime[Clock with Blocking]): Task[Unit] =
  fs2.Stream
    .awakeDelay[Task](10.seconds) // this type annotation is mandatory
    .evalTap(in => cats.effect.std.Console.make[Task].println(s"Hello $in"))
    .compile
    .drain

Rather than requiring the runtime implicit, we can add an import (if we don't mind depending on Runtime.default):

import zio.interop.catz._
import zio.interop.catz.implicits._

val example: Task[Unit] =
  fs2.Stream
    .awakeDelay[Task](10.seconds)
    .evalTap(in => cats.effect.std.Console.make[Task].println(s"Hello $in"))
    .compile
    .drain

The major downside to doing this is you rely on live implementations of Clock and Blocking to summon instances which makes testing much more difficult for any Cats Effect code that you use.

cats-core

If you only need instances for cats-core typeclasses, not cats-effect import zio.interop.catz.core._:

import zio.interop.catz.core._

Note that this library only has an Optional dependency on cats-effect โ€“ if you or your libraries don't depend on it, this library will not add it to the classpath.

Example

The following example shows how to use ZIO with Doobie (a library for JDBC access) and FS2 (a streaming library), which both rely on Cats Effect instances (cats.effect.Async and cats.effect.Temporal):

import zio._
import zio.interop.catz._
import doobie._
import doobie.implicits._
import zio.blocking.Blocking
import zio.clock.Clock
import scala.concurrent.duration._

object DoobieH2Example extends App {
  override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
    ZIO.runtime.flatMap { implicit r: Runtime[Clock with Blocking] =>
      val xa: Transactor[Task] =
        Transactor.fromDriverManager[Task]("org.h2.Driver", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "user", "")

      sql"SELECT 42"
        .query[Int]
        .stream
        .transact(xa)
        .delayBy(1.second)
        .evalTap(i => blocking.blocking(Task.effectTotal(println(s"Data $i"))))
        .compile
        .drain
        .exitCode
    }
}

interop-cats's People

Contributors

scala-steward avatar mijicd avatar neko-kai avatar adamgfraser avatar ghostdogpr avatar jorokr21 avatar calvinlfer avatar zeal18 avatar danslapman avatar guymers avatar mschuwalow avatar vigoo avatar mindartur avatar tomwadeson avatar senia-psm avatar softinio avatar rtimush avatar lemastero avatar sideeffffect avatar odomontois avatar fristi avatar eugengarkusha avatar godplayschess avatar blast-hardcheese avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.