Skip to content

Instantly share code, notes, and snippets.

@philipschwarz
Last active September 10, 2017 15:09
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 philipschwarz/58afd88408ee576e26e1cb529f799f33 to your computer and use it in GitHub Desktop.
Save philipschwarz/58afd88408ee576e26e1cb529f799f33 to your computer and use it in GitHub Desktop.
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] = ???
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