Returns a new map with the value come out of valueMapping. The value is associated with the key come out of keyMapping.
Returns: Dict
- The result Dict.
Throws:
TypeError
When keyMapping is not a function or a generator function. Or valueMapping is not a function or a generator function.
Param | Type | Description |
---|---|---|
keyMapping | function |
The function to get key. |
valueMpping | function |
The function to get value. |
Example
const dict_1 = new Dict(
[[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const resultDict_1 = dict_1.change(k => k === 3, v => "tría");
console.dir(resultDict_1);
// => [Dict] { 1 => 'one', 2 => 'two', 3 => 'tría', 4 => 'four' }
Returns: Dict
- The empty Dict.
Example
const dict = Dict.empty();
Returns: boolean
- True if the predicate evaluates to true for all of the key/value pairs in the Dict.
Throws:
TypeError
when predicate is not a function or a generator function.
Param | Type | Description |
---|---|---|
predicate | function |
The function to test the input elements. |
Example
const dict_2 = new Dict(
[[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const allValueHasLengthOf2 = dict_2.every((k, v) => v.length > 2);
console.log(allValueHasLengthOf2);
// => true
Returns: boolean
- True if the predicate returns true for one of the key/value paris.
Throws:
TypeError
when predicate is not a function or a generator function.
Param | Type | Description |
---|---|---|
predicate | function |
The function to test the input elements. |
Example
const dict_3 = new Dict(
[[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const hasValueWithLengthOf5 = dict_3.exists((k, v) => v.length === 5);
console.log(hasValueWithLengthOf5);
// => true
Returns: Dict
- The filtered dict.
Throws:
TypeError
when predicate is not a function or a generator function.
Param | Type | Description |
---|---|---|
predicate | function |
The function to test the key/value pairs. |
Example
const dict_4 = new Dict(
[[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const filtered = dict_4.filter((k, v) => k > 1 && v.length > 3);
console.log(filtered);
// => [Dict] { 3 => 'three', 4 => 'four' }
Returns: key
| undefined
- The first key for which the predicate evaluates true or undefined if key is not found.
Param | Type | Description |
---|---|---|
predicate | function |
The function to test the input elements. |
Example
const dict_5 = new Dict(
[[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const keyFound = dict_5.findKey((k, v) => v === "four");
console.log(keyFound);
// => 4
Returns: *
- The final state value.
Throws:
TypeError
when state is null or undefined. Or folder is not a function. Or folder is a generator function.
Param | Type | Description |
---|---|---|
folder | function |
The function to update the state given the input key/value pairs. |
state | * |
The initial state. |
Example
const dict_6 = new Dict(
[[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const state = dict_6.fold((s, k, v) => `${s}{${k} -> ${v}}\n`, '');
console.log(state);
// =>
// {1 -> one}
// {2 -> two}
// {3 -> three}
// {4 -> four}
Returns: *
- The final state value.
Throws:
TypeError
when state is null or undefined. Or folder is not a function. Or folder is a generator function.
Param | Type | Description |
---|---|---|
folder | function |
The function to update the state given the input key/value pairs. |
state | * |
The initial state |
Example
const dict_7 = new Dict(
[[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const finalState = dict_7.foldRight((k, v, s) => `${s}{${k} -> ${v}}\n`, '');
console.log(finalState);
// =>
// {4 -> four}
// {3 -> three}
// {2 -> two}
// {1 -> one}
Returns: boolean
- True if the map is empty.
Example
const dict = new Dict();
console.log(dict.isEmpty());
// => true
Returns: Dict
- The resulting dict of keys and transformed values.
Throws:
TypeError
When mapping is not a function or a generator function.
Param | Type | Description |
---|---|---|
mapping | function |
The function to transform the key/value pairs. |
Example
const dict_8 = new Dict(
[[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const mappedResult = dict_8.map((k, v) => k % 2 === 0 ? "even" : "odd");
console.log(mappedResult);
// => [Dict] { 1 => 'odd', 2 => 'even', 3 => 'odd', 4 => 'even' }
Returns: Dict
- A newly created dict.
Example
const newDict = Dict.of(
['Key1', 100], ['key2', 200], ['key3', 300]
);
console.log(newDict);
// => [Dict] { 'Key1' => 100, 'key2' => 200, 'key3' => 300 }
Returns: Array
- An array containing two dicts.
Throws:
TypeError
when predicate is not a function or a generator function.
Param | Type | Description |
---|---|---|
predicate | function |
The function to test the input elements. |
Example
const dict_9 = new Dict(
[[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const partitioned = dict_9.partition((k, v) => k < 3);
console.log(partitioned);
// =>
// [
// [Dict] { 1 => 'one', 2 => 'two' },
// [Dict] { 3 => 'three', 4 => 'four' }
// ]
Returns: Array
- The result array.
Example
const dict_10 = new Dict(
[[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const array = dict_10.toArray();
console.log(array);
// => [ [ 1, 'one' ], [ 2, 'two' ], [ 3, 'three' ], [ 4, 'four' ] ]
Returns: Set
- The result set.
Example
const dict_11 = new Dict(
[[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const set = dict_11.toArray();
console.log(set);
// => [ [ 1, 'one' ], [ 2, 'two' ], [ 3, 'three' ], [ 4, 'four' ] ]