Created
April 7, 2019 15:08
-
-
Save yasuabe/4fdc953f1b384742310037929ce59842 to your computer and use it in GitHub Desktop.
sample code for Cats MTL FunctorTell
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
import cats.FlatMap | |
import cats.data.{Chain, WriterT} | |
import cats.mtl.FunctorTell | |
import cats.syntax.flatMap._ | |
import cats.syntax.functor._ | |
import cats.data.Chain._ | |
import cats.effect.{IO, Sync} | |
import cats.mtl.instances.listen._ | |
import Chain.one | |
type Logs = Chain[String] | |
def tellFunc[F[_]: FlatMap: Sync](n: Int)(implicit F: FunctorTell[F, Logs]): F[Unit] = | |
for { | |
_ <- F.tell(one("begin")) | |
_ <- Sync[F].delay { println(s"n = $n") } | |
_ <- F.tell(one("end")) | |
} yield () | |
def program: IO[Logs] = tellFunc[WriterT[IO, Logs, ?]](42).written | |
program.unsafeRunSync() | |
// n = 42 | |
// res0: Logs = Chain(begin, end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment