Skip to content

Instantly share code, notes, and snippets.

@yasuabe
Created April 7, 2019 15:27
Show Gist options
  • Save yasuabe/432458417ae5f9353480b8383018922d to your computer and use it in GitHub Desktop.
Save yasuabe/432458417ae5f9353480b8383018922d to your computer and use it in GitHub Desktop.
sample code for Eff
import cats.data.State
import org.atnos.eff._
import org.atnos.eff.all._
import org.atnos.eff.syntax.all._
object E {
type _eitherString[R] = Either[String, ?] |= R
type _stateInt[R] = State[Int, ?] |= R
def decr[R: _eitherString: _stateInt]: Eff[R, Unit] = for {
x <- get
_ <- if (x > 0) put(x - 1) else left("error")
} yield ()
type ETree = Fx.fx2[State[Int, ?], Either[String, ?]]
def runSE(n: Int): Either[String, (Unit, Int)] = decr[ETree].runState(n).runEither.run
def runES(n: Int): (Either[String, Unit], Int) = decr[ETree].runEither.runState(n).run
}
E.runSE(0) // Left(error)
E.runSE(1) // Right(((), 0))
E.runES(0) // (Left(error),0)
E.runES(1) // (Right(()),0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment