Skip to main content

interface List.NonEmpty<T>

A non-empty random accessible immutable sequence of values of type T. See the List documentation and the List API documentation

Extends: Streamable.NonEmpty<T>, List<T>

Type parameters

NameDescription
Tthe value type
example
const l = List.of(1, 3, 2)

Properties

context

The list context that acts as a factory for all related list instances.

Definition

readonly context: List.Context;

Overrides

List.context

isEmpty

Returns false since this collection is known to be non-empty.

Definition

readonly isEmpty: false;

example
List(0, 1, 2).isEmpty   // => false

Overrides

List.isEmpty

length

Returns the number of values in the collection

Definition

readonly length: number;

example
List.empty().length      // => 0
List.of(0, 1, 2).length // => 3

Overrides

List.length

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]

append

Returns the List with the given value added to the end.

Definition

append(value: T): List.NonEmpty<T>;

Parameters

NameTypeDescription
valueTthe value to append.
example
List.of(0, 1, 2).append(-10)  // => List(0, 1, 2, -10)
note

O(logB(N)) for block size B - mostly o(1)

Overrides

List.append

asNormal

Returns this collection typed as a 'possibly empty' collection.

Definition

asNormal(): List<T>;

example
List.of(0, 1, 2).asNormal();  // type: List<number>

assumeNonEmpty

Returns a self reference since this collection is known to be non-empty.

Definition

assumeNonEmpty(): this;

example
const m = List.of(0, 1, 2);
m === m.assumeNonEmpty() // => true

Overrides

List.assumeNonEmpty

collect

Returns a List containing the values resulting from applying given collectFun to each value in this List.

Definition

collect<T2>(collectFun: CollectFun<T, T2>, range?: IndexRange, reversed?: boolean): List<T2>;

Type parameters

NameDescription
T2the result element type

Parameters

NameTypeDescription
collectFunCollectFun<T, T2>a function receiving
- value: the next value
- index: the value index
- skip: a token that, when returned, will not add a value to the resulting collection
- halt: a function that, when called, ensures no next elements are passed
rangeIndexRange(optional) the range of the list to include in the filtering process
reversedboolean(default: false) if true reverses the elements within the given range
example
List.of(0, 1, 2, 3).collect(v => v > 1)
// => List(false, false, true, true)
List.of(0, 1, 2, 3).collect((v, i, skip) => v === 1 ? skip : v * 2)
// => List(0, 4, 6)
List.of(0, 1, 2, 3).collect((v, i, skip, halt) => {
if (v > 1) halt()
return v * 2
})
// => List(0, 2)

Overrides

List.collect

concat

Returns the non-empty List succeeded by the values from all given StreamSource instances given in sources.

Definition

concat<T2 = T>(...sources: ArrayNonEmpty<StreamSource<T2>>): List.NonEmpty<T | T2>;

Type parameters

NameDefaultDescription
T2Tthe type of the source elements to add

Parameters

NameTypeDescription
sourcesArrayNonEmpty<StreamSource<T2>>an array of StreamSource instances containing values to be added to the list
note

this operation is most efficient when the given sources are instances of List from the same context.

example
List.of(0, 1, 2).concat([10, 11])                      // -> List(0, 1, 2, 10, 11)
List.of(0, 1, 2).concat([10, 11], new Set([12, 13])) // -> List(0, 1, 2, 10, 11, 12, 13)
note

O(logB(N)) for block size B

Overrides

List.concat

drop

Returns a List skipping the first given amount elements of this List.

Definition

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

Parameters

NameTypeDescription
amountnumberthe desired amount of values of to include
note

a negative index will be treated as follows:
- -1: the last element in the list
- -2: the second-last element in the list
- ...etc

example
List.of(0, 1, 2, 3).drop(2)    // => List(2, 3)
List.of(0, 1, 2, 3).drop(10) // => List()
List.of(0, 1, 2, 3).drop(-2) // => List(0, 1)
note

O(logB(N)) for block size B

Overrides

List.drop

filter

Returns a List containing only those values within optionally given range that satisfy given pred predicate. If reversed is true, the order of the values is reversed.

Definition

filter(pred: (value: T, index: number, halt: () => void) => boolean, range?: IndexRange, reversed?: boolean): List<T>;

Parameters

NameTypeDescription
pred(value: T, index: number, halt: () => void) => booleana predicate function receiving
- value: the next value
- index: the value index
- halt: a function that, when called, ensures no next elements are passed
rangeIndexRange(optional) the range of the list to include in the filtering process
reversedboolean(default: false) if true reverses the elements within the given range
example
List.of(0, 1, 2, 3).filter(v => v < 2)           // -> List(0, 1)
List.of(0, 1, 2, 3).filter((v, _, halt) => {
if (v > 1) halt();
return v;
}) // -> List(0, 1, 2)
List.of(0, 1, 2, 3)
.filter((_, i) => i > 1, undefined, true) // -> List(1, 0)

Overrides

List.filter

first

Returns the first value of the List.

Definition

first(): T;

example
List.of(0, 1, 2).first()   // => 0
note

O(1)

Overrides

List.first

flatMap

Returns a List containing the joined results of applying given flatMapFun to each value in this List.

Definitions

flatMap<T2>(flatMapFun: (value: T, index: number) => StreamSource.NonEmpty<T2>, range?: undefined, reversed?: boolean): List.NonEmpty<T2>;

flatMap<T2>(flatMapFun: (value: T, index: number) => StreamSource<T2>, range?: IndexRange, reversed?: boolean): List<T2>;

Type parameters

NameDescription
T2the result element type

Parameters

NameTypeDescription
flatMapFun(value: T, index: number) => StreamSource.NonEmpty<T2>a function taking the next value and its index, and returning a StreamSource of value to include in the resulting collection
rangeundefined(optional) the range of the list to include in the filtering process
reversedboolean(default: false) if true reverses the elements within the given range
example
List.of(1, 2, 3).flatMap(v => [v, v + 1]).toArray()
// => [1, 2, 2, 3, 3, 4]

Overrides

List.flatMap

forEach

Performs given function f for each value of the List.

Definition

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

Parameters

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

O(N)

Overrides

List.forEach

get

Returns the value in the List at the given index.

Definitions

get(index: number): T | undefined;

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

Parameters

NameTypeDescription
indexnumberthe element index
note

a negative index will be treated as follows:
- -1: the last value in the list
- -2: the second-last value in the list
- ...etc

example
List.of(0, 1, 2).get(5)             // => undefined
List.of(0, 1, 2).get(5, 'other') // => 'other'
List.of(0, 1, 2).get(1, 'other') // => 1
List.of(0, 1, 2).get(-1) // => 2
note

O(logB(N)) for block size B

Overrides

List.get

insert

Returns the non-empty List with the given values inserted at the given index.

Definition

insert(index: number, values: StreamSource<T>): List.NonEmpty<T>;

Parameters

NameTypeDescription
indexnumberthe index at which to insert the values
valuesStreamSource<T>a StreamSource of values to insert
note

a negative index will be treated as follows:
- -1: the last element in the list
- -2: the second-last element in the list
- ...etc

example
List.of(0, 1, 2, 3).insert(2, [10, 11])   // -> List(0, 1, 10, 11, 2, 3)
List.of(0, 1, 2, 3).insert(-1, [10, 11]) // -> List(0, 1, 2, 1, 11, 3)
note

O(logB(N)) for block size B

Overrides

List.insert

last

Returns the last value of the List.

Definition

last(): T;

example
List.of(0, 1, 2).last()   // => 2
note

O(1)

Overrides

List.last

map

Returns a non-empty List containing the result of applying given mapFun to each value in this List. If reversed is true, the order of the values is reversed.

Definition

map<T2>(mapFun: (value: T, index: number) => T2, reversed?: boolean): List.NonEmpty<T2>;

Type parameters

NameDescription
T2

Parameters

NameTypeDescription
mapFun(value: T, index: number) => T2a function receiving a value and its index, and returning a new value
reversedboolean(default: false) if true, reverses the order of the values
example
List.of(1, 2, 3).map(v => `value: ${v + 2}`).toArray()
// => ['value 2', 'value 3', 'value 3']

Overrides

List.map

mapPure

Returns a non-empty List containing the result of applying given mapFun to each value in this List. If reversed is true, the order of the values is reversed.

Definition

mapPure<T2>(mapFun: (value: T) => T2, reversed?: boolean): List.NonEmpty<T2>;

Type parameters

NameDescription
T2the result element type

Parameters

NameTypeDescription
mapFun(value: T) => T2a function receiving a value and returning a new value
reversedboolean(default: false) if true, reverses the order of the values
note

The given mapFun is expected to be side-effect free, so that structural sharing can be kept in place.

example
List.of(1, 2, 3).mapPure(v => `value: ${v + 2}`).toArray()
// => ['value 2', 'value 3', 'value 3']

Overrides

List.mapPure

nonEmpty

Returns true since this collection is known to be non-empty

Definition

nonEmpty(): true;

example
List.of(0, 1, 2).nonEmpty()   // => true

Overrides

List.nonEmpty

padTo

Returns the non-empty List where, if given length is larger than the List length, the given fill value is added to the start and/or end of the List according to the positionPercentage such that the result length is equal to length.

Definition

padTo(length: number, fill: T, positionPercentage?: number): List.NonEmpty<T>;

Parameters

NameTypeDescription
lengthnumberthe target length of the resulting list
fillTthe element used to fill up empty space in the resulting List
positionPercentagenumber(optional) a percentage indicating how much of the filling elements should be on the left side of the current List
example
List.of(0, 1).padTo(4, 10)       // -> List(0, 1, 10, 10)
List.of(0, 1).padTo(4, 10, 50) // -> List(10, 0, 1, 10)
List.of(0, 1).padTo(4, 10, 100) // -> List(0, 1, 10, 10)
List.of(0, 1, 2).padTo(2, 10) // -> List(0, 1, 2)
note

O(logB(N)) for block size B

Overrides

List.padTo

prepend

Returns the List with the given value added to the start.

Definition

prepend(value: T): List.NonEmpty<T>;

Parameters

NameTypeDescription
valueTthe value to prepend
example
List.of(0, 1, 2).prepend(-10)  // => List(-10, 0, 1, 2)
note

O(logB(N)) for block size B - mostly o(1)

Overrides

List.prepend

remove

Returns the List with the given amount of values removed at the given index.

Definition

remove(index: number, amount?: number): List<T>;

Parameters

NameTypeDescription
indexnumberthe index at which to remove values
amountnumberthe amount of elements to remove
note

a negative index will be treated as follows:
- -1: the last element in the list
- -2: the second-last element in the list
- ...etc

example
List.of(0, 1, 2, 3).remove(1, 2)  // -> List(0, 3)
List.of(0, 1, 2, 3).remove(-2, 1) // -> List(0, 1, 3)
note

O(logB(N)) for block size B

Overrides

List.remove

repeat

Returns a non-empty List that contains this List the given amount of times.

Definition

repeat(amount: number): List.NonEmpty<T>;

Parameters

NameTypeDescription
amountnumberthe amount of times to repeat the values in this List
note

if the given amount <= 1, the List itself is returned

example
List.of(0, 1, 2).repeat(2)   // -> List(0, 1, 2, 0, 1, 2)
List.of(0, 1, 2).repeat(0) // -> List(0, 1, 2)
note

O(logB(N)) for block size B

Overrides

List.repeat

reversed

Returns the non-empty List in reversed order.

Definition

reversed(): List.NonEmpty<T>;

example
List.of(0, 1, 2).reversed()  // -> List(2, 1, 0)
note

O(logB(n)) for block size B

Overrides

List.reversed

rotate

Returns the non-empty List where the first given shiftAmount of values are removed from this List, and are appended at the end.

Definition

rotate(shiftAmount: number): List.NonEmpty<T>;

Parameters

NameTypeDescription
shiftAmountnumberthe amount of values to rotate
note

if the shiftAmount is negative, the last shiftAmount values will be removed from the List and will be prepended.

example
List.of(0, 1, 2, 3).rotate(2)   // -> List(2, 3, 0, 1)
List.of(0, 1, 2, 3).rotate(-1) // -> List(1, 2, 3, 0)
note

O(logB(N)) for block size B

Overrides

List.rotate

slice

Returns the List containing the values within the given index range, potentially reversed in order if reversed is true.

Definition

slice(range: IndexRange, reversed?: boolean): List<T>;

Parameters

NameTypeDescription
rangeIndexRangethe index range to include
reversedboolean(default: false) if true reverses the order of the elements
note

a negative index will be treated as follows:
- -1: the last element in the list
- -2: the second-last element in the list
- ...etc

example
List.of(0, 1, 2, 3).slice({ start: 1, amount: 2 })        // -> List(1, 2)
List.of(0, 1, 2, 3).slice({ start: -2, amount: 2 }, true) // -> List(3, 2)
note

O(logB(N)) for block size B

Overrides

List.slice

splice

Returns the List, where at the given index the remove amount of values are replaced by the values from the optionally given insert StreamSource.

Definitions

splice(options: {
      index: number;
      remove?: number;
      insert: StreamSource.NonEmpty<T>;
    }): List.NonEmpty<T>;

splice(options: {
      index: number;
      remove?: number;
      insert?: StreamSource<T>;
    }): List<T>;

Parameters

NameTypeDescription
options{
      index: number;
      remove?: number;
      insert: StreamSource.NonEmpty<T>;
    }
object containing the following
- index: the index at which to replace values
- remove: (default: 0) the amount of values to remove
- insert: (default: []) a StreamSource of values to insert
note

a negative index will be treated as follows: - -1: the last element in the list - -2: the second-last element in the list - ...etc

example
List.of(0, 1, 2, 3).splice({ index: 2, remove: 1 })                    // -> List(0, 1, 3)
List.of(0, 1, 2, 3).splice({ index: 1, remove: 2, insert: [10, 11] }) // -> List(0, 10, 11, 3)
note

O(logB(N)) for block size B

Overrides

List.splice

stream

Returns a non-empty Stream containing the values in order of the List, or in reverse order if reversed is true.

Definition

stream(reversed?: boolean): Stream.NonEmpty<T>;

Parameters

NameTypeDescription
reversedboolean(default: false) if true reverses the order of the elements
example
List.of(0, 1, 2).stream().toArray()      // => [0, 1, 2]
List.of(0, 1, 2).stream(true).toArray() // => [2, 1, 0]

Overrides

NonEmpty.stream, List.stream

streamRange

Returns a Stream containing the values contained in the given index range, in order of the List, or in reverse order if reversed is true.

Definition

streamRange(range: IndexRange, reversed?: boolean): Stream<T>;

Parameters

NameTypeDescription
rangeIndexRangethe index range to include from the list
reversedboolean(default: false) if true reverses the order of the included elements
example
List.of(0, 1, 2, 3, 4).streamRange({ start: 1, end: 2 }).toArray()             // => [0, 1, 2]
List.of(0, 1, 2, 3, 4).streamRange({ start: 1, amount: 2 }, true).toArray() // => [2, 1]

Overrides

List.streamRange

take

Returns a List containing the first (or last) given amount values of this List.

Definition

take<N extends number>(amount: N): 0 extends N ? List<T> : List.NonEmpty<T>;

Type parameters

NameConstraintsDescription
Nnumberthe literal numeric type of amount

Parameters

NameTypeDescription
amountNthe desired amount of values of to include
note

a negative index will be treated as follows:
- -1: the last element in the list
- -2: the second-last element in the list
- ...etc

example
List.of(0, 1, 2, 3).take(2)    // => List(0, 1)
List.of(0, 1, 2, 3).take(10) // => List(0, 1, 2, 3)
List.of(0, 1, 2, 3).take(-2) // => List(2, 3)
note

O(logB(N)) for block size B

Overrides

List.take

toArray

Returns an array containing the values within given range (default: all) in this collection. If reversed is true, reverses the order of the values.

Definitions

toArray(range?: undefined, reversed?: boolean): ArrayNonEmpty<T>;

toArray(range?: IndexRange, reversed?: boolean): T[];

Parameters

NameTypeDescription
rangeundefined(optional) the range of the list to include in the filtering process
reversedboolean(default: false) if true reverses the elements within the given range
example
List.of(0, 1, 2, 3).toArray()                      // => [0, 1, 2, 3]
List.of(0, 1, 2, 3).toArray({ amount: 2 }) // => [0, 1]
List.of(0, 1, 2, 3).toArray({ amount: 2 }, true) // => [1, 0]
note

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

Overrides

List.toArray

toBuilder

Returns a builder object containing the values of this collection.

Definition

toBuilder(): List.Builder<T>;

example
const builder: List.Builder<number> = List.of(0, 1, 2, 3).toBuilder()

Overrides

List.toBuilder

toJSON

Returns a JSON representation of this collection.

Definition

toJSON(): ToJSON<T[], this['context']['typeTag']>;

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

Overrides

List.toJSON

toString

Returns a string representation of this collection.

Definition

toString(): string;

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

Overrides

List.toString

updateAt

Returns the non-empty List where at the given index the value is replaced or updated by the given update.

Definition

updateAt(index: number, update: Update<T>): List.NonEmpty<T>;

Parameters

NameTypeDescription
indexnumberthe index at which to update the value
updateUpdate<T>a new value or function taking the current value and returning a new value
note

a negative index will be treated as follows:
- -1: the last element in the list
- -2: the second-last element in the list
- ...etc

example
List.of(0, 1, 2).updateAt(1, 10)            // -> List(0, 10, 2)
List.of(0, 1, 2).updateAt(1, v => v + 1) // -> List(0, 2, 2)
List.of(0, 1, 2).updateAt(-1, 10) // -> List(0, 1, 10)
note

O(logB(N)) for block size B

Overrides

List.updateAt