Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Created February 28, 2011 04:38
Show Gist options
  • Select an option

  • Save chrislewis/846939 to your computer and use it in GitHub Desktop.

Select an option

Save chrislewis/846939 to your computer and use it in GitHub Desktop.
taming Either
class RichEither[L, R](val either: Either[L, R]) {
def mapRight[RR](f: R => RR): Either[L, RR] = either match {
case Left(l) => Left(l)
case Right(r) => Right(f(r))
}
}
implicit def either2rich[L, R](either: Either[L, R]) = new RichEither(either)
/*
def readFile(name: File): Either[String, String] = { ... }
def parseJson(raw: String): Either[String, JObject] = { ... }
def parsePerson(json: JObject): Either[String, Person] = { ... }
readFile("foo.json").mapRight(parseJson).mapRight(parsePerson)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment