Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Lazy<TElement>

The base class that all lazy iterable objects derive from. This can be extended with custom iterators if needed.

Type parameters

  • TElement

Hierarchy

  • Lazy

Implements

  • Iterable<TElement>

Index

Constructors

constructor

  • new Lazy<TElement>(): Lazy<TElement>
  • Type parameters

    • TElement

    Returns Lazy<TElement>

Methods

Abstract [Symbol.iterator]

  • [Symbol.iterator](): Iterator<TElement, any, undefined>
  • Returns Iterator<TElement, any, undefined>

aggregate

  • aggregate(agg: AggFn<TElement, TElement>): TElement
  • aggregate<TAcc>(agg: AggFn<TElement, TAcc>, seed: TAcc): TAcc
  • Applies an accumulator function over an interable.

    throws

    {Error} If the iterable was empty.

    remarks

    The function works very similarly to Array.prototype.reduce, with the added benefit of working on any general iterable object. This will cause a complete iteration of the iterable object.

    Parameters

    • agg: AggFn<TElement, TElement>

      The accumulator function to apply over the iterable.

    Returns TElement

    The final accumulator value.

  • Applies an accumulator function over an interable.

    remarks

    The function works very similarly to Array.prototype.reduce, with the added benefit of working on any general iterable object. This will cause a complete iteration of the iterable object.

    Type parameters

    • TAcc

    Parameters

    • agg: AggFn<TElement, TAcc>

      The accumulator function to apply over the iterable.

    • seed: TAcc

      The seed to set the initial acc param to in the accumulator function. If not given, then the first element is used.

    Returns TAcc

    The final accumulator value.

all

  • all(predicate: BoolPredicate<TElement>): boolean
  • Returns whether all elements satisfy the given condition.

    remarks

    This will iterate until the condition is false or until the iterable ends.

    Parameters

    • predicate: BoolPredicate<TElement>

      The function to use to test each element.

    Returns boolean

    Whether all elements satisfied the condition.

any

  • any(): boolean
  • any(predicate: BoolPredicate<TElement>): boolean
  • Returns whether the iterable is not empty.

    remarks

    For checking whether the given lazy query has any elements, prefer to use this function over Lazy.count, as that function will iterate the entire object, whereas this will stop at the first. This will iterate only a single time.

    Returns boolean

    Whether the iterable is not empty.

  • Returns whether any of the elements satisfy the given condition.

    remarks

    For checking whether the given lazy query has any elements, prefer to use this function over Lazy.count, as that function will iterate the entire object, whereas this will stop at the first element that satisfies the condition. This will iterate until the condition is true or until the iterable ends.

    Parameters

    • predicate: BoolPredicate<TElement>

      The function to use to test each element.

    Returns boolean

    Whether any element in the iterable satisfied the condition. If the iterable was empty, then false is returned

append

  • append(element: TElement): Lazy<TElement>
  • Appends the element to the end of the iterable.

    remarks

    Does not cause additional unexpected iteration.

    Parameters

    • element: TElement

      The element to append.

    Returns Lazy<TElement>

apply

  • apply<TLazy, TResult>(fn: (t: Lazy<TElement>) => TLazy): Lazy<TResult>
  • Applies the given lazy iterable implementation to the current object. This allows for using custom Lazy implementations using the standard chaining syntax.

    Type parameters

    • TLazy: Lazy<TResult, TLazy>

    • TResult = TElement

    Parameters

    • fn: (t: Lazy<TElement>) => TLazy

      The function that will create the iterable instance using the current object.

        • (t: Lazy<TElement>): TLazy
        • Parameters

          Returns TLazy

    Returns Lazy<TResult>

    The instantiated iterable object.

average

  • average(): TElement extends number ? number : never
  • average(selector: MapFn<TElement, number>): number
  • Computes the average of the iterable.

    throws

    {TypeError} If any element in the iterable was a non-number.

    throws

    {Error} If the iterable was empty.

    remarks

    This will cause a complete iteration of the iterable object.

    Returns TElement extends number ? number : never

    The numeric average of the iterable.

  • Computes the average of result of the selector function over the iterable.

    throws

    {Error} If the iterable was empty.

    remarks

    This will cause a complete iteration of the iterable object.

    Parameters

    • selector: MapFn<TElement, number>

      The transformation function to use for each element.

    Returns number

    The numeric average of the results of the selector function.

batchIn

  • batchIn(batchSize: number, includeIncomplete?: boolean): Lazy<Iterable<TElement>>
  • Batches elements in groups of the given batch size.

    remarks

    Does not cause additional unexpected iteration.

    Parameters

    • batchSize: number

      The size of each batch.

    • includeIncomplete: boolean = true

      Whether to include batches that are smaller than the target size. This only applies to the final batch of the iterable if the total size is not a multiple of the batch size.

    Returns Lazy<Iterable<TElement>>

cache

  • cache(): Lazy<TElement>
  • Resolves the underlaying iterable completely and returns a lazy of the result.

    remarks

    This will completely iterate the underlaying iterable, and return a completely new lazy object of the result. This allows for some optimisation when a chain has become complex and iteration of it is needed multiple times (as the resulting iterable will only be calulated once, and then reused). This can also be used to ensure that side-effects of the chain are only done once, which can be useful in more complicated computations. Since this will return a completely new Lazy iterable, it means that, if no other references exist, then the previous chain (and even the original iterable) can be freed for garbage collection, potentially helping with memory.

    Returns Lazy<TElement>

concat

  • concat(...iterables: Iterable<TElement>[]): Lazy<TElement>
  • Concatinates multiple iterables in order.

    remarks

    Does not cause additional unexpected iteration.

    Parameters

    • Rest ...iterables: Iterable<TElement>[]

      The other iterables to concatinate with.

    Returns Lazy<TElement>

contains

  • contains(element: TElement, comparer?: ComparerFn<TElement>): boolean
  • Determines whether the iterable has a given element.

    remarks

    This will iterable until the given value is found, or until the iterable ends.

    Parameters

    • element: TElement

      The value to search for.

    • Optional comparer: ComparerFn<TElement>

      The function that compares 2 elements and returns a boolean on whether they are equal or not. If not given, defaults to strict equals (===).

    Returns boolean

    Whether the element was in the iterable.

count

  • count(): number
  • count(predicate: BoolPredicate<TElement>): number
  • Returns the number of elements in the iterable.

    remarks

    To determine whether an iterable has any elements, prefer the Lazy.any method, as this will iterate the entire iterable regardless. This will cause a complete iteration of the iterable object.

    Returns number

    The number of elements in the iterable.

  • Returns the number of elements that satify the given condition.

    remarks

    To determine whether an iterable has any elements, prefer the Lazy.any method, as this will iterate the entire iterable regardless. This will cause a complete iteration of the iterable object.

    Parameters

    • predicate: BoolPredicate<TElement>

      The predicate to test each element with.

    Returns number

    The number of elements in the iterable that matched the condition.

defaultIfEmpty

  • defaultIfEmpty(defaultValue: TElement): Lazy<TElement>
  • Returns the elements in the iterable, or the given default value as the only element if it contained none.

    remarks

    Does not cause additional unexpected iteration.

    Parameters

    • defaultValue: TElement

      The value to use if the iterable was empty.

    Returns Lazy<TElement>

distinct

  • distinct<TKey>(compareOn?: MapFn<TElement, TKey>): Lazy<TElement>
  • Returns the distinct elements in the iterable.

    remarks

    Does not cause additional unexpected iteration.

    Type parameters

    • TKey

    Parameters

    • Optional compareOn: MapFn<TElement, TKey>

      A mapping function to get the key to compare with. The result will be effectively compared using a strict equals (===) against the others. If not given, then each element will used directly.

    Returns Lazy<TElement>

elementAt

  • elementAt(index: number): TElement
  • Returns the element at the given index of the iterable.

    throws

    {Error} If the index was < 0 or if it is >= the length of the iterable.

    remarks

    The will iterate until the specified index, or until the iterable ends.

    Parameters

    • index: number

      The index of the element to get.

    Returns TElement

    The element at the given index.

elementAtOrDefault

  • elementAtOrDefault(index: number, defaultValue: TElement): TElement
  • elementAtOrDefault<TDefault>(index: number, defaultValue: TDefault): TElement | TDefault
  • Returns the element at the given index of the iterable, or the given default value if out of range.

    remarks

    This will iterable until the specified index, or unitl the iterable ends.

    Parameters

    • index: number

      The index of the element to get.

    • defaultValue: TElement

      The default value to use if the index was out of range.

    Returns TElement

    The element at the given index.

  • Returns the element at the given index of the iterable, or the given default value if out of range.

    remarks

    This will iterable until the specified index, or unitl the iterable ends.

    Type parameters

    • TDefault

    Parameters

    • index: number

      The index of the element to get.

    • defaultValue: TDefault

      The default value to use if the index was out of range.

    Returns TElement | TDefault

    The element at the given index.

except

  • except<TKey>(second: Iterable<TElement>, compareOn?: MapFn<TElement, TKey>): Lazy<TElement>
  • Returns the set difference between 2 iterables. This like doing an XOR over the 2 iterables.

    remarks

    This will iterate the second iterable completely once it has started iteration (not before). It will not cause additional unexpected iteration on the underlying iterable.

    Type parameters

    • TKey = TElement

    Parameters

    • second: Iterable<TElement>

      The iterable to get the difference of.

    • Optional compareOn: MapFn<TElement, TKey>

      A mapping function to get the key to compare with. The value will be effectively compared using a strict equals (===) againt the others. If not given, then each element will used directly.

    Returns Lazy<TElement>

first

  • first(): TElement
  • first(predicate: BoolPredicate<TElement>): TElement
  • Returns the first element in the iterable.

    throws

    {Error} If the iterable was empty.

    remarks

    This will only iterate a single time.

    Returns TElement

    The first element in the iterable.

  • Returns the first element that satisfies the given condition.

    throws

    {Error} If the iterable was empty.

    remarks

    This will iterate until the condition is satisfied, or until the iterable ends.

    Parameters

    • predicate: BoolPredicate<TElement>

      The predicate to test each element with.

    Returns TElement

    The first element in the iterable that satisfies the condition.

firstOrDefault

  • firstOrDefault(defaultValue: TElement): TElement
  • firstOrDefault<TDefault>(defaultValue: TDefault): TElement | TDefault
  • firstOrDefault(defaultValue: TElement, predicate: BoolPredicate<TElement>): TElement
  • firstOrDefault<TDefault>(defaultValue: TDefault, predicate: BoolPredicate<TElement>): TElement | TDefault
  • Returns the first element in the iterable, or the given default value if the iterable was empty.

    remarks

    This will only iterate a single time.

    Parameters

    • defaultValue: TElement

      The value to use of the iterable was empty.

    Returns TElement

    The first element in the iterable, or the default value if empty.

  • Returns the first element in the iterable, or the given default value if the iterable was empty.

    remarks

    This will only iterate a single time.

    Type parameters

    • TDefault

    Parameters

    • defaultValue: TDefault

      The value to use of the iterable was empty.

    Returns TElement | TDefault

    The first element in the iterable, or the default value if empty.

  • Returns the first element in the iterable that satisfies the condition, or the given default value.

    remarks

    This will iterate until the condition is satisfied, or until the iterable ends.

    Parameters

    • defaultValue: TElement

      The value to use if no element satisfied the condition.

    • predicate: BoolPredicate<TElement>

      The predicate to test each element with.

    Returns TElement

    The first element in the iterable that satisfies the condition, or the default value if none satisfied it.

  • Returns the first element in the iterable that satisfies the condition, or the given default value.

    remarks

    This will iterate until the condition is satisfied, or until the iterable ends.

    Type parameters

    • TDefault

    Parameters

    • defaultValue: TDefault

      The value to use if no element satisfied the condition.

    • predicate: BoolPredicate<TElement>

      The predicate to test each element with.

    Returns TElement | TDefault

    The first element in the iterable that satisfies the condition, or the default value if none satisfied it.

forEach

  • forEach(callbackFn: CallbackFn<TElement>): void
  • Mimics the behaviour of Array.prototype.forEach, with the exception of not providing the entire array as the 3rd param of the callback.

    remarks

    This will cause a complete iteration of the iterable object.

    Parameters

    • callbackFn: CallbackFn<TElement>

      The callback function that will be executed for each element in the iterable.

    Returns void

groupBy

  • groupBy<TKey>(keyFn: MapFn<TElement, TKey>): Lazy<IGrouping<TKey, TElement>>
  • groupBy<TKey, TItem>(keyFn: MapFn<TElement, TKey>, elementSelector: MapFn<TElement, TItem>): Lazy<IGrouping<TKey, TItem>>
  • groupBy<TKey, TItem, TResult>(keyFn: MapFn<TElement, TKey>, elementSelector: MapFn<TElement, TItem>, resultSelector: CombineFn<TKey, Iterable<TItem>, TResult>): Lazy<TResult>
  • Groups the elements by key.

    remarks

    When this is iterated (not before), the underlying iterator is walked through completely.

    Type parameters

    • TKey

    Parameters

    • keyFn: MapFn<TElement, TKey>

      The function to extract the key from each element.

    Returns Lazy<IGrouping<TKey, TElement>>

  • Groups the elements by key and projects each element using the given function.

    remarks

    When this is iterated (not before), the underlying iterator is walked through completely.

    Type parameters

    • TKey

    • TItem

    Parameters

    • keyFn: MapFn<TElement, TKey>

      The function to extract the key from each element.

    • elementSelector: MapFn<TElement, TItem>

      The transformation function to use for each element.

    Returns Lazy<IGrouping<TKey, TItem>>

  • Groups the elements by key and projects each element using the given function. The elements of each group are projected using the given function.

    remarks

    When this is iterated (not before), the underlying iterator is walked through completely.

    Type parameters

    • TKey

    • TItem

    • TResult

    Parameters

    • keyFn: MapFn<TElement, TKey>

      The function to extract the key from each element.

    • elementSelector: MapFn<TElement, TItem>

      The transformation function to use for each element.

    • resultSelector: CombineFn<TKey, Iterable<TItem>, TResult>

      The transformation function to create the result elements with.

    Returns Lazy<TResult>

groupJoin

  • groupJoin<TSecond, TKey, TResult>(second: Iterable<TSecond>, firstKeyFn: MapFn<TElement, TKey>, secondKeyFn: MapFn<TSecond, TKey>, joinFn: CombineFn<TElement, Iterable<TSecond>, TResult>): Lazy<TResult>
  • Joins 2 iterables on the given key and groups the results.

    remarks

    This will iterate the second iterable completely once it has started iteration (not before). It will not cause additional unexpected iteration on the underlying iterable.

    Type parameters

    • TSecond

    • TKey

    • TResult

    Parameters

    • second: Iterable<TSecond>

      The other iterable to group join with.

    • firstKeyFn: MapFn<TElement, TKey>

      The function that extracts the key from an element of the first iterable.

    • secondKeyFn: MapFn<TSecond, TKey>

      The function that extracts the key from an element of the second iterable.

    • joinFn: CombineFn<TElement, Iterable<TSecond>, TResult>

      The function that takes an element from the first and an iterable of elements from the second, and outputs the resulting element.

    Returns Lazy<TResult>

intersect

  • intersect<TKey>(second: Iterable<TElement>, compareOn?: MapFn<TElement, TKey>): Lazy<TElement>
  • Returns the set intersection between 2 iterables. This like doing an AND over the 2 iterables.

    remarks

    This will iterate the second iterable completely once it has started iteration (not before). It will not cause additional unexpected iteration on the underlying iterable.

    Type parameters

    • TKey = TElement

    Parameters

    • second: Iterable<TElement>

      The iterable to get the intersection of.

    • Optional compareOn: MapFn<TElement, TKey>

      A mapping function to get the key to compare with. The value will be effectively compared using a strict equals (===) against the others. If not given, then each element will used directly.

    Returns Lazy<TElement>

iterableEquals

  • iterableEquals(second: Iterable<TElement>, comparer?: ComparerFn<TElement>): boolean
  • Determines whether 2 iterables are equal.

    remarks

    This will check for both order and value, and will iterate both iterables completely.

    Parameters

    • second: Iterable<TElement>

      The iterable to compare against.

    • Optional comparer: ComparerFn<TElement>

      The function to perform the comparision of each pair of elements with. If not given, defaults to strict equals (===).

    Returns boolean

    Whether the 2 iterables were both equal.

join

  • join<TSecond, TKey, TResult>(second: Iterable<TSecond>, firstKeyFn: MapFn<TElement, TKey>, secondKeyFn: MapFn<TSecond, TKey>, joinFn: CombineFn<TElement, TSecond, TResult>): Lazy<TResult>
  • Joins 2 iterables on the given matching keys. This is similar to a JOIN in SQL.

    remarks

    This will iterate the second iterable completely once it has started iteration (not before). It will not cause additional unexpected iteration on the underlying iterable.

    Type parameters

    • TSecond

    • TKey

    • TResult

    Parameters

    • second: Iterable<TSecond>

      The iterable to join.

    • firstKeyFn: MapFn<TElement, TKey>

      The function that extracts the key from the first iterable.

    • secondKeyFn: MapFn<TSecond, TKey>

      The function that extracts the key from the second iterable.

    • joinFn: CombineFn<TElement, TSecond, TResult>

      The function that takes in a single element from the from each of the first and second iterables, and outputs the resulting element.

    Returns Lazy<TResult>

last

  • last(): TElement
  • last(predicate: BoolPredicate<TElement>): TElement
  • Returns the last element in the iterable.

    throws

    {Error} If the iterable was empty.

    remarks

    This will cause a complete iteration of the iterable object.

    Returns TElement

    The last element in the iterable.

  • Returns the last element in the iterable that satisfies the given condition.

    throws

    {Error} If no elements satisfied the condition.

    remarks

    This will cause a complete iteration of the iterable object.

    Parameters

    • predicate: BoolPredicate<TElement>

      The predicate to test each element with.

    Returns TElement

    The last element in the iterable that satisfied the condition.

lastOrDefault

  • lastOrDefault(defaultValue: TElement): TElement
  • lastOrDefault<TDefault>(defaultValue: TDefault): TElement | TDefault
  • lastOrDefault(defaultValue: TElement, predicate: BoolPredicate<TElement>): TElement
  • lastOrDefault<TDefault>(defaultValue: TDefault, predicate: BoolPredicate<TElement>): TElement | TDefault
  • Returns the last element in the iterable, or the given default value if the iterable was empty.

    remarks

    This will cause a complete iteration of the iterable object.

    Parameters

    • defaultValue: TElement

      The value to use of the iterable was empty.

    Returns TElement

    The last element in the iterable, or the default value if empty.

  • Returns the last element in the iterable, or the given default value if the iterable was empty.

    remarks

    This will cause a complete iteration of the iterable object.

    Type parameters

    • TDefault

    Parameters

    • defaultValue: TDefault

      The value to use of the iterable was empty.

    Returns TElement | TDefault

    The last element in the iterable, or the default value if empty.

  • Returns the last element in the iterable that satisfies the given condition, or the given default value.

    remarks

    This will cause a complete iteration of the iterable object.

    Parameters

    • defaultValue: TElement

      The value to use of the iterable was empty.

    • predicate: BoolPredicate<TElement>

      The predicate to test each element with.

    Returns TElement

    The last element in the iterable, or the default value if no element satisfied the condition.

  • Returns the last element in the iterable that satisfies the given condition, or the given default value.

    remarks

    This will cause a complete iteration of the iterable object.

    Type parameters

    • TDefault

    Parameters

    • defaultValue: TDefault

      The value to use of the iterable was empty.

    • predicate: BoolPredicate<TElement>

      The predicate to test each element with.

    Returns TElement | TDefault

    The last element in the iterable, or the default value if no element satisfied the condition.

max

  • max(): TElement extends number ? number : never
  • max(selector: MapFn<TElement, number>): number
  • Returns the maximum value in the iterable.

    throws

    {TypeError} If any element in the iterable was a non-number.

    throws

    {Error} If the iterable was empty.

    remarks

    This will cause a complete iteration of the iterable object.

    Returns TElement extends number ? number : never

    The maximum element.

  • Returns the maximum value of result of the selector function over the iterable.

    throws

    {Error} If the iterable was empty.

    remarks

    This will cause a complete iteration of the iterable object.

    Parameters

    • selector: MapFn<TElement, number>

      The transformation function to use for each element.

    Returns number

    The maximum result of the selector function.

min

  • min(): TElement extends number ? number : never
  • min(selector: MapFn<TElement, number>): number
  • Returns the minimum value in the iterable.

    throws

    {TypeError} If any element in the iterable was a non-number.

    throws

    {Error} If the iterable was empty.

    remarks

    This will cause a complete iteration of the iterable object.

    Returns TElement extends number ? number : never

    The minimum element.

  • Returns the minimum value of result of the selector function over the iterable.

    throws

    {Error} If the iterable was empty.

    remarks

    This will cause a complete iteration of the iterable object.

    Parameters

    • selector: MapFn<TElement, number>

      The transformation function to use for each element.

    Returns number

    The minimum result of the selector function.

orderBy

  • orderBy<TKey>(keyFn: MapFn<TElement, TKey>, compareFn?: SortFn<TKey>): Lazy<TElement>
  • Sorts the iterable in ascending order.

    remarks

    When this is iterated (not before), the underlying iterator is walked through completely in order to allow sorting.

    Type parameters

    • TKey

    Parameters

    • keyFn: MapFn<TElement, TKey>

      The function used to get the key from a given element.

    • Optional compareFn: SortFn<TKey>

      The function that is passed to Array.prototype.sort to compare values and return the comparison number. If not given, a default sorting function will be used. This sorting function will match the behaviour of the standard sort comparison function.

    Returns Lazy<TElement>

