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
9
votes
1 answer

Handling failures with Either -> Where is the stacktrace?

I heard from some people that in Scala we tend (like other functional languages) to not break the control flow... Instead by convention we return the error in an Either Left. But how do we get the stracktrace from that exception? For now i return in…
Sebastien Lorber
  • 79,294
  • 59
  • 260
  • 386
9
votes
2 answers

Catching errors thrown with `error`?

There are some stdlib functions that throw errors on invalid input. For example: Prelude> read "1o2" :: Int *** Exception: Prelude.read: no parse I would like to wrap it to return a Either e a instead. How can I do that?
missingfaktor
  • 86,952
  • 56
  • 271
  • 360
8
votes
5 answers

Can I ask an Either whether it is Left (or Right)?

I know I can usually just pattern match, but sometimes I would find these functions useful: isLeft = either (const True) (const False) isRight = either (const False) (const True) Is there something like that in the standard library?
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
8
votes
1 answer

Reduce nestedness when using successive Either/Maybe

This is probably a very basic Haskell question, but let's assume the following function signatures -- helper functions getWeatherInfo :: Day -> IO (Either WeatherException WeatherInfo) craftQuery :: WeatherInfo -> Either QueryException…
Jivan
  • 16,401
  • 7
  • 56
  • 89
8
votes
4 answers

Using 'Either' in Haskell

I have two values, t1 and t2, of type Either String Type. The Left-value is used for error handling. These values are used in a function which returns Either String Type. What I want to do is check if both t1 and t2 are Right-values and satisfy p ::…
Viktor Dahl
  • 1,804
  • 2
  • 23
  • 35
8
votes
5 answers

How to convert functions raising exceptions to functions returning Either?

Suppose I have a few functions that raise exceptions. I am wrapping them to return Either[Throwable, ]. (Let's assume I need Either rather than Try). def fooWrapper(arg1: FooArg1, arg2: FooArg2) = try Right(foo(arg1, arg2))…
Michael
  • 37,415
  • 63
  • 167
  • 303
8
votes
2 answers

How does EitherT work?

I spend half of my day trying to figure out how to use EitherT as a way to deal with errors in my code. I have defined a transformer stack like this. -- Stuff Monad data StuffConfig = StuffConfig { appId :: T.Text, appSecret ::…
fho
  • 6,578
  • 19
  • 63
8
votes
2 answers

Applicative Functors and Left from Either

I have been working through the great good book, but I am struggling slightly with Applicative Functors. In the following example max is applied to the contents of the two Maybe functors and returns Just 6. max <$> Just 3 <*> Just 6 Why in the…
Jim Jeffries
  • 9,051
  • 13
  • 57
  • 99
8
votes
3 answers

Reducing Iterable[Either[A,B]] to Either[A, Iterable[B]]

I need to reduce an Iterable[Either[Throwable, String]] to an Either[Throwable, Iterable[String]]. I don't know if this operation is pretty common or not, haven't found nothing on the Iterable trait. So I have written this function: def reduce[A,…
Filippo De Luca
  • 664
  • 4
  • 21
8
votes
3 answers

Chaining method calls with Either

I'd like to know if it is possible to create some kind of "method call chain", with all methods returning the same Either[Error,Result]. What i'd like to do is: call all the methods successively, and when method returns a Left(Error), then stop the…
Sebastien Lorber
  • 79,294
  • 59
  • 260
  • 386
7
votes
2 answers

How to initialize an Either to Right and specify the type of Left?

I would like to initialize an Either to Left, but this requires specifying the type of the Right side (or vice versa). If I don't, then the Right side is typed to Nothing by default, and in order to do something such as: List(5, 42,…
Xavier Guihot
  • 32,132
  • 15
  • 193
  • 118
7
votes
4 answers

Pattern match on value of Either inside a for comprehension?

I have a for comprehension like this: for { (value1: String, value2: String, value3: String) <- getConfigs(args) // more stuff using those values } getConfigs returns an Either[Throwable, (Seq[String], String, String)] and when I try to…
covfefe
  • 1,819
  • 4
  • 26
  • 64
7
votes
3 answers

How to map (Either String (a -> b)) to (Either String [(a -> b)])

I try to find a solution on an exercise of my own, with the following requirements : We need to move an object against a given sequence. A sequence is composed of actions. Here are the possible actions : F, L, R F : move Forward L : rotate 90° to…
Maxime
  • 560
  • 3
  • 15
7
votes
2 answers

Pure error handling in Haskell with Either: how to fold with error possibility?

I'm mainly interested in the Either monad and all it's uilitites from Control.Error. Reading errors-1.0: Simplified error handling, I got convinced pure errors should be kept apart from IO errors. This means error, fail, exitFailure are functions…
Andras Gyomrey
  • 1,552
  • 11
  • 33
7
votes
1 answer

Is there a standard name or implementation of the "purely applicative Either"?

I frequently find use for what I call the "purely applicative Either", i.e. Either with the Applicative instance available so long as we don't implement a Monad instance as well. newtype AEither e a = AEither { unAEither :: Either e a } deriving…
J. Abrahamson
  • 64,404
  • 8
  • 128
  • 172
1 2
3
18 19