Skip to main content

interface VariantSetBase<T,Tp>

undocumented

Companion namespace: VariantSetBase

Extends: FastIterable<T>

Implemented by: VariantSet<T>, RSetBase<T,Tp>, VariantSetBase.NonEmpty<T,Tp>

Type parameters

NameConstraintsDefaultDescription
Tundocumented
TpVariantSetBase.TypesVariantSetBase.Typesundocumented

Properties

isEmpty

Returns true if the collection is empty.

Definition

readonly isEmpty: boolean;

size

Returns the number of values in the collection.

Definition

readonly size: number;

Methods

[Symbol.iterator]

Returns a FastIterator instance used to iterate over the values of this Iterable.

Definition

[Symbol.iterator](): FastIterator<T>;

Overrides

FastIterable.[Symbol.iterator]

assumeNonEmpty

Returns the same collection typed as non-empty.

Definition

assumeNonEmpty(): WithElem<Tp, T>['nonEmpty'];

throws

RimbuError.EmptyCollectionAssumedNonEmptyError if the collection is empty

example
HashSet.empty().assumeNonEmpty()          // => throws RimbuError.EmptyCollectionAssumedNonEmptyError
HashSet.from([[0, 1]]).assumeNonEmpty() // => List.NonEmpty(0, 1, 2)

difference

Returns a collection where each value of given other StreamSource is removed from this collection.

Definition

difference<U = T>(other: StreamSource<RelatedTo<T, U>>): WithElem<Tp, T>['normal'];

Type parameters

NameDefaultDescription
UT

Parameters

NameTypeDescription
otherStreamSource<RelatedTo<T, U>>a StreamSource containing values
example
HashSet.of(1, 2, 3).difference(HashSet.of(1, 3)).toArray()  // => [2]

filter

Returns a collection containing only those entries that satisfy given pred predicate.

Definitions

filter<TF extends T>(pred: (value: T, index: number, halt: () => void) => value is TF, options?: {
    negate?: false | undefined;
  }): WithElem<Tp, TF>['normal'];

filter<TF extends T>(pred: (value: T, index: number, halt: () => void) => value is TF, options: {
    negate: true;
  }): WithElem<Tp, Exclude<T, TF>>['normal'];

filter(pred: (value: T, index: number, halt: () => void) => boolean, options?: {
    negate?: boolean;
  }): WithElem<Tp, T>['normal'];

Type parameters

NameConstraintsDescription
TFT

Parameters

NameTypeDescription
pred(value: T, index: number, halt: () => void) => value is TFa predicate function receiving:
- value: the next value
- index: the entry index
- halt: a function that, when called, ensures no next elements are passed
options{
    negate?: false | undefined;
  }
(optional) an object containing the following properties:
- negate: (default: false) when true will negate the predicate
note

if the predicate is a type guard, the return type is automatically inferred

example
HashSet.of(1, 2, 3).filter(value < 3).toArray()
// => [1, 2]

forEach

Performs given function f for each value of the collection, using given state as initial traversal state.

Definition

forEach(f: (value: T, index: number, halt: () => void) => void, options?: {
    state?: TraverseState;
  }): void;

Parameters

NameTypeDescription
f(value: T, index: number, halt: () => void) => voidthe function to perform for each element, receiving: - value: the next element
- index: the index of the element
- halt: a function that, if called, ensures that no new elements are passed
options{
    state?: TraverseState;
  }
example
HashSet.of(1, 2, 3).forEach((value, i, halt) => {
console.log([value, i]);
if (i >= 1) halt();
})
// => logs [1, 0] [2, 1]
note

O(N)

has

Returns true if given value is in the collection.

Definition

has<U = T>(value: RelatedTo<T, U>): boolean;

Type parameters

NameDefaultDescription
UT

Parameters

NameTypeDescription
valueRelatedTo<T, U>the value to look for
example
HashSet.of(1, 2, 3).has(2)  // => true
HashSet.of(1, 2, 3).has(10) // => false

intersect

Returns a collection containing values that are both in this collection, and in the given other StreamSource.

Definition

intersect<U = T>(other: StreamSource<RelatedTo<T, U>>): WithElem<Tp, T>['normal'];

Type parameters

NameDefaultDescription
UT

Parameters

NameTypeDescription
otherStreamSource<RelatedTo<T, U>>a StreamSource containing values
example
HashSet.of(1, 2, 3).interface(HashSet.of(1, 3)).toArray()   // => [1, 3]

nonEmpty

Returns true if there is at least one entry in the collection, and instructs the compiler to treat the collection as a .NonEmpty type.

Definition

nonEmpty(): this is WithElem<Tp, T>['nonEmpty'];

example
const m: HashSet<number> = HashSet.of(1, 2, 2)
m.stream().first(0) // compiler allows fallback value since the Stream may be empty
if (m.nonEmpty()) {
m.stream().first(0) // compiler error: fallback value not allowed since Stream is not empty
}

remove

Returns the collection with given value removed.

Definition

remove<U = T>(value: RelatedTo<T, U>): WithElem<Tp, T>['normal'];

Type parameters

NameDefaultDescription
UT

Parameters

NameTypeDescription
valueRelatedTo<T, U>the value to remove
example
const s = HashSet.of(1, 2, 3)
s.remove(2).toArray() // => [1, 3]
s.remove(10).toArray() // => [1, 2, 3]

removeAll

Returns the collection with all values in the given values StreamSource removed.

Definition

removeAll<U = T>(values: StreamSource<RelatedTo<T, U>>): WithElem<Tp, T>['normal'];

Type parameters

NameDefaultDescription
UT

Parameters

NameTypeDescription
valuesStreamSource<RelatedTo<T, U>>a StreamSource containing values to remove
example
HashSet.of(1, 2, 3).removeAll([1, 3]).toArray()
// => [2]

stream

Returns a Stream containing all elements of this collection.

Definition

stream(): Stream<T>;

example
HashSet.of(1, 2, 3).stream().toArray()   // => [1, 2, 3]

toArray

Returns an array containing all values in this collection.

Definition

toArray(): T[];

example
HashSet.of(1, 2, 3).toArray()   // => [1, 2, 3]
note

O(log(N)) @note it is safe to mutate the returned array, however, the array elements are not copied, thus should be treated as read-only

toJSON

Returns a JSON representation of this collection.

Definition

toJSON(): ToJSON<T[]>;

example
HashSet.of(1, 2, 3).toJSON()   // => { dataType: 'HashSet', value: [1, 2, 3] }

toString

Returns a string representation of this collection.

Definition

toString(): string;

example
HashSet.of(1, 2, 3).toString()   // => HashSet(1, 2, 3)