Last active
August 22, 2016 07:43
-
-
Save labra/2c4c9565ed584b4e1aa8d08ab845e11b to your computer and use it in GitHub Desktop.
Example using WriterT and liftT
This file contains hidden or 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
package examples | |
import cats._, data._ | |
import cats.implicits._ | |
object writerTExample { | |
type Env = Int | |
type Err = String | |
type V[A] = Either[Err,A] | |
type ReaderV[A] = Kleisli[V,Env,A] | |
def checkPos(x: Int): ReaderV[Int] = Kleisli.lift( | |
if (x > 0) Either.right(x) | |
else Either.left(s"$x is not positive") | |
) | |
type WriterV[A] = WriterT[ReaderV,String,A] | |
// The following definition is required by liftT | |
type WriterV1[F[_], A] = WriterT[F, String, A] | |
def checkPosW(x: Int) : WriterV[Int] = for { | |
v <- checkPos(x).liftT[WriterV1] | |
_ <- WriterT.tell[ReaderV,String]("Writing in w1") | |
} yield x | |
val e: Env = 0 | |
def runW[A](c: WriterV[A]) = c.run.run(e) | |
val rOK = runW(checkPosW(3)) | |
val rFail = runW(checkPosW(-3)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment