Skip to content

Instantly share code, notes, and snippets.

@owainlewis
Last active August 27, 2015 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owainlewis/f82eb9ee0e2a4622935b to your computer and use it in GitHub Desktop.
Save owainlewis/f82eb9ee0e2a4622935b to your computer and use it in GitHub Desktop.
Scala Either

Working with Either

There are two ways to work with Either in Scala.

If you want to update only one side use a projection and map

response.right.map(_.parseJson.convertTo[Seq[String]])

If you want to update both sides then use a fold

response.fold(l => Left(l), r => Right(r))

Use some kind of bifunctor

def bimap[A,B,C,D](e: Either[A,B], f: A => C, g: B => D): Either[C, D] =
    e.fold(l => Left(f(l)), r => Right(g(r)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment