Created
April 7, 2019 15:18
-
-
Save yasuabe/80cf29613393ad21e2277dedf6ddcd44 to your computer and use it in GitHub Desktop.
sample code for Cats MTL ApplicativeLocal
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.data.ReaderT | |
import cats.effect.IO | |
import cats.syntax.flatMap._ | |
import cats.syntax.functor._ | |
import cats.{Applicative, Monad} | |
import cats.mtl.{ApplicativeAsk, ApplicativeLocal} | |
type Config = Map[String, String] | |
def get42[F[_]: Applicative](implicit F: ApplicativeAsk[F, Config]): F[String] = | |
F.ask.map(_.getOrElse("42", "none")) | |
def both[F[_]: Monad](implicit F: ApplicativeLocal[F, Config]): F[(String, String)] = | |
for { | |
m <- F.local(_.updated("42", "modified"))(get42[F]) | |
o <- get42[F] | |
} yield (m, o) | |
import cats.mtl.instances.local._ | |
both[ReaderT[IO, Config, ?]].run(Map("42" -> "original")).unsafeRunSync() | |
// (modified,original) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment