I have a list of Eithers
val list: List[Either[String, Int]] = List(Right(5), Left("abc"), Right(42))
As a result I want a Right
if everything in the list is a Right
else I want a Left
. This sounds like the list should be biased (e.g. use Try
instead) but let's assume it isn't or shouldn't be.
The content of the resulting Right
or Left
will always be the same (e.g. a string, see blow) - only the Container shall differ. E.g. with the list above we want to create a string from this list so the result should be of a Left
like Left("Right(5) -> Left(abc) -> Right(42)")
. If we had another Right(12)
instead of the Left("abc")
it should be Right("Right(5) -> Right(12) -> Right(42)")
.
I could manually check if there is at least one Left
in the list and branch with an if to create a Left
or a Right
as result, but I wonder: is there a more Scala-like way to do it?