This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val lRight = r.left | |
// lRight: scala.util.Either.LeftProjection[String,User] = | |
// LeftProjection(Right(User(Sam,24))) | |
lRight.map(_.toUpperCase) | |
// scala.util.Either[String,User] = Right(User(Sam,24)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val lProjection = l.left | |
// lProjection: scala.util.Either.LeftProjection[String,User] = | |
// LeftProjection(Left(I'm an error!)) | |
lProjection.map(_.toUpperCase) | |
// scala.util.Either[String,User] = Left(NOT A USER) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lefty(s: String): String = "This was an error: " + s | |
// lefty: (s: String)String | |
def righty(u: User): String = "This was a user: " + u.name | |
// righty: (u: User)String | |
r.fold(lefty, righty) | |
// String = This was a user: Sam | |
l.fold(lefty, righty) | |
// String = This was an error: Not a user |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r match { | |
case Right(a) => println("User is named " + a.name) | |
case Left(b) => println("Error! " + b) | |
} | |
// User is named Sam |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val f: Either[String, User] = Left("I'm an error!") | |
// f: Either[String,User] = Left(I'm an error!) | |
f.map(either) | |
// scala.util.Either[String,Either[String,Int]] = Left(I'm an error!) | |
f.flatMap(either) | |
// scala.util.Either[String,Int] = Left(I'm an error!) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def either(u: User): Either[String, Int] = { | |
if(u.name.size == 0) Left("No name!") | |
else Right(u.name.size) | |
} | |
// either: (u: User)Either[String,Int] | |
r.map(either) | |
// scala.util.Either[String,Either[String,Int]] = Right(Right(3)) | |
r.flatMap(either) | |
// scala.util.Either[String,Int] = Right(3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r.map(user => user.name.toUpperCase) | |
// scala.util.Either[String,String] = Right(SAM) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val x = Left("Not a user") | |
//x: scala.util.Left[String,Nothing] = Left(Not a user) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val x = Right(u) | |
// x: scala.util.Right[Nothing,User] = Right(User(Sam,24)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class User(name: String, age: Int) | |
// defined class User | |
val u = User("Sam", 24) | |
// u: User = User(Sam,24) | |
val r: Either[String, User] = Right(u) | |
// r: Either[String,User] = Right(User(Sam,24)) | |
val l: Either[String, User] = Left("Not a user") | |
// l: Either[String,User] = Left(Not a user) |