Skip to content

Instantly share code, notes, and snippets.

@ethul
Created May 12, 2012 05:06
Show Gist options
  • Save ethul/2664256 to your computer and use it in GitHub Desktop.
Save ethul/2664256 to your computer and use it in GitHub Desktop.
import scalaz._, Scalaz._, effect._
type Env = Map[String,String]
type Error = String
case class AppStack[A](f: Env => IO[Validation[Error,A]])
object AppStack {
implicit val appStackMonad = new Monad[AppStack] {
def point[A](a: => A): AppStack[A] =
AppStack(s => a.success[Error].point[IO])
def bind[A, B](fa: AppStack[A])(f: A => AppStack[B]): AppStack[B] =
AppStack { env =>
ValidationT(fa.f(env)).flatMap { (a: A) =>
ValidationT(f(a).f(env))
}.run
}
}
}
// Exercise the AppStack
type StatusMessage = String
def foo: AppStack[String] =
AppStack(e => e.get("foo").toSuccess("Error").point[IO])
def bar: String => AppStack[StatusMessage] =
s => s.point[AppStack]
def snaz: StatusMessage => Validation[Error,String] =
sm => sm.success
def liftValidation[A](v: Validation[Error, A]): AppStack[A] =
AppStack(e => v.point[IO])
def run =
for {
s <- foo
m <- bar(s)
z <- liftValidation(snaz(m))
} yield z
def go1 = run.f(Map("foo" -> "fiz")).unsafePerformIO
def go2 = run.f(Map()).unsafePerformIO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment