Either is a type used in functional languages such as Haskell and Scala to represent a value that is one of two parametrized types. In Scala, the Either type is often used as an alternative to scala.Option where Left represents failure (by convention) and Right is akin to Some.
Questions tagged [either]
341 questions
55
votes
4 answers
Using Either to process failures in Scala code
Option monad is a great expressive way to deal with something-or-nothing things in Scala. But what if one needs to log a message when "nothing" occurs? According to the Scala API documentation,
The Either type is often used as an
alternative to…

Alexander Azarov
- 12,971
- 2
- 50
- 54
52
votes
2 answers
Throwing exceptions in Scala, what is the "official rule"
I'm following the Scala course on Coursera.
I've started to read the Scala book of Odersky as well.
What I often hear is that it's not a good idea to throw exceptions in functional languages, because it breaks the control flow and we usually return…

Sebastien Lorber
- 89,644
- 67
- 288
- 419
43
votes
4 answers
Understanding how Either is an instance of Functor
In my free time I'm learning Haskell, so this is a beginner question.
In my readings I came across an example illustrating how Either a is made an instance of Functor:
instance Functor (Either a) where
fmap f (Right x) = Right (f x)
fmap f…

MarcoS
- 13,386
- 7
- 42
- 63
43
votes
3 answers
Using Eithers with Scala "for" syntax
As I understand it, Scala "for" syntax is extremely similar to Haskell's monadic "do" syntax. In Scala, "for" syntax is often used for Lists and Options. I'd like to use it with Eithers, but the necessary methods are not present in the default…

Dan Burton
- 53,238
- 27
- 117
- 198
42
votes
9 answers
Best way to turn a Lists of Eithers into an Either of Lists?
I have some code like the below, where I have a list of Eithers, and I want to turn it into an Either of Lists ... in particular (in this case), if there are any Lefts in the list, then I return a Left of the list of them, otherwise I return a Right…

Eric Bowman - abstracto -
- 1,915
- 1
- 14
- 24
37
votes
3 answers
Idiomatic error handling in Clojure
When I put on my C hat, I think that maybe idiomatic Clojure just does the simple thing and checks return values.
When I put on my Java hat (reluctantly, I must add), I think to myself that since Clojure runs on the JVM the natural way must be to…

Emil Eriksson
- 2,110
- 1
- 21
- 31
35
votes
4 answers
Method parameters validation in Scala, with for comprehension and monads
I'm trying to validate the parameters of a method for nullity but i don't find the solution...
Can someone tell me how to do?
I'm trying something like this:
def buildNormalCategory(user: User, parent: Category, name: String, description: String):…

Sebastien Lorber
- 89,644
- 67
- 288
- 419
33
votes
3 answers
Convert Option to Either in Scala
Suppose I need to convert Option[Int] to Either[String, Int] in Scala. I'd like to do it like this:
def foo(ox: Option[Int]): Either[String, Int] =
ox.fold(Left("No number")) {x => Right(x)}
Unfortunately the code above doesn't compile and I need…

Michael
- 41,026
- 70
- 193
- 341
33
votes
4 answers
Getting Value of Either
Besides using match, is there an Option-like way to getOrElse the actual content of the Right or Left value?
scala> val x: Either[String,Int] = Right(5)
scala> val a: String = x match {
case Right(x) => x.toString
…

Kevin Meredith
- 41,036
- 63
- 209
- 384
31
votes
9 answers
How to extract Left or Right easily from Either type in Dart (Dartz)
I am looking to extract a value easily from a method that return a type Either.
I am doing some tests but unable to test easily the return of my methods.
For example:
final Either result = await…

Maurice
- 2,129
- 2
- 25
- 33
29
votes
9 answers
How to split a List[Either[A, B]]
I want to split a List[Either[A, B]] in two lists.
Is there a better way ?
def lefts[A, B](eithers : List[Either[A, B]]) : List[A] = eithers.collect { case Left(l) => l}
def rights[A, B](eithers : List[Either[A, B]]) : List[B] = eithers.collect {…

Yann Moisan
- 8,161
- 8
- 47
- 91
25
votes
3 answers
Why Do We Need Maybe Monad Over Either Monad
I was playing around Maybe and Either monad types (Chaining, applying conditional functions according to returned value, also returning error message which chained function has failed etc.). So it seemes to me like we can achieve same and more…

altayseyhan
- 735
- 1
- 7
- 18
25
votes
4 answers
Mapping over Either's Left
Somewhere in my app I receive an Either ParserError MyParseResult from Parsec. Downstream this result gets some other parsing done over using other libs. During that second phase of parsing there also may occur some kind of error which I would like…

Nikita Volkov
- 42,792
- 11
- 94
- 169
23
votes
5 answers
Scala Either map Right or return Left
Is it possible to handle Either in similar way to Option? In Option, I have a getOrElse function, in Either I want to return Left or process Right. I'm looking for the fastest way of doing this without any boilerplate like:
val…

Michał Jurczuk
- 3,728
- 4
- 32
- 56
23
votes
1 answer
How map only left value from scala Either?
Consider a code:
val some: OneCaseClass Either TwoCaseClass = ???
val r = some.left.map(_.toString)
Why is r Serializable with Product with Either[String, TwoCaseClass] type instead of Either[String, TwoCaseClass]?
How to map only left value?

Cherry
- 31,309
- 66
- 224
- 364