Skip to content

Latest commit

 

History

History
353 lines (278 loc) · 9.45 KB

uniq.api.md

File metadata and controls

353 lines (278 loc) · 9.45 KB

Uniq APIs


difference(other) ⇒ Uniq

Returns a new set with the elements of the second set removed from the first.

Returns: Uniq - The set with the elements of other set removed from the source set.
Throws:

  • TypeError When other is null or undefined.
Param Description
other The set whose elements will be removed.

Example

const uniq_1 = new Uniq([1, 2, 3, 4, 5]);
const uniq_2 = new Uniq([1, 2]);
const difference = uniq_1.difference(uniq_2);
console.log(difference);
// => [Uniq] { 3, 4, 5 }

Uniq.empty() ⇒ Uniq

Creates a new set.

Returns: Uniq - The new set.
Example

const uniq = Uniq.empty();

every(predicate) ⇒ boolean

Tests if all elements of the collection satisfy the given predicate

Returns: boolean - True if all elements of set satisfy predicate.
Throws:

  • TypeError When predicate is not a function or a generator function.
Param Description
predicate The function to test set elements.

Example

const uniq_3 = Uniq.of(1, 3, 5);
const allOdds = uniq_3.every(x => x % 2 !== 0);
console.log(allOdds);
// => true

exists(predicate) ⇒ boolean

Tests if any element of the collection satisfies the given predicate.

Returns: boolean - True if any element of set satisfies predicate.

Param Description
predicate The function to test set elements.

Example

const uniq_4 = Uniq.of(1, 2, 3, 4, 5);
const hasEvenNum = uniq_4.exists(x => x % 2 === 0);
console.log(hasEvenNum);
// => true

filter(predicate) ⇒ Uniq

Returns a new collection containing only the elements of the collection for which the given predicate returns True.

Returns: Uniq - The set containing only the elements for which predicate returns true.
Throws:

  • TypeError When predicate is not a function or a generator function.
Param Description
predicate The function to test set elements.

Example

const uniq_5 = Uniq.of(1, 2, 3, 4, 5);
const filtered = uniq_5.filter(x => x > 2);
console.log(filtered);
// => [Uniq] { 3, 4, 5 }

fold(folder, state) ⇒ value

Applies the given accumulating function to all the elements of the set.

Returns: The final state.
Throws:

  • TypeError When state is null or undefined. Or folder is not a function or folder is a generator function.
Param Description
folder The accumulating function.
state The initial state.

Example

const uniq_6 = Uniq.of(1, 2, 3, 4, 5);
const foldResult = uniq_6.fold((state, elem) => state - elem, 0);
console.log(foldResult);
// => -15

foldRight(folder, state) ⇒ value

Applies the given accumulating function to all the elements of the set.

Returns: The final state.
Throws:

  • TypeError When state is null or undefined. Or folder is not a function or folder is a generator function.
Param Description
folder The accumulating function.
state The initial state.

Example

const uniq_7 = Uniq.of(1, 2, 3, 4, 5);
const foldRightResult = uniq_7.foldRight((elem, state) => elem - state, 0);
console.log(foldRightResult);
// => 3

intersect(...others) ⇒ boolean

Computes the intersection of sets.

Returns: Uniq - The intersection of the sets.
Throws:

  • TypeError When others has one or more sources which are not of type Set or Uniq.
Param Description
...others One or more of other sets.

Example

const uniq_8 = Uniq.of(1, 2, 3, 4, 5, 6, 7);
const otherSet_1 = Uniq.of(1, 2, 3, 4);
const otherSet_2 = new Set([3, 4]);
const intersectResult = uniq_8.intersect(otherSet_1, otherSet_2);
console.log(intersectResult);
// => [Uniq] { 3, 4 }

isEmpty() ⇒ boolean

Check if the set is empty.

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

const uniq = Uniq.of(1, 2, 3, 4);
console.log(uniq.isEmpty());
// => false

isProperSubsetOf(second) ⇒ boolean

Evaluates to "true" if all elements of the source set are in the second, and at least one element of the second is not in the source set.

Returns: True if the source set is a proper subset of the second.
Throws:

  • TypeError When second is null or undefined.
Param Description
second The second set to test against.

Example

const uniq_9 = Uniq.of(1, 2, 3);
const uniq_10 = Uniq.of(1, 2, 3, 4);
const isProperSubSet = uniq_9.isProperSubsetOf(uniq_10);
console.log(`uniq_9 is a proper subset of uniq_10: ${isProperSubSet}`);
// => uniq_9 is a proper subset of uniq_10: true

