applicative-fail: Applicative functor and monad which collects all your fails

[ bsd3, control, library ] [ Propose Tags ] [ Report a vulnerability ]

Applicative functor to perform parse-like actions and collect warnings/failures.


[Skip to Readme]

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.1, 0.0.2, 0.0.3, 1.0.0, 1.1.0, 1.1.1
Change log CHANGELOG.md
Dependencies base (>=4.6 && <5), bifunctors, dlist, mtl, transformers, transformers-base [details]
License BSD-3-Clause
Author Aleksey Uimanov
Maintainer s9gf4ult@gmail.com
Category Control
Home page https://bitbucket.org/s9gf4ult/applicative-fail
Source repo head: git clone git@bitbucket.org:s9gf4ult/applicative-fail.git
Uploaded by AlekseyUymanov at 2015-08-03T07:25:18Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 4546 total (1 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2015-08-03 [all 1 reports]

Readme for applicative-fail-1.1.1

[back to package description]

What is it for?

Assume you have some type

data Animal = Animal
    { species :: String
    , weight  :: Double
    , age     :: Int
    } deriving (Show)

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:

let spc = "Parastratiosphecomyia stratiosphecomyioides"
    w = 100
    a = 27234
    animal :: Fail [String] Animal
    animal = Animal
             <$> (if length spc > 20
                  then awarn "Name is too long" spc
                  else if spc == ""
                       then afail "Name can not be empty"
                       else pure spc)
             <*> (if w < 0
                  then afail "Weight can not be negative"
                  else pure w)
             <*> (if a < 0
                  then afail "Age can not be negative"
                  else pure a)

Now you can inspect the value we have got

>>> 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"]

Now this example can be rewritten with monadic syntax inside field checkers using module Control.Monad.Fail:

let animal :: Fail [String] Animal
    animal = Animal
             <$> (runFailI $ do
                          when (length spc > 20) $ mwarn "Name is too long"
                          when (spc == "") $ mfail "Name can not be empty"
                          return spc)
             <*> (runFailI $ do
                          when (w < 0) $ mfail "Weight can not be negative"
                          return w)
             <*> (runFailI $ do
                          when (a < 0) $ mfail "Age can not be negative"
                          return a)