liveBook · Manning

Arity

As explained in Functional Programming in JavaScript

The number of arguments a function accepts.

Arity is the number of arguments a function accepts. In a functional pipeline, a receiving function must declare at least one parameter so it can handle the previous function’s output. section 4.2

Arity in a typed tuple

A tuple can enforce arity by comparing the number of supplied values with its declared types. Constructing a two-value StringPair with three values therefore raises an arity mismatch. section 4.2

Listing 4.2. Typed Tuple data type
const Tuple = function( /* types */ ) {
   const typeInfo = Array.prototype.slice.call(arguments, 0);             #1
   const _T =  function( /* values */ ) {                                 #2
      const values = Array.prototype.slice.call(arguments, 0);            #3
      if(values.some((val) =>                                             #4
         val === null || val === undefined)) {
         throw new ReferenceError('Tuples may not have 
            any null values');
      }
      if(values.length !== typeInfo.length) {                             #5
         throw new TypeError('Tuple arity does not 
            match its prototype');
      }
      values.map(function(val, index) {                                   #6
            this['_' + (index + 1)] = checkType(typeInfo[index])(val); 
           }, this);
      Object.freeze(this);                                                #7
  };
  _T.prototype.values = function() {                                      #8
        return Object.keys(this).map(function(k) {
             return this[k];
        }, this);
  };
  return _T;
};

Inspect the Tuple implementation’s length check for a concrete runtime treatment of arity. section 4.2

How Functional Programming in JavaScript covers it

How deep it goes
Defines arity and demonstrates runtime tuple validation, while deferring broader type-system treatment.
Examples
Uses trim and normalize pipelines, multi-argument HTTP functions, and typed tuples with arity checks.
What it assumes
Assumes basic functions and arguments, then builds toward pipeline compatibility and typed tuples.
What you can do after
Evaluate whether functions and tuple values provide the required number of parameters or elements.
How it connects
Connects arity with function pipelines, type compatibility, and tuples.

Also explained in

Compare how 5 books explain arity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.