endo

Endomorphism utilities.

https://github.com/trskop/endo

Latest on Hackage:0.3.0.1@rev:1

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.

BSD-3-Clause licensed by Peter Trško
Maintained by [email protected]

Package defines extra functions for Data.Monoid.Endo data type, and also generic endomorphism folding machinery. Generic endomorphism folding can be used for various purposes, including as a builder.

Here is an example how to use it with optparse-applicative package:

data Verbosity = Silent | Normal | Verbose | Annoying
  deriving (Show)
data Config = Config Verbosity FilePath
  deriving (Show)
options :: Parser Config
options = runIdentityT $ runEndo defaultConfig <$> options'
  where
    -- All this IdentityT clutter is here to avoid orphan instances.
    options' :: IdentityT Parser (Endo Config)
    options' = foldEndo
        <*> outputOption     -- :: IdentityT Parser (Maybe (E Config))
        <*> verbosityOption  -- :: IdentityT Parser (Maybe (E Config))
        <*> annoyingFlag     -- :: IdentityT Parser (E Config)
        <*> silentFlag       -- :: IdentityT Parser (E Config)
        <*> verboseFlag      -- :: IdentityT Parser (E Config)

    defaultConfig :: Config
    defaultConfig = Config Normal ""
main :: IO ()
main = execParser (info options fullDesc) >>= print
ghci> :main -o an.out.put --annoying
Config Annoying "an.out.put"

For details how individual option parsers look like see module Data.Monoid.Endo.Fold which contains other examples as well as this one.