Skip to content

Instantly share code, notes, and snippets.

@tkroman
Last active March 4, 2019 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkroman/da857cb602d524bdbf3dbf87b5bad830 to your computer and use it in GitHub Desktop.
Save tkroman/da857cb602d524bdbf3dbf87b5bad830 to your computer and use it in GitHub Desktop.
loeb.scala
// http://blog.sigfpe.com/2006/11/from-l-theorem-to-spreadsheet.html
import cats._
import cats.implicits._
object Foo {
def main(args: Array[String]): Unit = {
// loeb :: Functor f => f (f x -> x) -> f x
// loeb x = fmap (\a -> a (loeb x)) x
type LF[F[_], A] = Eval[F[Eval[A]]]
type LL[A] = Eval[List[Eval[A]]]
def loeb[F[_]: Functor, A](ex: Eval[F[LF[F, A] => Eval[A]]]): Eval[F[Eval[A]]] = {
ex.map(x => x.fmap(a => loeb(ex).flatMap(l => a(Now(l)))))
}
import Eval._
val ex =
now(
List(
(_: LL[Int]) => now(1),
(xs: LL[Int]) => xs.map(ys => ys.length),
)
)
// test3 = [ (!!5), 3, (!!0)+(!!1), (!!2)*2, sum . take 3, 17]
val test3 =
now(
List(
(xs: LL[Int]) => xs.flatMap(_(5)),
(_: LL[Int]) => now(3),
(xs: LL[Int]) => xs.flatMap(xxs => xxs(0).flatMap(z => xxs(1).map(o => o + z))),
(xs: LL[Int]) => xs.flatMap(xxs => xxs(2).map(_ * 2)),
(xs: LL[Int]) => xs.map(_.take(3).map(_.value).sum),
(_: LL[Int]) => now(17),
)
)
println(loeb(test3).value.map(_.value))
println(loeb(ex).value.map(_.value))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment