either

Validation versus disjunction

亡梦爱人 提交于 2020-01-09 04:22:10
问题 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 needs to verify that both members can be parsed as integers and that the first is smaller than the second. It then needs to return the integers, accumulating any errors that turn up. First I'll define an error type: import scalaz._, Scalaz._ case class InvalidSizes(x: Int, y: Int) extends Exception( s

Error context in error handling in Scala

心不动则不痛 提交于 2020-01-05 12:31:11
问题 Suppose I need to call a remote JSON/HTTP service. I make a JSON request, send it by HTTP to the server, and receive and parse the JSON response. Suppose I have data type MyError for errors and all my functions return Either[MyError, R] type Result[A] = Either[MyError, A] def makeJsonRequest(requestData: RequestData): Result[String] = ... def invoke(url: URL, jsonRequest: String): Result[String] = ... def parseJsonResponse(jsonResponse: String): Result[ResponseData] = ... I can combine them

Error context in error handling in Scala

拥有回忆 提交于 2020-01-05 12:31:05
问题 Suppose I need to call a remote JSON/HTTP service. I make a JSON request, send it by HTTP to the server, and receive and parse the JSON response. Suppose I have data type MyError for errors and all my functions return Either[MyError, R] type Result[A] = Either[MyError, A] def makeJsonRequest(requestData: RequestData): Result[String] = ... def invoke(url: URL, jsonRequest: String): Result[String] = ... def parseJsonResponse(jsonResponse: String): Result[ResponseData] = ... I can combine them

Combining Futures (Twitter) and Either in Scala

亡梦爱人 提交于 2020-01-02 06:08:43
问题 We use Twitter futures (as part of the Finagle stack) and I don't like the concept of using (business) exceptions to control the flow of our application, because exceptions don't show up in method signatures. So I had the idea to use Future[Either[A,B]] as a replacement. But I have some problems in using for comprehensions over futures with this concept: E.g. we have a repository method: def getUserCredentialsByNickname(nickname: String): Future[Either[EntityNotFound, UserCredentials]] and a

Haskell - Maybe Either

你说的曾经没有我的故事 提交于 2019-12-31 04:14:07
问题 -- | Convert a 'Maybe a' to an equivalent 'Either () a'. Should be inverse -- to 'eitherUnitToMaybe'. maybeToEitherUnit :: Maybe a -> Either () a maybeToEitherUnit a = error "Not yet implemented: maybeToEitherUnit" -- | Convert a 'Either () a' to an equivalent 'Maybe a'. Should be inverse -- to 'maybeToEitherUnit'. eitherUnitToMaybe :: Either () a -> Maybe a eitherUnitToMaybe = error "Not yet implemented: eitherUnitToMaybe" -- | Convert a pair of a 'Bool' and an 'a' to 'Either a a'. Should be

Accumulating errors with EitherT

穿精又带淫゛_ 提交于 2019-12-30 04:50:10
问题 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,

Traversing Either in Scala

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 19:46:33
问题 I wrote the following simple code: import cats.effect.IO import cats.instances.either._ import cats.syntax.TraverseSyntax object Test extends App with TraverseSyntax{ val e: Either[String, IO[Int]] = Right(IO(2)) e.sequence //error here } Unfortunately it refuses to compile with the Error:(25, 94) value sequence is not a member of scala.util.Either Can you please explain why? I imported either instances which include Traverse[Either[A, ?]] . What's wrong? 回答1: Traverse[F] is defined as a

How to transform Either[Future[A], Future[B]] to Future[Either[A, B]]

孤街醉人 提交于 2019-12-22 04:01:23
问题 I have an instance of Either[Future[A], Future[B]] and I would like to transform it to Future[Either[A, B]] . Since my previous question, cats 0.8.1 has been released, changing the structure of the library and dropping Xor in favor of Either , which is right-biased in 2.12. Thus the method described in the previous accepted answer does not work anymore. I have tried to find the appropriate imports but failed. cats.instances.either._ cats.implicits._ cats.syntax.bitraverse._ looked plausible

How to split a List[Either[A, B]]

心不动则不痛 提交于 2019-12-17 19:37:34
问题 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](eithers : List[Either[A, B]]) : List[B] = eithers.collect { case Right(r) => r} 回答1: Not sure this is really much neater, but : scala> def splitEitherList[A,B](el: List[Either[A,B]]) = { val (lefts, rights) = el.partition(_.isLeft) (lefts.map(_.left.get), rights.map(_.right.get)) } splitEitherList: [A, B](el:

Chaining method calls with Either

﹥>﹥吖頭↗ 提交于 2019-12-14 03:42:05
问题 I'd like to know if it is possible to create some kind of "method call chain", with all methods returning the same Either[Error,Result]. What i'd like to do is: call all the methods successively, and when method returns a Left(Error), then stop the method calls and return the first Left found in the call chain. I've tryied some stuff, with fold, map, projections... but i'm new to Scala and don't find any elegant solution. I've tryed some thing like that: def createUserAndMandatoryCategories