API - Tuples

JavaScript implementation of tuples, with support for flattening and pattern-matching. This implementation does not support unit or singleton tuples. Additionally, pair tuples are specialized to support the fst and snd pair operations.

Tuples constructors

Tuple(x, y [, ...])
Creates a tuple, or a pair as appropriate.
Throws TypeError if attempting to create unit (zero argument) or singleton (one argument) tuples.
Pair(x, y)
Creates a pair (which is a specialized instance of tuple).
Throws TypeError if too few or too many arguments are provided.
Converts an array to a tuple as appropriate.
This factory method will return one of the following depending on the length of array:
  • 0: undefined
  • 1: array[0]
  • 2: Pair(array[0], array[1])
  • 3: Tuple(array...)

Tuple properties and methods

The number of component in the tuple.
Returns the n-th component in the tuple.
Calls the function f with tuple as the argument.
Calls the function f with the components of tuple as the arguments.

Pair methods

Returns the first component in the pair.
Returns the second component in the pair.

Tuple flattening

Flattens tuple, including nested tuples, into an array.
Calls the function f with tuple.Array() as the arguments.

Tuple pattern-matching

Matches a tuple to a pattern and returns an object with properties containing the matched values.
pattern is a string written in the following grammar:
  • tuple-pattern ::= tuple-component (',' tuple-component)+
  • tuple-component ::= identifier | blank | '(' tuple-pattern ')'
  • TypeError if the pattern is invalid.
  • MatchError if the tuple does not match the pattern.
  • Tuple(1,2,Tuple(Tuple(3,4),5)).match("a,,(b,c)") returns
    { a:1; b:Tuple(3,4); c:5 }
match can be used with the JavaScript with keyword to bring the matched values into scope:
  • with(tuple.match(pattern)) { ... }

Web Accessibility