| Copyright | (c) Francesco Gazzetta 2017 |
|---|---|
| License | BSD3 (see the file LICENSE) |
| Maintainer | francygazz@gmail.org |
| Stability | experimental |
| Portability | portable |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Data.Function.Polyvariadic
Contents
Description
Create and apply functions with an arbitrary number of arguments.
- class Polyvariadic accumulator result x where
- class Apply a b x where
- apply :: (Apply a b x, Foldable t) => x -> t a -> b
Creation
class Polyvariadic accumulator result x where Source #
Creation of functions with an arbitrary number of arguments.
The arguments will be accumulated in the given Accumulator,
which will then be passed as an argument to the function.
Examples
Three integers to a list. Note that you have to add type annotations for nearly everything to avoid ambiguities >>> polyvariadic mempty (id :: [Int] -> [Int]) (1::Int) (2::Int) (3::Int) :: [Int]
The classic printf function, which takes an arbitrary amount of arguments
and inserts them in a string:
{--}
{--}
{--}
import Data.Function.Polyvariadic
import Data.Accumulator
magicChar = '%'
notMagicChar = (/= magicChar)
data PrintfAccum = PrintfAccum { done :: String, todo :: String }
instance Show x => Accumulator PrintfAccum x where
accumulate x (PrintfAccum done (_:todo)) = PrintfAccum
(done ++ show x ++ takeWhile notMagicChar todo)
(dropWhile notMagicChar todo)
accumulate _ acc = acc
printf' str = polyvariadic
(PrintfAccum (takeWhile notMagicChar str) (dropWhile notMagicChar str))
done
>>>printf' "aaa%bbb%ccc%ddd" "TEST" 123 True"aaa\"TEST\"bbb123cccTrueddd"
Minimal complete definition
Methods
polyvariadic :: accumulator -> (accumulator -> result) -> x Source #
Takes an accumulator acc, a function f, and an arbitrary
number of additional arguments which will be accumulated in acc,
which is finally passed to f.
Instances
| Polyvariadic accumulator result result Source # | There are no more arguments to accumulate so the function is applied
to the |
| (Accumulator c i, Polyvariadic c b x) => Polyvariadic c b (i -> x) Source # | Accumulates the next argument |