Skip to content

Instantly share code, notes, and snippets.

@petekneller
Forked from channingwalton/Console.scala
Last active August 29, 2015 14:10
Show Gist options
  • Save petekneller/f348a15e0645d9d9be8a to your computer and use it in GitHub Desktop.
Save petekneller/f348a15e0645d9d9be8a to your computer and use it in GitHub Desktop.
// No case classes
object Consoles {
trait ConsoleAlg[F[_]] {
def readLine: F[Option[String]]
def printLine(line: String): F[Unit]
}
trait Console[+A] {
def run[F[+ _]](F: ConsoleAlg[F]): F[A]
}
object Console {
val readLine: Console[Option[String]] = new Console[Option[String]] {
def run[F[+ _]](F: ConsoleAlg[F]): F[Option[String]] = F.readLine
}
def printLine(line: String): Console[Unit] = new Console[Unit] {
def run[F[+ _]](F: ConsoleAlg[F]): F[Unit] = F.printLine(line)
}
}
}
// With case classes and a visitor
object ConsoleVisitor {
trait ConsoleAlg[F[_]] {
def readLine: F[Option[String]]
def printLine(line: String): F[Unit]
}
trait Console[+A] {
def run[F[+ _]](F: ConsoleAlg[F]): F[A]
}
case object ReadLine extends Console[Option[String]] {
def run[F[+ _]](F: ConsoleAlg[F]): F[Option[String]] = F.readLine
}
case class PrintLine(line: String) extends Console[Unit] {
def run[F[+ _]](F: ConsoleAlg[F]): F[Unit] = F.printLine(line)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment