Members
(static, constant) unit
The unit
object, an empty tuple. Note that isTuple(unit) === false
, as in Haskell.
Haskell> () :: ()
- Source:
Methods
(static) curry(f, x, y) → {function}
Convert an uncurried function to a curried function. For example, a function that expects a tuple
as an argument can be curried into a function that binds one value and returns another function
that binds the other value. This function can then be called with or without arguments bound, or
with arguments partially applied. Currying and uncurrying are transitive.
Haskell> curry :: ((a, b) -> c) -> a -> b -> c
Parameters:
Name | Type | Description |
---|---|---|
f |
function | The function to curry |
x |
* | Any value, the first value of the new tuple argument |
y |
* | Any value, the second value of the new tuple argument |
- Source:
Returns:
The curried function
- Type
- function
Example
const f = p => fst(p) - snd(p);
const a = curry(f); // a === f()()
const b = a(100); // b === f(100)()
const c = b(15); // c === f(100)(15) === 85
const p = tuple(100, 15);
const A = curry(f); // A(100)(15) === 85
const B = uncurry(A); // B(p) === 85
const C = curry(B); // A(100)(15) === C(100)(15) === 85
(static) fromArrayToTuple(arr) → {Tuple}
Convert an array into a Tuple
. Returns the value at index 0 for single element arrays and
unit
, the empty tuple, if the array is empty. Note that this function will not work on
array-like objects.
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array.<*> | The array to convert |
- Source:
Returns:
A new Tuple
, the converted array
- Type
- Tuple
Example
const arr = [10,20];
fromArrayToTuple(arr); // => (10,20)
(static) fromTupleToArray(p) → {Array.<*>}
Convert a Tuple
into an array.
Parameters:
Name | Type | Description |
---|---|---|
p |
Tuple | The |
- Source:
Returns:
A new array, the converted Tuple
.
- Type
- Array.<*>
Example
const tup = tuple(10,20);
fromTupleToArray(tup); // => [10,20]
(static) fst(p) → {*}
Extract the first value of a tuple.
Haskell> fst :: (a, b) -> a
Parameters:
Name | Type | Description |
---|---|---|
p |
Tuple | A |
- Source:
Returns:
The first value of the Tuple
.
- Type
- *
Example
const tup = tuple(10,20);
fst(tup); // => 10
(static) isTuple(a) → {boolean}
Determine whether an object is a Tuple
. The empty tuple, unit
, returns false
.
Parameters:
Name | Type | Description |
---|---|---|
a |
* | Any object |
- Source:
Returns:
true
if the object is a Tuple
or false
otherwise
- Type
- boolean
(static) isUnit(p) → {boolean}
Check whether a Tuple
is an empty tuple, or unit
. Returns true
if the Tuple
is unit
.
Returns false if the Tuple
is non-empty. Throws a type error, otherwise.
Parameters:
Name | Type | Description |
---|---|---|
p |
Tuple | A |
- Source:
Returns:
true
if the object is unit
, false
if it is a non-empty Tuple
- Type
- boolean
Example
isUnit(tuple(1,2)); // => false
isUnit(unit); // => true
isUnit(tuple(unit, unit)); // => false
(static) snd(p) → {*}
Extract the second value of a tuple.
Haskell> snd :: (a, b) -> b
Parameters:
Name | Type | Description |
---|---|---|
p |
Tuple | A |
- Source:
Returns:
The second value of the Tuple
.
- Type
- *
Example
const tup = tuple(10,20);
snd(tup); // => 20
(static) swap(p) → {Tuple}
Swap the values of a tuple. This function does not modify the original tuple.
Haskell> swap :: (a, b) -> (b, a)
Parameters:
Name | Type | Description |
---|---|---|
p |
Tuple | A |
- Source:
Returns:
A new Tuple
, with the values of the first tuple swapped
- Type
- Tuple
Example
const tup = tuple(10,20);
swap(tup); // => (20,10)
(static) tuple(…as) → {Tuple}
Create a new Tuple
from any number of values. A single value will be returned unaltered,
and unit
, the empty tuple, will be returned if no arguments are passed.
Haskell> (,) :: a -> b -> (a, b)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
as |
* |
<repeatable> |
The values to put into a |
- Source:
Returns:
A new Tuple
- Type
- Tuple
Example
tuple(10,20); // => (10,20)
(static) uncurry(f, p) → {function}
Convert a curried function to a single function that takes a tuple as an argument—mostly useful
for uncurrying functions previously curried with the curry
function. This function will not
work if any arguments are bound to the curried function (it would result in a type error in
Haskell). Currying and uncurrying are transitive.
Haskell> uncurry :: (a -> b -> c) -> (a, b) -> c
Parameters:
Name | Type | Description |
---|---|---|
f |
function | The function to uncurry |
p |
Tuple | The tuple from which to extract argument values for the function |
- Source:
Returns:
The uncurried function
- Type
- function
Example
const f = p => fst(p) - snd(p);
const p = tuple(100, 15);
const a = curry(f); // a === f()()
const b = uncurry(a); // b === f()
const c = b(p); // c === f({`1`:100,`2`:15}) === 85
const d = uncurry(a, p) // d === 85