Is there any for the following kind of pattern matching of Either? Something like map on both Right and Left to return an Either of different type. I don't want to specify Left and Right everywhere.
val v:Either[Throwable, String] = Right("Hello")
val result: Either[Int, String] = v match {
case Left(ex) => Left(ex.getMessage.size)
case Right(m) => Right(m)
}
In the following, you don't have to use pattern matching
v.left.map(_.getMessage).right.map(_)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With