either Questions
5
I am relatively new to Scala Test, and so I consulted the documentation on how to test on Either values.
I tried to replicate the instructions like that:
import org.scalatest.EitherValues
import or...
5
Solved
Besides using match, is there an Option-like way to getOrElse the actual content of the Right or Left value?
scala> val x: Either[String,Int] = Right(5)
scala> val a: String = x match {
ca...
3
Solved
I am a Haskell newbie and I wonder why there is no alternative instance for Either but a semigroup, which behaves as I would expect it from alternative:
instance Semigroup (Either a b) where
Left ...
Maidstone asked 10/6, 2017 at 10:1
4
Solved
Suppose I need to convert Option[Int] to Either[String, Int] in Scala. I'd like to do it like this:
def foo(ox: Option[Int]): Either[String, Int] =
ox.fold(Left("No number")) {x => Right(x)}
...
Stipe asked 10/1, 2016 at 14:19
4
Solved
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, sim...
Barger asked 13/2, 2019 at 9:1
9
Solved
I am looking to extract a value easily from a method that return a type Either<Exception, Object>.
I am doing some tests but unable to test easily the return of my methods.
For example:
...
Ganglion asked 6/11, 2019 at 15:55
2
Solved
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 t...
4
Solved
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...
Cud asked 9/4, 2022 at 17:33
9
Solved
I want to split a List[Either[A, B]] in two lists.
Is there a better way ?
def lefts[A, B](eithers : List[Either[A, B]]) : List[A] = eithers.collect { case Left(l) => l}
def rights[A, B](eithe...
3
I'm trying to read a list of objects from the database and mapping it to another type of list.
// Returns either a Failure or the expected result
suspend fun getCountries(): Either<Failure, Lis...
3
Solved
Let's say I have this (arguably mislead) piece of code laying around:
import System.Environment (getArgs)
import Control.Monad.Except
parseArgs :: ExceptT String IO User
parseArgs =
do
args <...
Conn asked 4/1, 2016 at 9:55
3
Solved
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...
Lodestone asked 12/6, 2021 at 20:44
1
Solved
This is probably a very basic Haskell question, but let's assume the following function signatures
-- helper functions
getWeatherInfo :: Day -> IO (Either WeatherException WeatherInfo)
craftQuer...
Thermodynamic asked 20/5, 2021 at 9:42
5
Solved
Is it possible to handle Either in similar way to Option? In Option, I have a getOrElse function, in Either I want to return Left or process Right. I'm looking for the fastest way of doing this wit...
2
Solved
I understand it would be difficult to change now without breaking existing code, but I'm wondering why it was done that way in the first place.
Why not just:
sealed trait Either[+A, +B]
case class ...
2
Solved
I am writing a small Scala Program which should:
Read a file (line by line) from a local FS
Parse from each line three double values
Make instances of a case class based on those three values
Pass...
9
Solved
I have some code like the below, where I have a list of Eithers, and I want to turn it into an Either of Lists ... in particular (in this case), if there are any Lefts in the list, then I return a ...
3
Solved
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]] = ....
1
Solved
I am fairly new to Haskell. I am trying to combine the State monad with error propagation by treating Either as a monad. I would like to recurse over an abstract syntax tree (for example, for writi...
Larcher asked 10/3, 2020 at 17:19
1
Solved
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 -> ...
1
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 exec...
Awe asked 13/9, 2019 at 13:44
2
Solved
Suppose I want to write a method with the following signature:
def parse(input: List[(String, String)]):
ValidationNel[Throwable, List[(Int, Int)]]
For each pair of strings in the input, it nee...
Artifice asked 19/11, 2013 at 7:42
2
Solved
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 ...
Mckale asked 15/7, 2019 at 14:19
4
Solved
Suppose I have few case classes and functions to test them:
case class PersonName(...)
case class Address(...)
case class Phone(...)
def testPersonName(pn: PersonName): Either[String, PersonName]...
Perrie asked 25/1, 2014 at 13:51
2
Solved
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 ...
1 Next >
© 2022 - 2025 — McMap. All rights reserved.