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

Loading

Require the core.arity package, after installing it:

var arity = require('core.arity')

Why?

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

Uncategorised

nullary()

core.arity.nullary(f)
Returns:A function that takes no arguments.
(α₁, α₂, ..., αₙ → β) → (Unit → β)

Restricts a variadic function to a nullary one.

unary()

core.arity.unary(f)
Returns:A function that takes one argument.
(α₁, α₂, ..., αₙ → β) → (α₁ → β)

Restricts a variadic function to an unary one.

binary()

core.arity.binary(f)
Returns:A function that takes two arguments.
(α₁, α₂, ..., αₙ → β) → (α₁ → α₂ → β)

Restricts a variadic function to a binary one.

ternary()

core.arity.ternary(f)
Returns:A function that takes three arguments.
(α₁, α₂, ..., αₙ → β) → (α₁ → α₂ → α₃ → β)

Restricts a variadic function to a ternary one.