Created
September 10, 2017 15:12
-
-
Save philipschwarz/0d5edd174056c8c121537b99655d794f to your computer and use it in GitHub Desktop.
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 scalaz._ | |
import Scalaz._ | |
import simulacrum._ | |
import stalactite._ | |
import scala.concurrent.Future | |
import scala.io.StdIn.readLine | |
object 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] | |
} | |
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] = new Execution[Now]{ | |
def doAndThen[A, B](c: Now[A])(f: A => Now[B]): Now[B] = f(c) | |
def create[B](b: B): Now[B] = b | |
} | |
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