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.

341 questions
5
votes
2 answers

Either for error handling in java

My new workplace is heavily using functional java Either for its error handling (http://www.functionaljava.org/javadoc/4.5/functionaljava/fj/data/Either.html). exceptions are almost not used at all. This is very annoying imo for many reasons. To…
yaarix
  • 490
  • 7
  • 18
5
votes
1 answer

How do I collapse an Either in PureScript?

I have an object of type Either String (Either String Int). I would like to collapse it to an object of type Either String Int. Is there a provided function for this in PureScript?
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
5
votes
2 answers

Scala Either : simplest way to get a property that exists on right and left

I have a template using a valueObject that might be one of two flavours depending on where it is used in our app. So I am importing it as an Either: valueObject: Either[ ObjectA, ObjectB ] Both objects have an identically named property on them…
xeno.be
  • 91
  • 1
  • 10
5
votes
4 answers

Future[Either[A, B]] to Future[Either[A, C]] using a (B => Future[C]) function

I've got a Future[Either[A, B]] and a function providing a Future[C] from a B. I need to transform the Future[Either[A, B]] to Future[Either[A, C]]. Is there a direct way to get the Future[Either[A, C]] and not a Future[Either[A, Future[C]]]? I'm…
Alban Dericbourg
  • 1,616
  • 2
  • 16
  • 39
5
votes
2 answers

How to avoid pyramid of cases?

I have code structured like the example below. I'm pretty sure there should be a way to structure it much more sanely. I would assume Either (or Error) monad could help, but I don't know where to start with that. Any pointers to get me going in the…
Łukasz
  • 35,061
  • 4
  • 33
  • 33
5
votes
4 answers

What is the name for the contrary of Tuple or Either with more than two options?

There is a Tuple as a Product of any number of types and there is an Either as a Sum of two types. What is the name for a Sum of any number of types, something like this data Thing a b c d ... = Thing1 a | Thing2 b | Thing3 c | Thing4 d | ... Is…
ais
  • 2,514
  • 2
  • 17
  • 24
5
votes
1 answer

Is Scala Either really a Monad

I was wondering if scala Either is really a Monad in Category Theory sense?. I know that Monads should have bind and return methods. What is Either's bind then?
goral
  • 1,275
  • 11
  • 17
5
votes
3 answers

Using returned EitherT in haskell program

I'm trying to use the "citation-resolve" package in a Haskell project I'm working on, but I'm having trouble getting my head around using EitherT's in real code. I get that they're monad transformers, and I think I understand what that means,…
AdamHarries
  • 335
  • 2
  • 11
5
votes
2 answers

Why this Either-monad code does not type check?

instance Monad (Either a) where return = Left fail = Right Left x >>= f = f x Right x >>= _ = Right x this code frag in 'baby.hs' caused the horrible compilation error: Prelude> :l baby [1 of 1] Compiling Main (…
pf_miles
  • 907
  • 1
  • 8
  • 17
4
votes
4 answers

Filtering a set of Either[x, y], with logging

Given a Set[Either[BadObject, GoodObject]], I'd like to convert it into a Set[GoodObject], while logging all the BadObjects. The problem I am having is that when I try to add logging in a collect call, like: someMethodThatReurnsTheSet.collect { …
TheDude
  • 361
  • 4
  • 13
4
votes
1 answer

Handling multiple error types via either/disjunction Scala

I have 3 distinct modules each with its own error type. Following is a very simplified version. object ModuleA { case class ErrorA(msg: String) def getA: ErrorA \/ String = "1".right } object ModuleB { case class ErrorB(msg: String) def…
4
votes
1 answer

How to yield all errors (failures) with Either

I am completely new to Scala. AFAIK, Either encapsulate failure handling allowing chain operations without writing boilerplate code repeatedly. It allows also circuit break the continuation of execution. But this may not always what I want. e.g. for…
yi.han
  • 369
  • 4
  • 8
4
votes
1 answer

Haskell defining Functor instance for an alternative Either data type

Going through Typeclassopedia to gain some routing working with type classes. Want to make an alternative to Either an instance of Functor, but even examining the definition of Either as an instance of Functor keeps getting me in trouble. Have…
Madderote
  • 1,107
  • 10
  • 19
4
votes
1 answer

Can I build something like a lens when my getter and setter return `Either`?

In brief My getter and setter could both fail, with messages describing how. Therefore they return Either String, which means I can't make lenses out of them in the normal way. In detail Consider these types: import qualified Data.Vector as V data…
Jeffrey Benjamin Brown
  • 3,427
  • 2
  • 28
  • 40
4
votes
1 answer

Scala - Define type for Either for compactness or write it explicitly for readability?

In Scala, I can have: trait Api { def someApiCall: Either[Failure, GoodResult]; } or object SomeObject { type SomeResult = Either[Failure, GoodResult] } trait Api { def someApiCall: SomeObject.SomeResult; } where the former is more…
ron
  • 9,262
  • 4
  • 40
  • 73