orderByDecending

  • orderByDecending<TKey>(keyFn: MapFn<TElement, TKey>, compareFn?: SortFn<TKey>): Lazy<TElement>
  • Sorts the iterable in descending order.

    remarks

    When this is iterated (not before), the underlying iterator is walked through completely in order to allow sorting.

    Type parameters

    • TKey

    Parameters

    • keyFn: MapFn<TElement, TKey>

      The function used to get the key from a given element.

    • Optional compareFn: SortFn<TKey>

      The function that is passed to Array.prototype.sort to compare values and return the comparison number. If not given, a default sorting function will be used. This sorting function will match the behaviour of the standard sort comparison function.

    Returns Lazy<TElement>

orderNumericallyBy

  • orderNumericallyBy(keyFn: MapFn<TElement, number>): Lazy<TElement>
  • Sorts the iterable in acending order according to the numeric result of the key function.

    remarks

    When this is iterated (not before), the underlying iterator is walked through completely in order to allow sorting. This function is equivalent to orderBy with a numeric comparison compare function.

    Parameters

    • keyFn: MapFn<TElement, number>

      The function user to get the key from the given element.

    Returns Lazy<TElement>

orderNumericallyByDecending

  • orderNumericallyByDecending(keyFn: MapFn<TElement, number>): Lazy<TElement>
  • Sorts the iterable in descending order according to the numeric result of the key function.

    remarks

    When this is iterated (not before), the underlying iterator is walked through completely in order to allow sorting. This function is equivalent to orderByDecending with a numeric comparison compare function.

    Parameters

    • keyFn: MapFn<TElement, number>

      The function used to get the key from a given element.

    Returns Lazy<TElement>

prepend

  • prepend(element: TElement): Lazy<TElement>
  • Prepends the element to the beginning of the iterable.

    remarks

    Does not cause additional unexpected iteration.

    Parameters

    • element: TElement

      The element to prepend.

    Returns Lazy<TElement>

resolveAll

  • resolveAll(): Promise<TElement extends PromiseLike<TResult> ? Lazy<TResult> : Lazy<TElement>>
  • Resolves all of the promises in the iterable, and returns a new Lazy iterable from the result.

    remarks

    This will cause a complete iteration of the iterable object. This will behave similar to cache, in that the original lazy iterable will be resolved completely and then discarded, in favour of the resolved iterable.

    Returns Promise<TElement extends PromiseLike<TResult> ? Lazy<TResult> : Lazy<TElement>>

    A promise that will resolve to a lazy iterable object.

reverse

  • reverse(): Lazy<TElement>
  • Reverses the order of the iterable.

    remarks

    When this is iterated (not before), the underlying iterator is walked through completely in order to allow starting from the end.

    Returns Lazy<TElement>

select

  • select<TResult>(selector: IndexMapFn<TElement, TResult>): Lazy<TResult>
  • Projects the elements of the iterable into a new form.

    remarks

    Does not cause additional unexpected iteration.

    Type parameters

    • TResult

    Parameters

    • selector: IndexMapFn<TElement, TResult>

      The transformation function to use for each element.

    Returns Lazy<TResult>

selectMany

  • selectMany<TResult>(selector: IndexMapFn<TElement, Iterable<TResult>>): Lazy<TResult>
  • Projects the elements of the iterable into a new form, and flattens the iterable of iterables into a single iterable.

    remarks

    Does not cause additional unexpected iteration.

    Type parameters

    • TResult

    Parameters

    • selector: IndexMapFn<TElement, Iterable<TResult>>

      The transformation function to use for each element. The index parameter is the index that the element was at in the source iterable, not the resulting one.

    Returns Lazy<TResult>

single

  • single(predicate: BoolPredicate<TElement>): TElement
  • Returns a single element from the iterable that matches the given condition.

    throws

    {Error} If no element could be found that matched the condition.

    remarks

    This will iterate until the condition is met or until the iterable ends.

    Parameters

    • predicate: BoolPredicate<TElement>

      The predicate function to test each element with.

    Returns TElement

    The element that satisfies the condition.

singleOrDefault

  • singleOrDefault(predicate: BoolPredicate<TElement>, defaultValue: TElement): TElement
  • singleOrDefault<TDefault>(predicate: BoolPredicate<TElement>, defaultValue: TDefault): TElement | TDefault
  • Returns a single element from the iterable that matches the given condition, or a default value if no element was found.

    remarks

    This will iterate until the condition is met or until the iterable ends.

    Parameters

    • predicate: BoolPredicate<TElement>

      The predicate function to test each element with.

    • defaultValue: TElement

      The default value to use if no element could be found.

    Returns TElement

    The element that satisfies the condition, or the default value if no element was found.

  • Returns a single element from the iterable that matches the given condition, or a default value if no element was found.

    remarks

    This will iterate until the condition is met or until the iterable ends.

    Type parameters

    • TDefault

    Parameters

    • predicate: BoolPredicate<TElement>

      The predicate function to test each element with.

    • defaultValue: TDefault

      The default value to use if no element could be found.

    Returns TElement | TDefault

    The element that satisfies the condition, or the default value if no element was found.

skip

  • skip(count: number): Lazy<TElement>
  • Skips the given number of elements from the start of the iterable and returns the rest.

    remarks

    Does not cause additional unexpected iteration.

    Parameters

    • count: number

      The number of elements to skip.

    Returns Lazy<TElement>

skipLast

  • skipLast(count: number): Lazy<TElement>
  • Skips the given number of elements from the end of the iterable, returning the rest.

    remarks

    This iterator requires the iterable to be finite in length. It will iterate slightly ahead of the resulting iterable.

    Parameters

    • count: number

      The number of elements to skip from the end.

    Returns Lazy<TElement>

skipWhile

  • skipWhile(predicate: IndexPredicate<TElement>): Lazy<TElement>
  • Skips all elements in the iterable until the condition returns true, after which all elements are returned regardless.

    remarks

    Does not cause additional unexpected iteration.

    Parameters

    • predicate: IndexPredicate<TElement>

      The predicate function to check the condition with.

    Returns Lazy<TElement>

stringJoin

  • stringJoin(separator?: string, strFn?: StrFn<TElement>): string
  • Joins all the elements in the iterable together into a single string, split by the given separator.

    remarks

    This will cause a complete iteration of the iterable object.

    Parameters

    • Optional separator: string

      The separator to split each element with in the string. Defaults to ''.

    • Optional strFn: StrFn<TElement>

      The function to convert each element into a string.

    Returns string

sum

  • sum(): TElement extends number ? number : never
  • sum(selector: MapFn<TElement, number>): number
  • Computes the sum of all the elements in the iterable.

    throws

    {TypeError} If any element in the iterable was a non-number.

    remarks

    This will cause a complete iteration of the iterable object.

    Returns TElement extends number ? number : never

    The sum of all the elements.

  • Returns the sum of all the results of the selector function over the iterable.

    throws

    {Error} If the iterable was empty.

    remarks

    This will cause a complete iteration of the iterable object.

    Parameters

    • selector: MapFn<TElement, number>

      The transformation function to use for each element.

    Returns number

    The sum of the results of the selector function.

take

  • take(count: number): Lazy<TElement>
  • Returns the given number of elements from the start of the iterable, ignoring the rest.

    remarks

    Does not cause additional unexpected iteration.

    Parameters

    • count: number

      The number of elements to take from the start.

    Returns Lazy<TElement>

takeLast

  • takeLast(count: number): Lazy<TElement>
  • Returns the given number of elements from the end of the iterable, ignore the elements before.

    remarks

    This iterator requires the iterable to be finite in length. It will iterate until the end.

    Parameters

    • count: number

    Returns Lazy<TElement>

takeWhile

  • takeWhile(predicate: IndexPredicate<TElement>): Lazy<TElement>
  • Takes all elements in the iterable until the condition returns true, after which the iterable is considered to have ended.

    remarks

    Does not cause additional unexpected iteration.

    Parameters

    • predicate: IndexPredicate<TElement>

      The predicate function to check the condition with.

    Returns Lazy<TElement>

toArray

  • toArray(): TElement[]
  • Converts the iterable into a standard JavaScript Array.

    remarks

    This will cause a complete iteration of the iterable object.

    Returns TElement[]

toJSON

  • toJSON(): TElement[]
  • Converts the iterable to a JSON-serialisable array.

    remarks

    This will cause a complete iteration of the iterable object. This will not do anything to the elements, meaning that you are responsible for ensuring that they are all JSON-serialisable.

    Returns TElement[]

toMap

  • toMap<TKey, TResult>(keyFn: MapFn<TElement, TKey>, valueFn?: MapFn<TElement, TResult>): Map<TKey, TResult>
  • Converts the iterable into a map using the key and value function.

    remarks

    This will cause a complete iteration of the iterable object.

    Type parameters

    • TKey

    • TResult = TElement

    Parameters

    • keyFn: MapFn<TElement, TKey>

      The function to use to derive the key of each map element.

    • Optional valueFn: MapFn<TElement, TResult>

      The function to use to derive the value of map value. If not given, then the value itself is used.

    Returns Map<TKey, TResult>

    A Map<TKey, TResult> derived from the iterable.

union

  • union<TKey>(second: Iterable<TElement>, compareOn?: MapFn<TElement, TKey>): Lazy<TElement>
  • Returns the set union between 2 iterables. This like doing an OR over the 2 iterables.

    remarks

    This will iterate the second iterable completely once it has started iteration (not before). It will not cause additional unexpected iteration on the underlying iterable.

    Type parameters

    • TKey = TElement

    Parameters

    • second: Iterable<TElement>

      The iterable to get the union of.

    • Optional compareOn: MapFn<TElement, TKey>

      A mapping function to get the key to compare with. The value will be effectively compared using a strict equals (===) against the others. If not given, then the element will used directly.

    Returns Lazy<TElement>

where

  • where<TResult>(predicate: IndexIsPredicate<TElement, TResult>): Lazy<TResult>
  • where(predicate: IndexPredicate<TElement>): Lazy<TElement>
  • Filters elements based on the given predicate.

    remarks

    Does not cause additional unexpected iteration.

    Type parameters

    • TResult

    Parameters

    • predicate: IndexIsPredicate<TElement, TResult>

      The predicate function to filter elements with.

    Returns Lazy<TResult>

  • Filters elements based on the given predicate.

    remarks

    Does not cause additional unexpected iteration.

    Parameters

    • predicate: IndexPredicate<TElement>

      The predicate function to filter elements with.

    Returns Lazy<TElement>

zip

  • zip<TSecond>(second: Iterable<TSecond>): Lazy<[TElement, TSecond]>
  • zip<TSecond, TResult>(second: Iterable<TSecond>, selector: CombineFn<TElement, TSecond, TResult>): Lazy<TResult>
  • Combines 2 iterables into an iterable of tuples. This will merge elements on the same index, finishing on the iterable that finishes first. If the first iterable has 3 elements, and the second 4, then the result will have 3 elements.

    remarks

    Does not cause additional unexpected iteration.

    Type parameters

    • TSecond

    Parameters

    • second: Iterable<TSecond>

      The iterable to zip with.

    Returns Lazy<[TElement, TSecond]>

  • Combines 2 iterables using the given selector. This will merge elements on the same index, finishing on the iterable that finishes first. If the first iterable has 3 elements, and the second 4, then the result will have 3 elements.

    remarks

    Does not cause additional unexpected iteration.

    Type parameters

    • TSecond

    • TResult

    Parameters

    • second: Iterable<TSecond>

      The iterable to zip with.

    • selector: CombineFn<TElement, TSecond, TResult>

      The function to combine the iterables with.

    Returns Lazy<TResult>

Static empty

  • empty<TElement>(): Lazy<TElement>
  • Creates an empty lazy iterable.

    Type parameters

    • TElement

    Returns Lazy<TElement>

    The empty lazy iterable object.

Static from

  • from<TElement>(iterable: Iterable<TElement>): Lazy<TElement>
  • Creates a lazy iterable object from the given iterable object.

    remarks

    If you pass in a lazy iterator, then it is returned without changes.

    Type parameters

    • TElement

    Parameters

    • iterable: Iterable<TElement>

      The object to source for lazy iteration.

    Returns Lazy<TElement>

    The lazy iterable object with the given iterable as the source.

Static range

  • range(start: number, end?: number): Lazy<number>
  • Creates a lazy iterable object that will produce a range of integers.

    remarks

    When creating an infinite interable, be very careful. If you do not include your own stop condition (e.g. with .take(n)), then it will lock up the thread until the process is aborted. You will also have to take into account that some lazy iterators require the interable to be finite to work. Check the remarks on the function you want to use to see which ones will work.

    Parameters

    • start: number

      The starting number of the range (inclusive).

    • Optional end: number

      The ending number of the range (exclusive). If not given, then the value is assumed to be +Infinity.

    Returns Lazy<number>

    The lazy iterable object with the range as the source.

Static repeat

  • repeat<TElement>(element: TElement, count?: number): Lazy<TElement>
  • Creates a lazy iterable object that will repeate the element a given number of times.

    throws

    {Error} If count < 0.

    remarks

    When creating an infinite interable, be very careful. If you do not include your own stop condition (e.g. with .take(n)), then it will lock up the thread until the process is aborted. You will also have to take into account that some lazy iterators require the interable to be finite to work. Check the remarks on the function you want to use to see which ones will work.

    Type parameters

    • TElement

    Parameters

    • element: TElement

      The value to repeat.

    • Optional count: number

      The number of times to repeat it. If not given, then the value is assumed to be +Infinity.

    Returns Lazy<TElement>

    The lazy iterable object with the repeated value as the source.

Generated using TypeDoc