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
0
votes
2 answers

How to write it idiomatically in Scala?

Suppose I have a function to check a string: case class MyError(msg: String) val oops = MyError("oops") def validate(s: String):Either[MyError, Unit] = if (s == "a") Right(()) else Left(oops) Now I would like to reuse it and write a new…
Michael
  • 41,026
  • 70
  • 193
  • 341
0
votes
2 answers

De-referencing Either back into the object

I'm trying to use an Either return type from my function to either get back an object or a string. In the case it is an object, I'd like to start calling methods from this object. In the case it is a string, I'd like to call some other functions…
shutch
  • 197
  • 1
  • 1
  • 10
0
votes
1 answer

Simple way to Narrow down types over Left and Right on Either

Suppose you have the following Either[A1,B2] instance where A1 and B2 are maped to match Either[A,B], where A and B have a common subtype S. Is there a way to easily narrow down the types A and B to S?
isaias-b
  • 2,255
  • 2
  • 25
  • 38
0
votes
1 answer

How to use return value from function returning Future(Either[A, B]) in scala?

I have the below function. def get(id: Int): Future[Either[String, Item]] = { request(RequestBuilding.Get(s"/log/$id")).flatMap { response => response.status match { case OK => Unmarshal(response.entity).to[Item].map(Right(_)) …
Priya R
  • 451
  • 4
  • 14
0
votes
2 answers

How to extract a Either's Right and keep information about its Left in case of error in Haskell?

I am trying to extract the value in the Right constructor of a Either value, while giving an error if the Either in question is actually a Left (i.e. an error). The answers in Either Right Left how to read value gives me something like: fromRight e…
thor
  • 21,418
  • 31
  • 87
  • 173
0
votes
2 answers

Use of Either and returning the error immediately

I have a function which returns an instance of Either where the Left side represent the exception / error, while the second side stores the return value. If the Either instance has been Left instantiated to the Error branch I want to return…
akaphenom
  • 6,728
  • 10
  • 59
  • 109
0
votes
1 answer

Dealing with Future[Either] Types in Scala

I'm a bit struggling to get this structured. Here is what I'm trying to do: def checkResultAndFetchUser(result: WriteResult, encryptedEmail: String): Future[Either[ServiceError, User]] = Future { if (result.code contains 11000) …
joesan
  • 13,963
  • 27
  • 95
  • 232
0
votes
2 answers

Idiomatic Scala way to come a list of Eithers into a Left or Right depending on the lists content

I have a list of Eithers val list: List[Either[String, Int]] = List(Right(5), Left("abc"), Right(42)) As a result I want a Right if everything in the list is a Right else I want a Left. This sounds like the list should be biased (e.g. use Try…
valenterry
  • 757
  • 6
  • 21
0
votes
1 answer

Scala - Futures and Eithers

this thread gave me an idea how to structure my code: Scala-way to handle conditions in for-comprehensions? The part in question: // First create the JSON val resultFuture: Future[Either[Failure, JsResult]] = for { userRes <-…
Tim Joseph
  • 847
  • 2
  • 14
  • 28
0
votes
1 answer

scoobi concatenate DLists issue

I have a scala scoobi code (that i execute locally for now) of the following structure: def run() = { val myvalue1 = processSource.source1("mypath/inputData/", references) // has several subfolders with input files that should be processed and…
dadam
  • 181
  • 1
  • 1
  • 6
0
votes
1 answer

Short circuiting a list of functions returns \/

So I have an expensive method with this signature def func(param: Int): \/[String, Int] I am trying to loop over a list of params and returns \/[String, List[Int]] but stop the loop whenever the method returns -\/. I come up with this: scala> def…
Chikei
  • 2,104
  • 1
  • 17
  • 21
0
votes
2 answers

How can I make analog of Either?

Compiler complains at Left(e): Expression of type Left(List[ServiceError, Nothing]) doesn't conform to expected type Either[E , R] sealed trait ServiceResult[+E <: List[ServiceError], +R ] { def toEither: Either[E , R] = this match { …
Deil
  • 492
  • 4
  • 14
0
votes
3 answers

Regex Challenge - either ... or

I havent been able to figure this one out. I need to match all those strings by matching whole and its surroundings underscores (in one regex statement): whole_anything anything_whole anything_whole_anything but it must NOT match…
Lukáš Řádek
  • 1,273
  • 1
  • 11
  • 23
0
votes
2 answers

Scala: Either with parameterized function type

Should Either work with parameterized functions? case class FunOrSeq[T1, T2](e: Either[Function1[T1, T2], Iterable[T1]]) def f: Int => Int = x => x scala> FunOrSeq(Left(f)) :11: error: type mismatch; found : scala.util.Left[Int =>…
Austin
  • 1,122
  • 2
  • 10
  • 27
0
votes
1 answer

Error handling with Either in Scala

This is a followup to my previous question. Suppose I have the following functions: type Result[A] = Either[String, A] // left is an error message def f1(a: A): Result[B] = ... def f2(b: B): Result[C] = ... def f3(c: C): Result[D] = ... def f(a:…
Michael
  • 41,026
  • 70
  • 193
  • 341
1 2 3
22
23