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
def fail: Task[Int] = Task.fail(new Error("oops")) | |
lazy val test1 = f(fail) // hangs | |
def test2 = f(fail) // works | |
lazy val test3 = f(Task.fail(new Error("oops"))) // works |
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
def eta[M[_],A](a: A, m: Monad[M]): M[A] = m.unit(a) |
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
sealed trait Interact[A] | |
case class Ask(prompt: String) | |
extends Interact[String] | |
case class Tell(msg: String) | |
extends Interact[Unit] | |
trait Monad[M[_]] { | |
def pure[A](a: A): M[A] |
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
sealed trait Interact[A] | |
case class Ask(prompt: String) | |
extends Interact[String] | |
case class Tell(msg: String) | |
extends Interact[Unit] | |
trait Monad[M[_]] { | |
def pure[A](a: A): M[A] |
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 scala.reflect.macros.whitebox.Context | |
import scala.language.experimental.macros | |
sealed trait Trampoline[A] { | |
def flatMap[B](f: A => Trampoline[B]): Trampoline[B] = macro Trampoline.flatMapImpl[A,B] | |
} | |
case class Return[A](a: A) extends Trampoline[A] | |
case class Suspend[A](k: () => Trampoline[A]) extends Trampoline[A] |
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 scala.language.experimental.macros | |
import scala.reflect.macros.whitebox.Context | |
sealed trait Trampoline[A] { | |
def flatMap[B](f: A => Trampoline[B]): Trampoline[B] = macro Trampoline.flatMapImpl[A,B] | |
} | |
case class Return[A](a: A) extends Trampoline[A] | |
case class Suspend[A](k: () => Trampoline[A]) extends Trampoline[A] |
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
def fix[H,DA:c.WeakTypeTag,DB:c.WeakTypeTag]( | |
f: (c.Expr[DA => DB], H) => c.Expr[DA => DB]): H => c.Expr[DA => DB] = | |
h => c.Expr[DA => DB]( | |
q"{ def self(n: ${c.weakTypeOf[DA]}): ${c.weakTypeOf[DB]} = ${f((c.Expr(q"self"), h))}(n); self _}") |
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
// Finally tagless lambda calculus | |
object Final { | |
def varZ[A,B](env1: A, env2: B): A = env1 | |
def varS[A,B,T](vp: B => T)(env1: A, env2: B): T = vp(env2) | |
def b[E](bv: Boolean)(env: E): Boolean = bv | |
def lam[A,E,T](e: (A, E) => T)(env: E): A => T = x => e(x, env) | |
def app[A,E,T](e1: E => A => T, e2: E => A)(env: E): T = e1(env)(e2(env)) | |
def testf1[E,A](env: E): Boolean = | |
app(lam[Boolean,E,Boolean](varZ) _, b(true))(env) |
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 scala.language.experimental.macros | |
import scala.reflect.macros.whitebox.Context | |
class Foo { | |
override def toString: String = macro Foo.toStringImpl | |
} | |
object Foo { | |
def toStringImpl(c: Context): c.Expr[String] = { | |
import c.universe._ |
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
sealed trait WriterBox { | |
type T | |
def value: T | |
def evidence: Writer[T] | |
} | |
object WriterBox { | |
def apply[B](b:B)(implicit W: Writer[B]): WriterBox = | |
new WriterBox { | |
type T = B |