new Pair()
The Pair data type. Pair acts
- Version:
- 3.1.0
- Source:
Example
const {Pair} = require('futils').data;
Pair(1, 2); // -> Pair(_1: 1, _2: 2)
Pair(1, 2)._1; // -> 1
Pair(1, 2)._2; // -> 2
Extends
Methods
(static) from(a, b) → {Pair}
Lifts two values into a Pair. This method is curried
- Source:
Parameters:
Name | Type | Description |
---|---|---|
a |
any
|
The first value to lift |
b |
any
|
The second value to lift |
Returns:
- Type:
-
Pair
The values wrapped in a Pair
Example
const {Pair} = require('futils').data;
Pair.from(1, 'a'); // -> Pair(_1: 1, _2: 'a')
Pair.from(1); // -> a => Pair(_1: 1, _2: a)
(static) fromRight(a, b) → {Pair}
Lifts two values into a Pair from the right. This method is curried
- Source:
Parameters:
Name | Type | Description |
---|---|---|
a |
any
|
The second value to lift |
b |
any
|
The first value to lift |
Returns:
- Type:
-
Pair
The values wrapped in a Pair
Example
const {Pair} = require('futils').data;
Pair.fromRight(1, 'a'); // -> Pair(_1: 'a', _2: 1)
Pair.fromRight(1); // -> a => Pair(_1: a, _2: 1)
concat(a) → {Pair}
Concatenates a Pair with another. Please note, that the inner values have to be part of a Semigroup as well for concatenation to succeed
- Source:
Parameters:
Name | Type | Description |
---|---|---|
a |
Pair
|
The Pair instance to concatenate with |
Returns:
- Type:
-
Pair
A new Pair
Example
const {Pair} = require('futils').data;
const h = Pair('hello', 'how');
const w = Pair('world', 'are you today?');
const s = Pair(' ', ' ');
h.concat(s).concat(w); // -> Pair('hello world', 'how are you today?')
extend(f) → {Pair}
If given a function that takes a Pair and returns a value, returns a Pair
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
A function taking a Pair |
Returns:
- Type:
-
Pair
A new Pair
Example
const {Pair} = require('futils').data;
const addFstSnd = ({ _1, _2 }) => _1 + _2;
Pair(1, 1).extend(addFstSnd); // -> Pair(1, 2)
fst() → {any}
Returns the first value of a Pair
- Source:
Returns:
- Type:
-
any
The first value of the Pair
Example
const {Pair} = require('futils').data;
Pair(1, 2).fst(); // -> 1
map(f) → {Pair}
Maps a function over the second value of a Pair and returns a new Pair
- Source:
Parameters:
Name | Type | Description |
---|---|---|
f |
function
|
The function to map |
Returns:
- Type:
-
Pair
A new Pair
Example
const {Pair} = require('futils').data;
const inc = a => a + 1;
Pair(1, 1).map(inc); // -> Pair(1, 2)
snd() → {any}
Returns the second value of a Pair
- Source:
Returns:
- Type:
-
any
The second value of the Pair
Example
const {Pair} = require('futils').data;
Pair(1, 2).snd(); // -> 2