Skip to content

Instantly share code, notes, and snippets.

@yasuabe
Created April 7, 2019 15:08
Show Gist options
  • Save yasuabe/4fdc953f1b384742310037929ce59842 to your computer and use it in GitHub Desktop.
Save yasuabe/4fdc953f1b384742310037929ce59842 to your computer and use it in GitHub Desktop.
sample code for Cats MTL FunctorTell
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