variadic-function: Create and transform functions with variable arity.

[ bsd3, data, functions, library ] [ Propose Tags ]


Maintainer's Corner

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS],,
Change log
Dependencies base (>=4.7 && <5) [details]
License BSD-3-Clause
Copyright 2021 Boris Lykah
Author Boris Lykah
Category Data, Functions
Home page
Bug tracker
Source repo head: git clone
Uploaded by BorisLykah at 2021-07-05T04:47:16Z
Distributions NixOS:
Downloads 262 total (17 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2021-07-05 [all 1 reports]

Readme for variadic-function-

[back to package description]


Create and transform functions with variable arity.

How to use

The core of the library is the Function class. Use createFunction to make a function with a variable number of arguments. Use transformFunction to access the arguments and change the result of any function.

constN :: Function f args a EmptyConstraint
       => a -> f
constN a = createFunction
  -- The arguments are not constrained
  (undefined :: p EmptyConstraint)
  -- Combine argument with accumulator. Here we just ignore the argument
  -- Ignore the accumulator and return `a` as a result
  (const a)
  -- Accumulator for combining with the arguments.
  -- We don't take any information from the arguments, so it is just ()

composeN :: (Function f args b EmptyConstraint, Function g args a EmptyConstraint)
         => (a -> b) -> g -> f
composeN f = transformFunction
  -- The arguments are not constrained
  (undefined :: p EmptyConstraint)
  -- Ignore arguments
  -- Ignore the accumulator and apply f to result of the original function `g`
  (\_ r -> f r)
  -- Composition does not use the accumulator either, so it is ()

Here is a more complex example that constrains arguments and uses the accumulator:

sumN :: forall r f args. (Function f args r ((~) r), Num r)
     => f
sumN = createFunction
  -- The argument must be the same type as the function result. 
  -- To be able to mention `r` in here, the function signature 
  -- has `forall` and ScopedTypeVariables is enabled.
  (undefined :: proxy ((~) r))
  -- Add argument to the accumulator
  -- Return accumulator as the result
  -- The initial value of accumulator