applicative-fail: Applicative functor which collects all your fails

[ bsd3, control, library ] [ Propose Tags ]

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


[Skip to Readme]
Versions [faq] 0.0.1, 0.0.2, 0.0.3, 1.0.0, 1.1.0, 1.1.1
Dependencies base (>=4.6 && <4.8), bifunctors [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 Wed Mar 18 12:25:35 UTC 2015
Distributions NixOS:1.1.1
Downloads 2202 total (64 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2015-03-18 [all 1 reports]

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for applicative-fail-0.0.3

[back to package description]

What is it for?

Assume you have some type

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:

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

λ> 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:

λ> (,) <$> 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")