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
TypeErrorif 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
TypeErrorif too few or too many arguments are provided. Tuple.fromArray(array)- 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...)
- 0:
Tuple properties and methods
tuple.length- The number of component in the tuple.
tuple.item(n)- Returns the n-th component in the tuple.
tuple.bind(f)- Calls the function f with tuple as the argument.
tuple.applyTo(f)- Calls the function f with the components of tuple as the arguments.
Pair methods
pair.fst()- Returns the first component in the pair.
pair.snd()- Returns the second component in the pair.
Tuple flattening
tuple.Array()- Flattens tuple, including nested tuples, into an array.
tuple.applyArrayTo(f)- Calls the function f with tuple.Array() as the arguments.
Tuple pattern-matching
tuple.match(pattern)- 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 ')'
- Throws:
TypeErrorif the pattern is invalid.MatchErrorif the tuple does not match the pattern.
- Example:
Tuple(1,2,Tuple(Tuple(3,4),5)).match("a,,(b,c)")returns
{ a:1; b:Tuple(3,4); c:5 }
matchcan be used with the JavaScriptwithkeyword to bring the matched values into scope:with(tuple.match(pattern)) { ... }