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

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

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 ChangeLog.md base (>=4.7 && <5) [details] BSD-3-Clause 2021 Boris Lykah Boris Lykah lykahb@gmail.com Data, Functions https://github.com/lykahb/variadic-function#readme https://github.com/lykahb/variadic-function/issues head: git clone https://github.com/lykahb/variadic-function by BorisLykah at 2021-07-05T04:47:16Z NixOS:0.1.0.2 1 direct, 0 indirect [details] 296 total (9 in the last 30 days) (no votes yet) [estimated by Bayesian average] λ λ λ Docs available Last success reported on 2021-07-05

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