either

Scala - Futures and Eithers

元气小坏坏 提交于 2019-12-13 06:38:51
问题 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 <- userDao.findUser(userId) user <- userRes.withFailure(UserNotFound).right authRes <- userDao.authenticate(user) auth <- authRes.withFailure(NotAuthenticated).right goodRes <- goodDao.findGood(goodId) good <- goodRes.withFailure(GoodNotFound).right checkedGood <- checkGood

Efficient and/or idiomatic way to turn Seq[Either[String, Int]] to (Seq[String], Seq[Int])

余生长醉 提交于 2019-12-12 17:32:14
问题 Slightly simplifying, my problem comes from a list of strings input that I want to parse with a function parse returning Either[String,Int] . Then list.map(parse) returns a list of Either s. The next step in the program is to format an error message summing up all the errors or passing on the list of parsed integers. Lets call the solution I'm looking for partitionEithers . Calling partitionEithers(List(Left("foo"), Right(1), Left("bar"))) Would give (List("foo", "bar"),List(1)) Finding

Accumulating errors with EitherT

大兔子大兔子 提交于 2019-12-12 05:23:08
问题 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 the errors package). However, the problem is that EitherT breaks the computation on the first Left encountered and just returns the first "error" it sees. What I would like is a list of error messages, all that are possible to produce. For instance,

haskell Either String (NestedList a)- why doesn't it work

南笙酒味 提交于 2019-12-12 01:05:39
问题 I am trying to append function that works on Nested Lists like regular lists. I want to use Either String (Nested a) so that it returns error or the appended list. But it keeps failing. I am not doing NestedList[NestedList a] anywhere. Why does it say it expected [NestedList (NestedList a)] module Main where data NestedList a=Elem a | List[NestedList a] deriving Show flatten ::NestedList a->[a] flatten (Elem x)=[x] flatten (List(x:xs))=flatten(x)++flatten(List xs) --flatten NestedList (x:xs)

Either-Or constraints in LPsolveAPI

我怕爱的太早我们不能终老 提交于 2019-12-11 06:24:01
问题 I'm currently writing a MIP in LPsolveAPI in R. The program itself is straightforward, but I can't find a way to write an either-or constraint without being able to directly call a new binary variable or the binary values on the lhs. Does LPsolveAPI not support this or am I missing something obvious? 回答1: The use of new binary variables is the standard way to model either-or-constraints in lp_solve. (LpSolveAPI is based on the lp_solve solver.). So you are not missing anything obvious. That

Scala: Combine Either per the whole List with Either per elements

匆匆过客 提交于 2019-12-10 18:47:32
问题 I have a list of Either, which represents error: type ErrorType = List[String] type FailFast[A] = Either[ErrorType, A] import cats.syntax.either._ val l = List(1.asRight[ErrorType], 5.asRight[ErrorType]) If all of them are right, I want to get a list of [A], in this case - List[Int] If any Either is left, I want to combine all errors of all either and return it. I've found a similar topic at [How to reduce a Seq[Either[A,B]] to a Either[A,Seq[B]] But it was quite long ago. For instance, one

How to compose Future of Either/Disjunction in Scala

岁酱吖の 提交于 2019-12-10 18:46:03
问题 Suppose I have the following functions to compose: val mayFail1: Int => Error \/ Int = ??? val slowAndMayFail: Int => Error \/ String = ??? val mayFail2: String => Error \/ Int = ??? val mayFail3: String => Error \/ Int = ??? val mayFail: Int => Error \/ Int = {x => for { x1 <- mayFail1(x) s <- slowAndMayFail(x1) x2 <- mayFail2(s) x3 <- mayFail3(x2) } yield x3 } The function mayFail is slow because of slowAndMayFail , so I would like it to return a future of Error \/ Int . The straightforward

Haskell defining Functor instance for an alternative Either data type

你离开我真会死。 提交于 2019-12-10 14:23:59
问题 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 this, but will not compile. data Alt a b = Success a | Failure b deriving (Show, Eq, Ord) instance Functor (Alt a) where fmap _ (Failure a) = Failure a fmap f (Success x) = Success (f x) • Couldn't match expected type ‘a1’ with actual type ‘a’ ‘a1’ is

Either monadic operations

依然范特西╮ 提交于 2019-12-10 10:36:43
问题 I just start to be used to deal with monadic operations. For the Option type, this Cheat Sheet of Tony Morris helped: http://blog.tmorris.net/posts/scalaoption-cheat-sheet/ So in the end it seems easy to understand that: map transforms the value of inside an option flatten permits to transform Option[Option[X]] in Option[X] flatMap is somehow a map operation producing an Option[Option[X]] and then flattened to Option[X] At least it is what I understand until now. For Either, it seems a bit

Handling failures with Either -> Where is the stacktrace?

耗尽温柔 提交于 2019-12-10 02:26:34
问题 I heard from some people that in Scala we tend (like other functional languages) to not break the control flow... Instead by convention we return the error in an Either Left . But how do we get the stracktrace from that exception? For now i return in the Left a simple Error case class with a code, message and cause ( Error too). But if i have an error, i can't get the stacktrace. If my application become complexe it may be hard to find the code block that returned that Error ... The root