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
6
votes
3 answers
How can I convert a list of Either to a list of Right values?
I do some data conversion on a list of string and I get a list of Either where Left represents an error and Right represents a successfully converted item.
val results: Seq[Either[String, T]] = ...
I partition my results with:
val (errors, items)…

Christian Rodemeyer
- 2,001
- 1
- 19
- 22
6
votes
2 answers
Future[List[Error \/ Double]] to Future[[List[Error] \/ List[Double]] in Scala
I'm playing around with Scala(z) to learn functionnal programming.
I have a value of type Future[List[Error \/ Double]] and want to transform it to something with the type Future[[List[Error] \/ List[Double]].
The goal is to group the lefts and…

DennisVDB
- 1,347
- 1
- 14
- 30
6
votes
2 answers
Why does Either derives Show but Maybe does not?
Documentation of both Either and Maybe indicate that they have instances of Show.
Either is defined as deriving Show, simply :
data Either a b = Left a | Right b
deriving (Eq, Ord, Read, Show, Typeable)
Yet, Maybe does not :
data Maybe a = …

gxtaillon
- 1,016
- 1
- 19
- 33
6
votes
4 answers
Is it possible to change the monad type in a monadic sequence?
I know it's possible to change the wrapped type, so that you can have
f :: (a -> m b)
g :: (b -> m c)
f >>= g :: (a -> m c)
but is it possible to change m? If m is a MonadError and is implemented both by an Either ErrorA and Either ErrorB, can i…

BruceBerry
- 1,166
- 1
- 9
- 21
5
votes
1 answer
Either a b. Different Hoogle results after two years?
I am following a video that was recorded about two years ago. The speaker enters Either a b -> IO b in the public Hoogle search input and the result contains (among others):
either :: (a -> c) -> (b -> c) -> Either a b -> c
If I try it today, this…

Marco Faustinelli
- 3,734
- 5
- 30
- 49
5
votes
1 answer
Implementing Either Monad in Typescript
I've been experimenting with TypeScript lately, and I'm trying to implement some basic monads. I've already got a reasonably functioning Maybe (with the same methods of my Either below, at least), but Either is eluding me for type-related reasons I…

M-N
- 621
- 7
- 20
5
votes
2 answers
EitherT: Call function returning Either only if a certain condition is true (otherwise return right)
So I have a certain function which I need to call only if a certain condition is true. If it's false, I consider it as Right.
I would use EitherT.cond, but the thing is my function's return type is Future[Either[ErrorType, Unit]], so it's not…

Anastasia
- 53
- 5
5
votes
1 answer
Cats: mapping over tuples with the same applicative
Let's say I have:
val x1: Either[String, Int] = Right(1)
val x2: Either[String, Float] = Left("Nope")
val x3: Either[String, Double] = Left("Not Today")
I want to combine these together and get a Either[NonEmptyList[String], (Int, Float, Double)].…

Maths noob
- 1,684
- 20
- 42
5
votes
4 answers
How to test Either from Arrow in functional style
I would like to test the obtained result using Either. Let's assume I have a simple example without Either
@Test
fun `test arithmetic`() {
val simpleResult = 2 + 2
Assertions.assertEquals(4, simpleResult)
}
And now i have wrapped…

Bukharov Sergey
- 9,767
- 5
- 39
- 54
5
votes
3 answers
Generic sequence of Either to Either of sequence with preserved type
I would like to have a function that converts any iterable type C[_] of Either[A, B] to Either[C[A], C[B]].
I got it working but I used asInstanceOf method and I feel like this approach can fail in some scenario (I don't yet know what scenerio that…

bottaio
- 4,963
- 3
- 19
- 43
5
votes
2 answers
What is an idiomatic way to filter out Either Left in an akka stream?
I have a stream having a huge bunch of either values in it. I am looking for an itiomatic way to filter out the Either-Left and map on the Either-Right. I want to avoid something like
final case class Foo(x: Either[String, Int], y:Int)
val foos =…

Stoecki
- 585
- 1
- 3
- 16
5
votes
2 answers
Chain asynchronous operations that return Either using Language-Ext in C#
I am using the Language-Ext library for C# and I am trying to chain asynchronous operations that return an Either type. Let's say that I have three functions that would return an integer if they succeed and a string if the fail, and another function…

Carlos Rodriguez
- 523
- 1
- 5
- 13
5
votes
2 answers
List[String] does not have a member traverse from cats
I am attempting to convert a List[Either[Int]] to anEither[List[Int]] using traverse from cats.
Error
[error] StringCalculator.scala:19:15: value traverseU is not a member of List[String]
[error] numList.traverseU(x => {
Code
import…

vamsiampolu
- 6,328
- 19
- 82
- 183
5
votes
2 answers
How to turn `Either[Error, Option[Either[Error, Account]]]` to `Either[Error, Option[Account]]` with typelevel cats?
I'm using cats, wonder how to turn a data with it.
From
val data = Either[Error, Option[Either[Error, Account]]]
to
val target: Either[Error, Option[Account]] = howToConvert(data)
If there is any Error happens, the result will be Left(error) with…

Freewind
- 193,756
- 157
- 432
- 708
5
votes
1 answer
How to convert a `NonEmptyList[Either[Error, User]]` to `Either[Error, NonEmptyList[User]]` with cats?
I'm using cats, wonder how to turn a data with it:
val data = NonEmptyList[Either[Error, User]]
to
val target: Either[Error, NonEmptyList[User]] = howToConvert(data)

Freewind
- 193,756
- 157
- 432
- 708