either

How is it possible to collect all error messages in the Either Monad?

核能气质少年 提交于 2021-02-04 19:11:07
问题 I tried to validate the construction of a Record with Applicatives and the Either Monad . It works fine. But I can't see all Error Messages. Only the first is visible because the Right Path of the Either Monad ignores them. Here is my code: import Data.Either (either) import Text.Printf (printf) data Record = Record { fieldA :: String , fieldB :: String , fieldC :: String } deriving (Show, Eq) type Err = String setField :: String -> String -> Either Err String setField field value | length

Why do `Left` and `Right` have two type parameters?

落爺英雄遲暮 提交于 2020-12-01 03:23:57
问题 I understand it would be difficult to change now without breaking existing code, but I'm wondering why it was done that way in the first place. Why not just: sealed trait Either[+A, +B] case class Left[A](x: A) extends Either[A, Nothing] case class Right[B](x: B) extends Either[Nothing, B] Is there some drawback here that I'm failing to see...? 回答1: Not sure how relevant this answer really is to Scala, but it certainly is in Haskell which is evidently where Scala's Either was borrowed from

Why do `Left` and `Right` have two type parameters?

谁说胖子不能爱 提交于 2020-12-01 03:23:09
问题 I understand it would be difficult to change now without breaking existing code, but I'm wondering why it was done that way in the first place. Why not just: sealed trait Either[+A, +B] case class Left[A](x: A) extends Either[A, Nothing] case class Right[B](x: B) extends Either[Nothing, B] Is there some drawback here that I'm failing to see...? 回答1: Not sure how relevant this answer really is to Scala, but it certainly is in Haskell which is evidently where Scala's Either was borrowed from

Why do `Left` and `Right` have two type parameters?

醉酒当歌 提交于 2020-12-01 03:22:32
问题 I understand it would be difficult to change now without breaking existing code, but I'm wondering why it was done that way in the first place. Why not just: sealed trait Either[+A, +B] case class Left[A](x: A) extends Either[A, Nothing] case class Right[B](x: B) extends Either[Nothing, B] Is there some drawback here that I'm failing to see...? 回答1: Not sure how relevant this answer really is to Scala, but it certainly is in Haskell which is evidently where Scala's Either was borrowed from

How to convert Either to MonadThrow

一笑奈何 提交于 2020-06-16 07:18:30
问题 I have a function that handles errors via Either : funErrViaEither :: a -> Either SomeException b I want to use this function in another function that should be more flexible and return MonadThrow m : funErrViaThrow :: MonadThrow m => a -> m b funErrViaThrow x = if x = someCondition then funErrViaEither else throwM (SomeException MyCustomException) This does not compile; the type checker complains that the return type of funErrViaEither does not match the expected type m b . I don't

How to convert Either to MonadThrow

夙愿已清 提交于 2020-06-16 07:18:15
问题 I have a function that handles errors via Either : funErrViaEither :: a -> Either SomeException b I want to use this function in another function that should be more flexible and return MonadThrow m : funErrViaThrow :: MonadThrow m => a -> m b funErrViaThrow x = if x = someCondition then funErrViaEither else throwM (SomeException MyCustomException) This does not compile; the type checker complains that the return type of funErrViaEither does not match the expected type m b . I don't

How to extract Left or Right easily from Either type in Dart (Dartz)

三世轮回 提交于 2020-01-25 06:53:28
问题 I am looking to extract a value easily from a method that return a type Either<Exception, Object> . I am doing some tests but unable to test easily the return of my methods. For example: final Either<ServerException, TokenModel> result = await repository.getToken(...); To test I am able to do that expect(result, equals(Right(tokenModelExpected))); // => OK Now how can I retrieve the result directly? final TokenModel modelRetrieved = Left(result); ==> Not working.. I found that I have to cast

Pattern match on value of Either inside a for comprehension?

荒凉一梦 提交于 2020-01-21 11:06:43
问题 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? 回答1: Like this: for { tuple <- getConfigs() } println(tuple) Joking

Pattern match on value of Either inside a for comprehension?

[亡魂溺海] 提交于 2020-01-21 11:04:09
问题 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? 回答1: Like this: for { tuple <- getConfigs() } println(tuple) Joking

Replacing Exceptions With Either/Maybe/Option

 ̄綄美尐妖づ 提交于 2020-01-12 03:52:50
问题 I came across this dead end while trying to replace exceptions with either monad in c#. Which leads me to think maybe it is not only language specific problem and more technique related missing feature. Let me try to re-explain it more globally: Given: I have a 3rd party function( a function that is imported into my code and I have no access to) which receives a lazy list (c# IEnumerable,f# Seq...) and consume it I Want: To apply a function (LINQ select,map...) on the method's lazy list