Skip to content

Instantly share code, notes, and snippets.

@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))
}
@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
object HigherOrderAbstractSyntax {
/**
* Let's write a language which supports:
*
* let i = 0
* in while (i < 10) { i = i + 1 }
*/
trait Expr[F[_]] {
def intLit(value: Int): F[Int]
def add(l: F[Int], r: F[Int]): F[Int]
trait MonadReader[R, F[_]] {
def read: F[R]
}
trait HasEnv1[R] {
def env1: Lens[R, LowDslEnv1]
}
case class LowDslEnv1()
def myLowDsl1[R: HasEnv1, F[_]: MonadReader[R, ?]]: F[Unit] = ???
trait HasEnv2[R] {
@mmenestret
mmenestret / fp_taglessfinal_explanation.md
Last active April 19, 2024 14:19
FP and tagless final - Fabio Labella

Purity and pure FP

  • purity is a syntactical property of programs, more precisely defined as referential transparency: replacing an expression for its bound value doesn't change meaning (precise definition)
  • side effects are things that break referential transparency (precise definition)
  • There's no direct connection between the concepts of IO, State and so on, and side effects. Traditionally, these things are side effectful (break referential transparency), but you can have abstractions that are pure (don't break ref.trans.)
  • Due to the above, the term purely functional programming is not an oxymoron, it makes perfect sense.
  • In haskell style pure FP, type constructors and algebras are used to represent "things that would otherwise traditionally be side effects (break ref.trans.)". We call these F[_]s effects or computational contexts for brevity. You can see for yourself how this cannot be a precise definition (especially because they could also have kind higher than * -> *, even though most famous a
object Validation extends App {
object FailFast {
def validationEven(i: Int): Either[String, Int] = if (i % 2 == 0) Right(i) else Left("Odd")
def validationLT100(i: Int): Either[String, Int] = if (i < 100) Right(i) else Left("GT 100")
def validationGT0(i: Int): Either[String, Int] = if (i > 0) Right(i) else Left("LT 0")
def validation(i: Int): Either[String, Int] =
for {
_ <- validationEven(i)
@mmenestret
mmenestret / tf_to_readert.scala
Last active February 26, 2019 14:10
Tagless final -> Explicit dependencies passing -> ReaderT
object BoilerPlate {
// Some random effect type classes we'll want to stack (MTL)
trait IOEffect[F[_]]
trait StateEffect[F[_]]
// Our "final" type that match our needs, providing the typeclass instances needed by our algebras implementations
// It's often a monad transformer stack or custom type classes implementations
type MyF[A]
implicit def myFIO: IOEffect[MyF] = ???
object EnvEffetWithoutSubtyping {
import EnvEffetWithoutSubtyping.Alg1.HasAlg1
import EnvEffetWithoutSubtyping.Alg2.HasAlg2
import EnvEffetWithoutSubtyping.Alg3.HasAlg3
trait Alg1
object Alg1 {
trait HasAlg1[E] {
def alg1(e: E): Alg1
object EnvEffetWithoutSubtyping {
import EnvEffetWithoutSubtyping.Alg1.HasAlg1
import EnvEffetWithoutSubtyping.Alg2.HasAlg2
import EnvEffetWithoutSubtyping.Alg3.HasAlg3
trait Alg1
object Alg1 {
// Describe an env intersection type which contains an Alg1 type
trait HasAlg1[E] {
implicit class InfixMappable[A, B, C](f: A => B) {
def `<$>`[G[_]: Functor](ga: G[A]): G[B] = Functor[G].map(ga)(f)
}
val ap1: Option[Int] = ???
val ap2: Option[Int] = ???
val ap3: Option[Int] = ???
val f: Int => Int => Int => Int = ???
f `<$>` ap1 ap ap2 ap ap3