Questions tagged [either]

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.

284 questions
54
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,235
  • 2
  • 45
  • 50
51
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
  • 79,294
  • 59
  • 260
  • 386
42
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
  • 12,788
  • 5
  • 36
  • 61
41
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
  • 51,332
  • 25
  • 109
  • 190
38
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…
35
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
  • 1,971
  • 1
  • 19
  • 30
34
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
  • 79,294
  • 59
  • 260
  • 386
28
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
  • 37,415
  • 63
  • 167
  • 303
26
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
  • 38,251
  • 58
  • 190
  • 340
25
votes
8 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
  • 7,685
  • 6
  • 35
  • 83
23
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
  • 675
  • 1
  • 5
  • 15
22
votes
2 answers

Try[Result], IO[Result], Either[Error,Result], which should I use in the end

I'd like to know what should be the signature of my methods so that I handle different kind of failures elegantly. This question is somehow the summary of many questions I already had about error handling in Scala. You can find some questions…
Sebastien Lorber
  • 79,294
  • 59
  • 260
  • 386
22
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
  • 41,289
  • 10
  • 85
  • 162
21
votes
1 answer

Why `scala.util.Try` is not mentioned in chapter "Handling errors without exceptions" of book "functional programming in Scala"?

In the chapter "Handling errors without exceptions" of book "functional programming in Scala", the author gives: The problem of throwing exceptions from the body of a function Use Option if we don't care about the actual exception Use Either if we…
Freewind
  • 177,284
  • 143
  • 381
  • 649
20
votes
1 answer

Layering State with Either in scalaz

In Integrating State with Either (slide 88), given the pattern of State layered under Either, is there a recommended approach for adding another type of state, e.g., logging via something like Writer? It seems the new state has to live between the…
Sim
  • 11,689
  • 7
  • 57
  • 85
1
2 3
18 19