Skip to content

Latest commit

 

History

History
296 lines (246 loc) · 8.25 KB

dict.api.md

File metadata and controls

296 lines (246 loc) · 8.25 KB

Dict APIs


change(keyMapping, valueMpping) ⇒ Dict

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 Description
keyMapping The function to get key.
valueMpping 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' }

Dict.empty() ⇒ Dict

Create an empty Dict.

Returns: Dict - The empty Dict.
Example

const dict = Dict.empty();

every(predicate) ⇒ Dict

Returns true if the given predicate returns true for all of the bindings in the dict.

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 Description
predicate 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

exists(predicate) ⇒ Dict

Returns true if the given predicate returns true for one of the bindings in the Dict.

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 Description
predicate 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

filter(predicate) ⇒ Dict

Builds a new dict containing only the bindings for which the given predicate returns 'true'.

Returns: Dict - The filtered dict.
Throws:

  • TypeError when predicate is not a function or a generator function.
Param Description
predicate 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' }

findKey(predicate) ⇒ key|undefined

Evaluates the function on each mapping in the collection. Returns the key for the first mapping where the function returns 'true'.

Returns: key | undefined - The first key for which the predicate evaluates true or undefined if key is not found.

Param Description
predicate 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

fold(folder, state) ⇒ value

Folds over the bindings in the dict.

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 Description
folder 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}

foldRight(folder, state) ⇒ value

Folds over the bindings in the map.

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 Description
folder 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}

isEmpty() ⇒ boolean

Check if the map is empty.

Returns: boolean - True if the map is empty.
Example

const dict = new Dict();
console.log(dict.isEmpty());
// => true

map(mapping) ⇒ Dict

Builds a new dict whose elements are the results of applying the given function to each of the elements of the dict. The key passed to the function indicates the key of element being transformed.

Returns: Dict - The resulting dict of keys and transformed values.
Throws:

  • TypeError When mapping is not a function or a generator function.
Param Description
mapping 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' }

Dict.of() ⇒ Dict

Creates a new dict from a variable number of key/value pair arguments.

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 }

partition(predicate) ⇒ Array

Builds two new dicts, one containing the bindings for which the given predicate returns 'true', and the other the remaining bindings.

Returns: An array containing two dicts.
Throws:

  • TypeError when predicate is not a function or a generator function.
Param Description
predicate 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' }
//   ]

toArray() ⇒ Array

Returns an array of all key-value pairs in the mapping. The array will be ordered by the keys of the dict.

Returns: 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' ] ]

toSet() ⇒ Set

Returns a set of all key-value pairs in the mapping. The set will be ordered by the keys of the map.

Returns: 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' ] ]

toUniq() ⇒ Uniq

Returns a uniq of all key-value pairs in the mapping. The uniq will be ordered by the keys of the map.

Returns: The result set.
Example

const dict = new Dict(
   [[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const uniq = dict.toUniq();
console.log(uniq);
// => [ [ 1, 'one' ], [ 2, 'two' ], [ 3, 'three' ], [ 4, 'four' ] ]

toVec() ⇒ Vec

Returns a vec of all key-value pairs in the mapping. The vec will be ordered by the keys of the dict.

Returns: The result vec.
Example

const dict = new Dict(
   [[1, "one"], [2, "two"], [3, "three"], [4, "four"]]
);
const vec = dict.toVec();
console.log(vec);
// => [ [ 1, 'one' ], [ 2, 'two' ], [ 3, 'three' ], [ 4, 'four' ] ]