Created
September 10, 2017 10:21
-
-
Save philipschwarz/0e46c2567c2d8a532539e1d39c1bb4f7 to your computer and use it in GitHub Desktop.
This file contains 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 scalaz._ | |
import Scalaz._ | |
import simulacrum._ | |
import stalactite._ | |
import scala.concurrent.Future | |
class Main { | |
trait Terminal[C[_]] { | |
def read: C[String] | |
def write(t: String): C[Unit] | |
} | |
type Now[X] = X | |
object TerminalSync extends Terminal[Now] { | |
def read: String = readLine | |
def write(t: String): Unit = println(t) | |
} | |
object TerminalAsync extends Terminal[Future] { | |
def read: Future[String] = ??? | |
def write(t: String): Future[Unit] = ??? | |
} | |
trait Execution[C[_]] { | |
def doAndThen[A, B](c: C[A])(f: A => C[B]): C[B] | |
def create[B](b: B): C[B] | |
} | |
object Execution { | |
implicit class Ops[A, C[_]](c: C[A]) { | |
def flatMap[B](f: A => C[B])(implicit e: Execution[C]): C[B] = | |
e.doAndThen(c)(f) | |
def map[B](f: A => B)(implicit e: Execution[C]): C[B] = | |
e.doAndThen(c)(f andThen e.create) | |
} | |
} | |
def echo[C[_]](implicit t: Terminal[C], e: Execution[C]): C[String] = | |
for { | |
in <- t.read | |
_ <- t.write(in) | |
} yield in | |
implicit val aTerminal:Terminal[Now] = TerminalSync | |
implicit val anExecution:Execution[Now] = ??? | |
def main(args: Array[String]): Unit = { echo } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment