either

Why `scala.util.Try` is not mentioned in chapter “Handling errors without exceptions” of book “functional programming in Scala”?

情到浓时终转凉″ 提交于 2019-12-02 15:38:23
In the chapter "Handling errors without exceptions" of book "functional programming in Scala", the author gives: The problem of throwing exceptions from the body of a function Use Option if we don't care about the actual exception Use Either if we care about the actual exception But scala.util.Try is not mentioned. From my point of view, I think Try is very suitable when we care about the actual exception, why it's not mentioned? Is there any reason I have missed? I'm neither of the authors of Functional Programming in Scala, but I can make a few guesses about why they don't mention Try . Some

Pattern match on value of Either inside a for comprehension?

江枫思渺然 提交于 2019-12-01 17:40:34
I have a for comprehension like this: for { (value1: String, value2: String, value3: String) <- getConfigs(args) // more stuff using those values } getConfigs returns an Either[Throwable, (Seq[String], String, String)] and when I try to compile I get this error: value withFilter is not a member of Either[Throwable,(Seq[String], String, String)] How can I use this method (that returns an Either ) in the for comprehension? Like this: for { tuple <- getConfigs() } println(tuple) Joking aside, I think that is an interesting question but it is misnamed a bit. The problem (see above) is not that for

Can I ask an Either whether it is Left (or Right)?

限于喜欢 提交于 2019-12-01 00:30:45
问题 I know I can usually just pattern match, but sometimes I would find these functions useful: isLeft = either (const True) (const False) isRight = either (const False) (const True) Is there something like that in the standard library? 回答1: While this is pretty old, posting here for reference. This is now in the standard library under Data.Either since 4.7: https://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Either.html isLeft :: Either a b -> Bool Return True if the given value is a Left

Is there no standard (Either a) monad instance?

守給你的承諾、 提交于 2019-11-30 11:19:22
I was under the impression that there was an instance for Either a somewhere, but I can't seem to find it. I have tried importing Control.Monad, Control.Monad.Instances and Data.Either as shown module Main where import Control.Monad import Data.Either import Control.Monad.Instances test :: [Either a b] -> Either a [b] test = sequence main = return () but ghc tells me that it could not deduce (Monad (Either a)). Adding instance Monad (Either a) where return = Right Right b >>= f = f b Left a >>= _ = Left a makes the code compile, but this instance declaration seems so general that it doesn't

Is there no standard (Either a) monad instance?

拥有回忆 提交于 2019-11-29 17:32:21
问题 I was under the impression that there was an instance for Either a somewhere, but I can't seem to find it. I have tried importing Control.Monad, Control.Monad.Instances and Data.Either as shown module Main where import Control.Monad import Data.Either import Control.Monad.Instances test :: [Either a b] -> Either a [b] test = sequence main = return () but ghc tells me that it could not deduce (Monad (Either a)). Adding instance Monad (Either a) where return = Right Right b >>= f = f b Left a >

Getting Value of Either

最后都变了- 提交于 2019-11-29 16:36:52
问题 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 { case Right(x) => x.toString case Left(x) => "left" } a: String = 5 回答1: I don't particularly like Either and as a result I'm not terribly familiar with it, but I believe you're looking for projections: either.left.getOrElse or either.right.getOrElse . Note that projections can be used in for-comprehensions as

Using Eithers with Scala “for” syntax

有些话、适合烂在心里 提交于 2019-11-29 09:02:52
As I understand it, Scala "for" syntax is extremely similar to Haskell's monadic "do" syntax. In Scala, "for" syntax is often used for List s and Option s. I'd like to use it with Either s, but the necessary methods are not present in the default imports. for { foo <- Right(1) bar <- Left("nope") } yield (foo + bar) // expected result: Left("nope") // instead I get "error: value flatMap is not a member..." Is this functionality available through some import? There is a slight hitch: for { foo <- Right(1) if foo > 3 } yield foo // expected result: Left(???) For a List, it would be List() . For

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

北城以北 提交于 2019-11-28 10:48:30
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} 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: List[Either[A,B]])(List[A], List[B]) scala> val el : List[Either[Int, String]] = List(Left(1), Right(

Using Eithers with Scala “for” syntax

人走茶凉 提交于 2019-11-28 02:24:35
问题 As I understand it, Scala "for" syntax is extremely similar to Haskell's monadic "do" syntax. In Scala, "for" syntax is often used for List s and Option s. I'd like to use it with Either s, but the necessary methods are not present in the default imports. for { foo <- Right(1) bar <- Left("nope") } yield (foo + bar) // expected result: Left("nope") // instead I get "error: value flatMap is not a member..." Is this functionality available through some import? There is a slight hitch: for { foo

Best way to turn a Lists of Eithers into an Either of Lists?

99封情书 提交于 2019-11-27 19:19:41
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 Left of the list of them, otherwise I return a Right of the list of the rights. val maybe: List[Either[String, Int]] = getMaybe val (strings, ints) = maybe.partition(_.isLeft) strings.map(_.left.get) match { case Nil => Right(ints.map(_.right.get)) case stringList => Left(stringList) } Calling get always makes me feel like I must be missing something. Is there a more idiomatic way to do this? Viktor