applicative-fail 0.0.1 → 0.0.2
raw patch · 3 files changed
+75/−8 lines, 3 filesdep +bifunctorsPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependencies added: bifunctors
API changes (from Hackage documentation)
- Control.Applicative.Fail: mapFail :: (e -> e') -> Fail e a -> Fail e' a
+ Control.Applicative.Fail: instance Bifunctor Fail
Files
- README.md +62/−0
- applicative-fail.cabal +7/−2
- src/Control/Applicative/Fail.hs +6/−6
+ README.md view
@@ -0,0 +1,62 @@+# What is it for?++Assume you have some type++```haskell+data Animal =+ Animal+ { species :: String+ , weight :: Double+ , age :: NominalDiffTime+ }+```++And you would like to produce this value from some data (e.g. query+parameters). There can be some warnigns or value can not be produced+at all. It would be great to have some simple tool to notify about+warnings and/or fail computation.++Like that:++```haskell+spc = "Parastratiosphecomyia stratiosphecomyioides"+w = 100+a = 27234++animal = Animal+ <$> (if length spc > 20+ then fwarn "Name is too long" spc+ else if spc == ""+ then ffail "Name can not be empty"+ else fsucc spc)+ <*> (if w < 0+ then ffail "Weight can not be negative"+ else fsucc w)+ <*> (if a < 0+ then ffail "Age can not be negative"+ else fsucc a)+```++Now you can inspect the value we have got++```haskell+λ> animal+Fail ["Name is too long"] (Just (Animal {species = "Parastratiosphecomyia stratiosphecomyioides", weight = 100.0, age = 27234}))+λ> getSucc animal+Just (Animal {species = "Parastratiosphecomyia stratiosphecomyioides", weight = 100.0, age = 27234})+λ> getFail animal+Just ["Name is too long"]+```++Here is another simple examples:++```haskell+λ> (,) <$> ffail "oups" <*> ffail "duh"+Fail ["oups","duh"] Nothing+λ> (,) <$> fwarn "oups" "hello" <*> fwarn "duh" "world"+Fail ["oups","duh"] (Just ("hello","world"))+λ> (,) <$> fsucc "hello" <*> fwarn "duh" "world"+Fail ["duh"] (Just ("hello","world"))+λ> (,) <$> fsucc "hello" <*> fsucc "world"+Success ("hello","world")+```
applicative-fail.cabal view
@@ -1,9 +1,9 @@ name: applicative-fail-version: 0.0.1+version: 0.0.2 synopsis: Applicative functor which collects all your fails description: Applicative functor to perform parse-like actions and- collect wanrings/failures while+ collect wanrings/failures license: BSD3 license-file: LICENSE@@ -15,6 +15,8 @@ cabal-version: >=1.10 +extra-source-files: README.md+ homepage: https://bitbucket.org/s9gf4ult/applicative-fail source-repository head type: git@@ -31,5 +33,8 @@ , DeriveTraversable build-depends: base >=4.6 && <4.8+ , bifunctors exposed-modules: Control.Applicative.Fail++ ghc-options: -Wall
src/Control/Applicative/Fail.hs view
@@ -7,13 +7,13 @@ , getSucc -- * Combinators , failEither- , mapFail , joinFail , bindFail , composeFail ) where import Control.Applicative+import Data.Bifunctor import Data.Foldable import Data.Monoid import Data.Traversable@@ -58,6 +58,10 @@ , Foldable, Traversable , Typeable, Generic ) +instance Bifunctor Fail where+ bimap fe fa (Fail e a) = Fail (fe e) (fa <$> a)+ bimap _ fa (Success a) = Success (fa a)+ instance (Monoid e) => Applicative (Fail e) where pure a = Success a (Success f) <*> a = fmap f a@@ -90,11 +94,7 @@ getSucc :: Fail e a -> Maybe a getSucc (Success a) = Just a-getSucc _ = Nothing--mapFail :: (e -> e') -> Fail e a -> Fail e' a-mapFail _ (Success a) = Success a-mapFail f (Fail e a) = Fail (f e) a+getSucc (Fail _ a) = a -- | Join two fails like monad does joinFail :: (Monoid e)