isProperSupersetOf(second) ⇒ boolean

Evaluates to "true" if all elements of the second set are in the source set, and at least one element of the source set is not in the second set.

Returns: True if the source set is a proper superset of the second.
Throws:

  • TypeError When second is null or undefined.
Param Description
second The second set to test against.

Example

const uniq_11 = Uniq.of(1, 2, 3, 4);
const uniq_12 = Uniq.of(1, 2, 3);
const isProperSuperset = uniq_11.isProperSupersetOf(uniq_12);
console.log(`uniq_11 is a proper superset of uniq_12: ${isProperSuperset}`);
// => uniq_11 is a proper superset of uniq_12: true

isSubsetOf(second) ⇒ boolean

Evaluates to "true" if all elements of the source set are in the second.

Returns: True if the source set is a subset of the second.
Throws:

  • TypeError When second is null or undefined.
Param Description
second The set to test against.

Example

const uniq_13 = Uniq.of(1, 2, 3);
const uniq_14 = Uniq.of(1, 2, 3, 4);
const isSubset = uniq_13.isSubsetOf(uniq_14);
console.log(`uniq_13 is a subset of uniq_14: ${isSubset}`);
// => uniq_13 is a subset of uniq_14: true

isSupersetOf(second) ⇒ boolean

Evaluates to "true" if all elements of the second set are in the source set.

Returns: True if the source set is a superset of the second.
Throws:

  • TypeError When second is null or undefined.
Param Description
second The set to test against.

Example

const uniq_15 = Uniq.of(1,2,3);
const uniq_16 = Uniq.of(1,2,3);
const isSuperset = uniq_15.isSupersetOf(uniq_16);
console.log(`uniq_15 is a superset of uniq_16: ${isSuperset}`);
// => uniq_15 is a superset of uniq_16: true

map(mapping) ⇒ Uniq

Returns a new collection containing the results of applying the given function to each element of the input set.

Returns: Uniq - A set containing the transformed elements.
Throws:

  • TypeError When mapping is not a function or a generator function.
Param Description
mapping The function to transform elements of the input set.

Example

const uniq_17 = Uniq.of(1, 2, 3, 4, 5);
const mappedResult = uniq_17.map(x => x + x);
console.log(mappedResult);
// => [Uniq] { 2, 4, 6, 8, 10 }

Uniq.of() ⇒ Uniq

Creates a new set from a variable number arguments.

Returns: Uniq - A newly created uniq.
Example

const uniq = Uniq.of(1, 1, 2, 2, 3, 4, 4, 5);
console.log(uniq);
// => [Uniq] { 1, 2, 3, 4, 5 };

partition(predicate) ⇒ Uniq

Splits the set into two sets containing the elements for which the given predicate returns true and false respectively.

Returns: An array containing two split sets.
Throws:

  • TypeError When predicate is not a function or a generator function.
Param Description
predicate The function to test set elements.

toArray() ⇒ Array

Returns an array containing all the elements of the set.

Returns: The result array.
Example

const uniq = Uniq.of(1, 2, 3);
const array = uniq.toArray();
console.log(array);
// => [ 1, 2, 3 ]

toDict() ⇒ Dict

Returns a Dict containing all the elements of the set.

Returns: The result Dict.
Example

const uniq_20 = Uniq.of(1, 2, 3, 4, 5);
const dict = uniq_20.toDict();
console.log(dict);
// [Dict] { 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 }

toMap() ⇒ Map

Returns a Map containing all the elements of the set.

Returns: The result Map.
Example

const uniq_21 = Uniq.of(1, 2, 3, 4, 5);
const map = uniq_21.toMap();
console.log(map);
// => Map(5) { 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5 }

toVec() ⇒ Vec

Returns a Vec containing all the elements of the set.

Returns: The result Vec.
Example

const uniq = Uniq.of(1, 2, 3);
const vec = uniq.toVec();
console.log(vec);
// => [ 1, 2, 3 ]

union(...others) ⇒ Uniq

Computes the union of sets.

Returns: Uniq - The union of sets.
Throws:

  • TypeError When others has one or more sources which are not of type Set or Uniq.
Param Description
...others One or more of other sets.

Example

const uniq_18 = Uniq.of(1, 2, 3, 4, 5);
const uniq_19 = Uniq.of(11, 12, 13, 14, 15);
const union = uniq_18.union(uniq_19);
console.log(union);
// => [Uniq] { 1, 2, 3, 4, 5, 11, 12, 13, 14, 15 }