either

How to convert functions raising exceptions to functions returning Either?

孤街浪徒 提交于 2019-12-04 04:38:56
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 code. What would you suggest ? I would write something of the form like this: def wrap[Value](f: =>

Chaining method calls with Either

痴心易碎 提交于 2019-12-04 03:59:41
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(user: User) : Either[Error,User] = { User.create(user).right.map { Logger.info("User created") Category

how do I process returned Either

被刻印的时光 ゝ 提交于 2019-12-03 10:04:59
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 ? 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) | }) Hurray! On Or you can use it like getOrElse in Option : scala> a.fold( l => "Default" , r => r ) res2:

Mapping over Either's Left

给你一囗甜甜゛ 提交于 2019-12-03 08:35:51
问题 Somewhere in my app I receive an Either ParserError MyParseResult from Parsec. Downstream this result gets some other parsing done over using other libs. During that second phase of parsing there also may occur some kind of error which I would like to pass as a Left String , but for that I need to convert the result from Parsec to String too. To achieve that I need a function which will allow me to map over a Left with a show function. The mapping function I'm thinking of looks something like

Idiomatic error handling in Clojure

南楼画角 提交于 2019-12-03 08:17:20
问题 When I put on my C hat, I think that maybe idiomatic Clojure just does the simple thing and checks return values. When I put on my Java hat (reluctantly, I must add), I think to myself that since Clojure runs on the JVM the natural way must be to use JVM exceptions. When I put on my functional hat, I'm thinking that there must be some sort of monadic construction or threading macro that can handle errors in a composable way. So what's the idiomatic way to handle errors in a Clojure program?

Convert Option to Either in Scala

谁说我不能喝 提交于 2019-12-03 06:47:59
问题 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)} Unfortunately the code above doesn't compile and I need to add type Either[String, Int] explicitly: ox.fold(Left("No number"): Either[String, Int]) { x => Right(x) } Is it possible to convert Option to Either this way without adding the type ? How would you suggest convert Option to Either ? 回答1: No, if

Understanding how Either is an instance of Functor

做~自己de王妃 提交于 2019-12-03 05:29:53
问题 In my free time I'm learning Haskell, so this is a beginner question. In my readings I came across an example illustrating how Either a is made an instance of Functor : instance Functor (Either a) where fmap f (Right x) = Right (f x) fmap f (Left x) = Left x Now, I'm trying to understand why the implementation maps in the case of a Right value constructor, but doesn't in the case of a Left ? Here is my understanding: First let me rewrite the above instance as instance Functor (Either a) where

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

一世执手 提交于 2019-12-03 02:02:13
问题 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? 回答1: I'm neither of the

Convert Option to Either in Scala

不想你离开。 提交于 2019-12-02 20:26:59
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)} Unfortunately the code above doesn't compile and I need to add type Either[String, Int] explicitly: ox.fold(Left("No number"): Either[String, Int]) { x => Right(x) } Is it possible to convert Option to Either this way without adding the type ? How would you suggest convert Option to Either ? No, if you do it this way, you can't leave out the type. The type of Left("No number") is inferred to be Either

Understanding how Either is an instance of Functor

五迷三道 提交于 2019-12-02 18:47:57
In my free time I'm learning Haskell, so this is a beginner question. In my readings I came across an example illustrating how Either a is made an instance of Functor : instance Functor (Either a) where fmap f (Right x) = Right (f x) fmap f (Left x) = Left x Now, I'm trying to understand why the implementation maps in the case of a Right value constructor, but doesn't in the case of a Left ? Here is my understanding: First let me rewrite the above instance as instance Functor (Either a) where fmap g (Right x) = Right (g x) fmap g (Left x) = Left x Now: I know that fmap :: (c -> d) -> f c -> f