Skip to main content

interface SortedSet<T>

A type-invariant immutable Set of value type T. In the Set, there are no duplicate values. See the Set documentation and the SortedSet API documentation

Companion namespace: SortedSet

Type parameters​

NameDescription
Tthe value type
note
  • The SortedSet keeps the inserted values in sorted order according to the context's comp Comp instance.
example
const s1 = SortedSet.empty<string>()
const s2 = SortedSet.of('a', 'b', 'c')

Methods​

drop​

Returns a SortedSet containing all but the first amount of values of this SortedSet.

Definition​

drop(amount: number): SortedSet<T>;

Parameters​

NameTypeDescription
amountnumberthe amount of elements to keep
note

a negative amount drops the last values instead of the first, e.g. -2 is the last 2 elements

example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.drop(2).toArray())
// => ['c', 'd']
console.log(m.drop(-2).toArray())
// => ['a', 'b']

findIndex​

Returns the index of the given value in the SortedSet, or -1 if the value is not present.

Definition​

findIndex(value: T): number;

Parameters​

NameTypeDescription
valueTthe value to find the index for
example
const m = SortedSet.of('b', 'd', 'a', 'c');
console.log(m.findIndex('c'))
// => 2
console.log(m.findIndex('q'))
// => -1

getAtIndex​

Returns the value at the given index of the value sort order of the SortedSet, or a fallback value (default: undefined) if the index is out of bounds.

Definitions​

getAtIndex(index: number): T | undefined;

getAtIndex<O>(index: number, otherwise: OptLazy<O>): T | O;

Parameters​

NameTypeDescription
indexnumberthe index in the key sort order
note

negative index values will retrieve the values from the end of the sort order, e.g. -1 is the last value

example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.getAtIndex(1))
// => 'b'
console.log(m.getAtIndex(-1))
// => 'd'
console.log(m.getAtIndex(10))
// => undefined
console.log(m.getAtIndex(10, 'q'))
// => 'q'

max​

Returns the maximum value of the SortedSet, or a fallback value (default: undefined) if the SortedSet is empty.

Definitions​

max(): T | undefined;

max<O>(otherwise: OptLazy<O>): T | O;

example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.max())
// => 'd'
console.log(m.max('q'))
// => 'd'
console.log(SortedSet.empty().max())
// => undefined
console.log(SortedSet.empty().max('q'))
// => 'q'

min​

Returns the minimum value of the SortedSet, or a fallback value (default: undefined) if the SortedSet is empty.

Definitions​

min(): T | undefined;

min<O>(otherwise: OptLazy<O>): T | O;

example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.min())
// => 'a'
console.log(m.min('q'))
// => 'a'
console.log(SortedSet.empty().min())
// => undefined
console.log(SortedSet.empty().min('q'))
// => 'q'

slice​

Returns a SortedSet containing only those values that are within the given keyRange.

Definition​

slice(range: Range<T>): SortedSet<T>;

Parameters​

NameTypeDescription
rangeRange<T>a Range defining the values to include
example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.slice({ start: 'b', end: 'c' }).toArray())
// => ['b', 'c']

sliceIndex​

Returns a SortedSet containing only those values that are within the given range index range of the value sort order.

Definition​

sliceIndex(range: IndexRange): SortedSet<T>;

Parameters​

NameTypeDescription
rangeIndexRangean IndexRange defining the sort order indices to include.
example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.sliceIndex({ start: 1, amount: 2 }).toArray())
// => ['b', 'c']

stream​

undocumented

Definition​

stream(options?: {
    reversed?: boolean;
  }): Stream<T>;

Parameters​

NameTypeDescription
options{
    reversed?: boolean;
  }

streamRange​

Returns a Stream of sorted values of this collection within the given keyRange.

Definition​

streamRange(range: Range<T>, options?: {
    reversed?: boolean;
  }): Stream<T>;

Parameters​

NameTypeDescription
rangeRange<T>
options{
    reversed?: boolean;
  }
(optional) an object containing the following properties:
- reversed: (default: false) when true will reverse the stream element order
example
const m = SortedSet.of('b', 'd', 'a', 'c');
console.log(m.streamRange({ start: 'b', end: 'c' }).toArray())
// => ['b', 'c']

streamSliceIndex​

Returns a Stream of sorted values of this collection within the given range index range.

Definition​

streamSliceIndex(range: IndexRange, options?: {
    reversed?: boolean;
  }): Stream<T>;

Parameters​

NameTypeDescription
rangeIndexRangethe range of values to include in the stream
options{
    reversed?: boolean;
  }
(optional) an object containing the following properties:
- reversed: (default: false) when true will reverse the stream element order
example
const m = SortedSet.of('b', 'd', 'a', 'c');
console.log(m.streamSliceIndex({ start: 1, amount: 2 }).toArray())
// => ['b', 'c']

take​

Returns a SortedSet containing the first amount of values of this SortedSet.

Definition​

take(amount: number): SortedSet<T>;

Parameters​

NameTypeDescription
amountnumberthe amount of elements to keep
note

a negative amount takes the last values instead of the first, e.g. -2 is the last 2 elements

example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.take(2).toArray())
// => ['a', 'b']
console.log(m.take(-2).toArray())
// => ['c', 'd']