Skip to content

tmccarthy/bfect

Repository files navigation

CircleCI Maven Central

Bfect

A collection of bifunctor effect typeclasses, with instances for ZIO and conversions for cats-effect.

Project structure

  • bfect-core - A collection of bifunctor effect typeclasses, based loosely around the structure of cats-effect
  • bfect-testing - An implementation of a bifunctor state monad along with instances for the bfect-core typeclasses
  • bfect-interop-cats - Implicit conversions between the bfect-core typeclasses and their analogs in cats-core and cats-effect
  • bfect-interop-fs2 - Utilities for interoperation with fs2
  • bfect-interop-zio - Instances of the bfect-core typeclasses for the ZIO IO monad
  • bfect-io - A half-finished bifunctor IO monad (don't use this)

Each of these are available through Maven Central, just add them to your project with your favourite build tool.

Typeclasses

Typeclass Cats equivalent Comment
Bifunctor (BFunctor) cats.Functor/cats.Bifunctor Functor with biMap and its derivations (map/rightMap, leftMap)
BifunctorMonad (BMonad) cats.Monad Monad. Adds flatMap, rightPure and leftPure.
BifunctorMonadError (BME) cats.MonadError Represents the ability to handle errors with handleErrorWith. Comes with the alias BME.
effects.Bracket cats.effect.Bracket Bracket. Represents the pure equivalent of try/finally
effects.Now cats.effect.Timer Represents the ability to create a timestamp
effects.Timer cats.effect.Timer Extends Now with the ability to delay execution for a period of time
effects.Die Extends BifunctorMonadError with the ability to suspend an effect that fails in an unchecked manner
effects.Sync cats.effect.Sync Extends Die with the ability to suspend arbitrary synchronous effects
effects.Async cats.effect.Async Extends Sync with the ability to register asynchronous effects
effects.Concurrent cats.effect.Concurrent Represents the effect of starting and cancelling tasks
effects.extra.Console Represents the effect of writing to the console
effects.extra.EnvVars Represents the effect of accessing environment variables
effects.extra.Resources Represents the effect of accessing Java resources
effects.extra.Calendar Extends Now with the ability to determine the system timezone, enabling computation of the local date and so on.

Note that unlike in cats, Bracket and Concurrent are not part of the main inheritance chain that includes Sync and Async

Usage

Use the following imports:

  • import au.id.tmm.bfect.syntax.all._ for extension methods
  • import au.id.tmm.bfect.instances.all._ for instances
  • import au.id.tmm.bfect.implicits._ for everything
import au.id.tmm.bfect.effects.Sync
import au.id.tmm.bfect.implicits._

// Companion objects provide static methods:

def hello1[F[_, _] : Sync]: F[Nothing, String] = Sync[F].pure("hello")
def hello2[F[_, _] : Sync]: F[Nothing, String] = Sync.pure("hello")

def print1[F[_, _] : Sync](string: String): F[Nothing, Unit] = Sync[F].sync(println(string))
def print2[F[_, _] : Sync](string: String): F[Nothing, Unit] = Sync.sync(println(string))

// Sync.Ops provides instance methods. The following are equivalent:

def printHello1[F[_, _] : Sync]: F[Nothing, Unit] = Sync[F].flatMap(hello1)(print1)
def printHello2[F[_, _] : Sync]: F[Nothing, Unit] = hello1.flatMap(print1)

// Importing Sync.Ops enables for-yield syntax:

def printHello3[F[_, _] : Sync]: F[Nothing, Unit] =
  for {
    hello <- hello1
    _     <- print1(hello)
  } yield ()

Cats interoperation

The bfect-interop-cats package provides implicits for interoperation with Cats. This includes instances for effect types using EitherT. The easiest way to access these is with the following import:

import au.id.tmm.bfect.interop.cats.implicits._