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

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

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2
Change log ChangeLog.md
Dependencies base (>=4.7 && <5) [details]
License BSD-3-Clause
Copyright 2021 Boris Lykah
Author Boris Lykah
Maintainer lykahb@gmail.com
Category Data, Functions
Home page https://github.com/lykahb/variadic-function#readme
Bug tracker https://github.com/lykahb/variadic-function/issues
Source repo head: git clone https://github.com/lykahb/variadic-function
Uploaded by BorisLykah at 2021-07-05T04:47:16Z
Distributions NixOS:0.1.0.2
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 407 total (10 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-0.1.0.2

[back to package description]

variadic-function

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
  const
  -- 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
  const
  -- 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
  id
  -- The initial value of accumulator
  0