| 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.
Synopsis
- class Polyvariadic accumulator result x where
- polyvariadic :: accumulator -> (accumulator -> result) -> x
- class Apply a b x where
- apply' :: x -> [a] -> b
- 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"
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 |
Defined in Data.Function.Polyvariadic Methods polyvariadic :: accumulator -> (accumulator -> result) -> result Source # | |
| (Accumulator c i, Polyvariadic c b x) => Polyvariadic c b (i -> x) Source # | Accumulates the next argument |
Defined in Data.Function.Polyvariadic Methods polyvariadic :: c -> (c -> b) -> i -> x Source # | |
Application
class Apply a b x where Source #
Application of function with an arbitrary number of arguments to the elements of a list.
Will raise an error if the list doesn't have enough elements
Examples
>>>apply ((+) :: Int -> Int -> Int) ([1,2] :: [Int]) :: Int3
Instances
| Apply a b b Source # | The final type is reached and the application terminates |
Defined in Data.Function.Polyvariadic | |
| Apply a b x => Apply a b (a -> x) Source # | The final type is not reached yet and the application continues |
Defined in Data.Function.Polyvariadic | |