Skip to content

Instantly share code, notes, and snippets.

@mmenestret
mmenestret / effectElimination.scala
Last active September 14, 2018 09:56
Local effect elimination
type Task[A] = IO[Throwable, A]
def createMonadState[S]: Task[MonadState[Task, S]] = ???
def myLocalState[F[_]: MonadState[?, MyStateType]]: F[Boolean] = ???
for {
monadState <- createMonadState[MyStateType]
result <- myLocalState[MyIO](monadState)
} yield result
@mmenestret
mmenestret / LoggingT.scala
Last active September 14, 2018 09:16
Creation of a MonadT for Log
case class LoggingT[F[_], A](
run: List[String] => F[(List[String], A)]) { self =>
def map[B](f: A => B)(implicit F: Functor[F]): LoggingT[F, B] =
LoggingT[F, B](log => self.run(log).map(t => (t._1, f(t._2))))
def flatMap[B](f: A => LoggingT[F, B])(implicit F: Monad[F]): LoggingT[F, B] =
LoggingT[F, B](log => self.run(log).flatMap(t => f(t._2).run(t._1)))
def eval(implicit F: Functor[F]): F[(List[String], A)] = run(Nil).map(t => (t._1.reverse, t._2))
}