polyvariadic-0.3.0.0: Creation and application of polyvariadic functions

Copyright(c) Francesco Gazzetta 2017
LicenseBSD3 (see the file LICENSE)
Maintainerfrancygazz@gmail.org
Stabilityexperimental
Portabilityportable
Safe HaskellSafe
LanguageHaskell2010

Data.Function.Polyvariadic

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

polyvariadic

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

Methods

polyvariadic :: accumulator -> (accumulator -> result) -> result Source #

(Accumulator c i, Polyvariadic c b x) => Polyvariadic c b (i -> x) Source #

Accumulates the next argument

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]) :: 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

Methods

apply' :: 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

Methods

apply' :: (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