core.arity
¶Stability: | 3 - Stable |
---|---|
Bug Tracker: | https://github.com/folktale/core.arity/issues |
Version: | 1.0.0 |
Repository: | https://github.com/folktale/core.arity |
Portability: | Portable |
npm package: | core.arity |
Restricts the arity of variadic functions.
Since all functions in JavaScript are variadic, programmers often take advantage of this fact by providing more arguments than what a function takes, and the callee can just ignore them. With curried functions, calling a binary function with three arguments ends up invoking the return value of the function with the extra argument!
var curry = require('core.lambda').curry;
function add(a, b) {
return a + b;
}
var cadd = curry(2, add);
cadd(1)(2) // => 3
cadd(1, 2) // => 3
cadd(1, 2, 4) // => Error: 3 is not a function
To fix this, one would need to wrap the curried function such that the wrapper only passes two arguments to it, and ignores the additional ones:
var binary = require('core.arity').binary;
binary(cadd)(1, 2, 4) // => 3
core.arity.
nullary
(f)¶Returns: | A function that takes no arguments. |
---|
(α₁, α₂, ..., αₙ → β) → (Unit → β)
Restricts a variadic function to a nullary one.
core.arity.
unary
(f)¶Returns: | A function that takes one argument. |
---|
(α₁, α₂, ..., αₙ → β) → (α₁ → β)
Restricts a variadic function to an unary one.