Contents

Description

Create and apply functions with an arbitrary number of arguments.

Synopsis

# 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.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

(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 Methodspolyvariadic :: accumulator -> (accumulator -> result) -> result Source # (Accumulator c i, Polyvariadic c b x) => Polyvariadic c b (i -> x) Source # Accumulates the next argument Methodspolyvariadic :: 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]) :: Int
3


Minimal complete definition

apply'

Methods

apply' :: x -> [a] -> b Source #

Instances

 Apply a b b Source # The final type is reached and the application terminates Methodsapply' :: b -> [a] -> b Source # Apply a b x => Apply a b (a -> x) Source # The final type is not reached yet and the application continues Methodsapply' :: (a -> x) -> [a] -> b Source #

apply :: (Apply a b x, Foldable t) => x -> t a -> b Source #

Like apply' but with an arbitrary Foldable instead if a list