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
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
  • 2,485
  • 8
  • 47
  • 77
7
votes
1 answer

Handle errors with union

I came to C from high-level Scala language and came up with a question. In Scala we usually handle error/exceptional conditions using Either which looks like the following: sealed abstract class Either[+A, +B] extends Product with Serializable So…
Some Name
  • 8,555
  • 5
  • 27
  • 77
7
votes
1 answer

C# FP: Validation and execution with error handling functional way - space for improvement?

I'm new to functional way of thinking in C# (well... not limited to language). Let's say there's method: T LoadRecord(int id) Concept 1. Validation When invalid input is given, I should return something like Either,…
Zdeněk
  • 929
  • 1
  • 8
  • 25
7
votes
2 answers

why can't I extract a tuple from Either projection inside for comprehension using pattern matching?

Why does this work: val somePair: Option[(String,String)] = Some(("John", "Doe")) (for { pair <- somePair.toRight("Hello unknown!").right } yield s"Hello ${pair._1} ${pair._2}!").merge But this doesn't: val somePair: Option[(String,String)] =…
shayan
  • 1,211
  • 9
  • 12
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
  • 570
  • 3
  • 18
7
votes
2 answers

Accumulating errors with EitherT

I have the following little mini-sample application of a web API that takes a huge JSON document and is supposed to parse it in pieces and report error messages for each of the pieces. Following code is a working example of that using EitherT (and…
Fredrik
  • 4,161
  • 9
  • 28
  • 31
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
  • 72,246
  • 9
  • 135
  • 180
7
votes
1 answer

Scala.Either getOrElse method

Why when I type this all works fine? Right(2).left getOrElse Right(4).left getOrElse Left("Error") but when I type this compilation fails? Right[String, Int](2).left getOrElse Right[String, Int](4).left getOrElse Left[String,…
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
7
votes
3 answers

Scala.Either orElse method

What is idiomatic way to work with Either's in Scala? For example when using Option I can use orElse method to get next optional value if current is None. But how to work with Either in same way? I didn't find method like orElse to chain Either's…
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
7
votes
1 answer

Either, Options and for comprehensions

I was coding a for comprehension, and wondered something: def updateUserStats(user: User): Either[Error,User] = for { stampleCount <- stampleRepository.getStampleCount(user).right userUpdated <- Right(copyUserWithStats(user,stampleCount)).right…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
7
votes
2 answers

Scala polymorphic function for filtering an input List of Either

Seeking a more elegant solution I have this piece of code, I just use it in test cases where it isn't necessary to do any error handling. What it does is: take an input list of strings parse them using the DSJSonmapper.parseDSResult method filters…
Bryan Hunt
  • 3,685
  • 2
  • 24
  • 36
6
votes
4 answers

How to implement a generic Either type in Go?

With the new generics in Go 1.18, I thought it might be possible to create a 'Either[A,B]' type that can be used to express that something could be either of type A or type B. A situation where you might use this is in situations where a function…
Kris
  • 3,898
  • 1
  • 23
  • 32
6
votes
3 answers

A way to generalize Haskell's Either type for arbitrarily many types?

I am creating a turn based game. I want to define a datatype that encodes one type out of many possible types. Here is the motivating example: I have defined a Turn type using GADTs, so the type of each value of Turn a says something about it's…
6
votes
1 answer

Correct usage of Either, Try and Exceptions/ControlThrowable in scala

In my scala code (libraries as well as applications) I currently use a mixture of Option and Try, whenever either of both feels more apropriate. I tend to implement "doSomething"-methods which can succeed with a return value or with a failure with…
user826955
  • 3,137
  • 2
  • 30
  • 71
6
votes
1 answer

For comprehensions with kotlin and arrow-kt library

I use arrow-kt library and I'm trying to employ Either and IO within the same for comprehension. Say I have the next piece of code: IO.monad().binding { val ans: Either = someFunctionThatReturnsEitherWrappedInIO().bind() } Now, I'd…
MaxG
  • 1,079
  • 2
  • 13
  • 26