Skip to content

Instantly share code, notes, and snippets.

@halcat0x15a
Created September 10, 2012 06:06
do
import scala.language._
import scala.util.continuations._
import scalaz._, Scalaz._
import effect._, IO._
import scalaz.syntax.Ops
object Do {
sealed trait DoBindOps[F[_], A] extends Ops[F[A]] {
implicit val F: Bind[F]
val self: F[A]
def bind[B] = shift((k: A => F[B]) => self >>= k)
}
implicit def ToDoBindOps[F[_]: Bind, A](fa: F[A]) = new DoBindOps[F, A] {
val F = implicitly[Bind[F]]
val self = fa
}
def apply[F[_]: Bind, A](fa: => F[A] @cps[F[A]]) = reset(fa)
}
object Main extends SafeApp {
import Do._
override def runc = Do {
ch07.basicio.bind[Unit]
ch07.callingpure
}
object ch07 {
def basicio = Do {
putStrLn("Greetings! What is your name?").bind[Unit]
val inpStr = readLn.bind[Unit]
putStrLn(s"Welcome to Haskell, $inpStr!")
}
def callingpure = {
def name2reply(name: String) = {
val charcount = name.length
s"""Pleased to meet you, $name.
Your name contains $charcount characters."""
}
Do {
putStrLn("Greetings once again. What is your name?").bind[Unit]
val inpStr = readLn.bind[Unit]
val outStr = name2reply(inpStr)
putStrLn(outStr)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment