Skip to content

Instantly share code, notes, and snippets.

@atamborrino
Created October 1, 2014 13:37
Show Gist options
  • Save atamborrino/2d827db1df29dfb4f5a0 to your computer and use it in GitHub Desktop.
Save atamborrino/2d827db1df29dfb4f5a0 to your computer and use it in GitHub Desktop.
Simple monadic structure
sealed trait IOToolResponse[+A] {
def flatMap[B](f: A => IOToolResponse[B]): IOToolResponse[B] = this match {
case Authenticated(a) => f(a)
case na: NotAuthenticated => na
}
def map[B](f: A => B): IOToolResponse[B] = this.flatMap(a => IOToolResponse.unit(f(a)))
}
object IOToolResponse {
def unit[A](a: A): IOToolResponse[A] = Authenticated(a)
}
case class Authenticated[A](data: A) extends IOToolResponse[A]
case class NotAuthenticated(code: String, msg: String) extends IOToolResponse[Nothing]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment