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
Category Control
Home page
Source repo head: git clone
Uploaded by AlekseyUymanov at Wed Mar 18 09:37:47 UTC 2015
Distributions NixOS:1.1.1
Downloads 2202 total (61 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]




Maintainer's Corner

For package maintainers and hackage trustees

Readme for applicative-fail-0.0.2

[back to package description]

What is it for?

Assume you have some type

data 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")