Hello everybody! I would like to run some validations depending on previous computations and fail at the first error. Here is a made up example:
getENumber :: Effect (Maybe Int)
getENumber = pure (Just 15)
toEither :: forall a. String -> Maybe a -> Either String a
toEither errorMessage Nothing = Left errorMessage
toEither errorMessage (Just x) = Right x
greater10 :: String -> Int -> (Either String Int)
greater10 msg n
| n > 10 = Right n
greater10 msg n = Left msg
add10 :: String -> Int -> Either String Int
add10 msg n
| n < 30 = Right (n + 10)
add10 msg n = Left msg
addE10 :: String -> Int -> Effect (Either String Int)
addE10 msg n
| n < 50 = pure $ Right (n + 10)
addE10 msg n = pure $ Left msg
pipe :: Effect (Either String Int)
pipe =
getENumber
>>= toEither "no number" --Error
>>= greater10 "first check"
>>= add10 "failed at +10"
>>= add10 "failed at +20"
>>= add10 "failed at +30"
>>= addE10 "failed at +40"
>>= add10 "failed at +50"
>>= greater10 "last check"
In chapter 8 address
was lifted to work with Maybe. How can lift these functions properly to work with Either?