either

Is there a standard name or implementation of the “purely applicative Either”?

南楼画角 提交于 2019-12-09 05:54:39
问题 I frequently find use for what I call the "purely applicative Either ", i.e. Either with the Applicative instance available so long as we don't implement a Monad instance as well. newtype AEither e a = AEither { unAEither :: Either e a } deriving Functor -- technically we only need Semigroup instance Monoid e => Applicative (AEither e) where pure a = AEither (pure a) AEither e1 <*> AEither e2 = AEither (combine e1 e2) where combine (Right f) (Right a) = Right (f a) combine (Left m1) (Left m2)

Scala Either map Right or return Left

别等时光非礼了梦想. 提交于 2019-12-06 19:01:06
问题 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 without any boilerplate like: val myEither:Either[String, Object] = Right(new Object()) myEither match { case Left(leftValue) => value case Right(righValue) => "Success" } 回答1: In Scala 2.12, Either is right-biased, which means that Right is assumed to be the default case to operate on. If it is

Either monadic operations

给你一囗甜甜゛ 提交于 2019-12-06 05:11: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 more difficult to understand since Either itself is not right biaised, does not have map / flatMap

How to convert functions raising exceptions to functions returning Either?

﹥>﹥吖頭↗ 提交于 2019-12-05 21:28:59
问题 Suppose I have a few functions that raise exceptions. I am wrapping them to return Either[Throwable, <function return type>] . (Let's assume I need Either rather than Try ). def fooWrapper(arg1: FooArg1, arg2: FooArg2) = try Right(foo(arg1, arg2)) catch { case NonFatal(e) => Left(e) } def barWrapper(arg1: BarArg1, arg2: BarArg2, a3: BarArg3) = try Right(bar(arg1, arg2, artg3)) catch { case NonFatal(e) => Left(e) } ... Now I would like to write a generic wrapper to get rid of the bolierpllate

Combining Futures (Twitter) and Either in Scala

爱⌒轻易说出口 提交于 2019-12-05 15:16:07
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 handler method which uses this repo and does some other checks and also creates a token def process

scala Either.RightProjection confusion (for comprehension de-sugaring)

北战南征 提交于 2019-12-05 14:27:56
问题 I can use an = in a scala for-comprehension (as specified in section 6.19 of the SLS) as follows: Option Suppose I have some function String => Option[Int] : scala> def intOpt(s: String) = try { Some(s.toInt) } catch { case _ => None } intOpt: (s: String)Option[Int] Then I can use it thus scala> for { | str <- Option("1") | i <- intOpt(str) | val j = i + 10 //Note use of = in generator | } | yield j res18: Option[Int] = Some(11) It was my understanding that this was essentially equivalent to:

Handling failures with Either -> Where is the stacktrace?

让人想犯罪 __ 提交于 2019-12-05 03:42:37
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 cause is essential. So what do we do in practice? Should i return, instead of a custom Error , the java

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

ぐ巨炮叔叔 提交于 2019-12-05 03:25:53
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 but unfortunately does not work. Compilation still fails with value bisequence is not a member of

Scala Either map Right or return Left

怎甘沉沦 提交于 2019-12-05 00:16:30
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 without any boilerplate like: val myEither:Either[String, Object] = Right(new Object()) myEither match { case Left(leftValue) => value case Right(righValue) => "Success" } In Scala 2.12, Either is right-biased, which means that Right is assumed to be the default case to operate on. If it is Left, operations like map, flatMap, ... return the Left value unchanged so you can do myEither.map(_ =>

how do I process returned Either

岁酱吖の 提交于 2019-12-04 17:04:02
问题 if a scala function is def A(): Either[Exception, ArrayBuffer[Int]] = { ... } what should be the right way to process the returned result? val a = A() and ? 回答1: I generally prefer using fold . You can use it like map: scala> def a: Either[Exception,String] = Right("On") a.fold(l => Left(l), r => Right(r.length)) res0: Product with Either[Exception,Int] = Right(2) Or you can use it like a pattern match: scala> a.fold( l => { | println("This was bad") | }, r => { | println("Hurray! " + r) | })