Options
All
  • Public
  • Public/Protected
  • All
Menu

Class AsyncIter<Elem>

Enrichment class allowing for manipulation of asynchronous iterables.

typeparam

the element type.

Type parameters

  • Elem

Hierarchy

  • AsyncIter

Implements

  • AsyncIterable<Elem>

Index

Methods

__@asyncIterator

  • __@asyncIterator(): AsyncIterator<Elem>
  • AsyncIterable interface: When called, returns an AsyncIterator of type T

    Returns AsyncIterator<Elem>

append

  • Returns an AsyncIter yielding the given elements after the elements of this Iter have been yielded

    example
    asyncIter([1, 3, 5]).append(6, 7)
    result: (1, 3, 5, 6, 7)

    Parameters

    • Rest ...elems: NonEmpty<Elem>

      a non-empty list of elements that should be appended

    Returns AsyncIter<Elem>

applyCustomOperation

  • applyCustomOperation<R>(createIterator: function): AsyncIter<R>
  • Returns an AsyncIter instance yielding the values resulting from the iterator output of the createIterator function receiving this iterable as an argument.

    example
    asyncIter([1, 2, 3, 4])
      .applyCustomOperation(async function*(iterable) {
        for await { const elem of iterable } {
          if (Math.random() > 0.5) yield `foo${elem}`
        }
      })
      .join(',')
    result (example): 'foo2,foo3'

    Type parameters

    • R

      the result iterator element type

    Parameters

    • createIterator: function

      a function receiving the current iterable and returning an iterator of new elements

        • (iterable: AsyncIterable<Elem>): AsyncIterator<R>
        • Parameters

          • iterable: AsyncIterable<Elem>

          Returns AsyncIterator<R>

    Returns AsyncIter<R>

collect

  • collect<Res>(collector: Op<Elem, Res>): Promise<Res>
  • Applies the values of the current AsyncIter to the given collector and returns the result.

    typeparam

    the result type of the collector

    example
    asyncIter([1, 3, 5]).collect(Collectors.sum)
    result: 9

    Type parameters

    • Res

    Parameters

    • collector: Op<Elem, Res>

      the collector to apply

    Returns Promise<Res>

collectIter

  • collectIter<Res>(collector: Op<Elem, Res>): AsyncIter<Res>
  • Returns an AsyncIter yielding each result of applying the collector to the next element in this AsyncIter.

    typeparam

    the result type of the collector

    example
    asyncIter([1, 3, 4]).collectIter(Collectors.sum)
    result: (1, 4, 8)

    Type parameters

    • Res

    Parameters

    • collector: Op<Elem, Res>

      the collector to apply

    Returns AsyncIter<Res>

concat

  • Returns an AsyncIter yielding the values of this AsyncIter, followed by the values in each of the iterables supplied as arguments.

    example
    asyncIter([2, 4]).concat([5, 3], Iter.nats)
    result: (2, 4, 5, 3, 1, 2, 3, ...)

    Parameters

    • Rest ...otherIterables: NonEmpty<AnyIterable<Elem>>

      a non-empty list of iterables of the same type

    Returns AsyncIter<Elem>

delay

  • Returns an AsyncIter that emits the same elements as this AsyncIter, however only after waiting the given ms milliseconds before yielding each element.

    Parameters

    • ms: number

      the amount of milliseconds to delay yielding an element

    Returns AsyncIter<Elem>

distinct

  • Returns an AsyncIter that yields each distinct value of this Iter at most once.

    example
    asyncIter([1, 5, 1, 3, 2, 5, 1]).distinct()
    result: (1, 5, 3, 2)

    Returns AsyncIter<Elem>

distinctBy

  • distinctBy<K>(keyFun: function): AsyncIter<Elem>
  • Returns an AsyncIter that applies the given keyFun to each element, and will yield only elements for which keyFun returns a key that was not calculated before.

    example
    asyncIter(['bar', 'foo', 'test', 'same']).distinctBy(v => v.length)
    result: ('bar', 'test')

    Type parameters

    • K

      the type of key that the key function returns

    Parameters

    • keyFun: function

      a function that, given an element and its index, returns a calculated key

        • (value: Elem, index: number): K
        • Parameters

          • value: Elem
          • index: number

          Returns K

    Returns AsyncIter<Elem>

drop

  • Returns an AsyncIter that skips the first given amount of elements of this AsyncIter, then yields all following elements, if present.

    example
    asyncIter([1, 2, 3]).drop(2)
    result: (3)

    Parameters

    • amount: number

      the amount of elements to skip

    Returns AsyncIter<Elem>

dropLast

  • Returs an AsyncIter that skips the last amount of elements of this AsyncIter.

    Parameters

    • amount: number

      the amount of last elements to skip

      iter.range(0, 10).toAsync().dropLast(5)
      result: (0, 1, 2, 3, 4)

    Returns AsyncIter<Elem>

dropWhile

  • Returns an AsyncIter that skips items of this AsyncIter as long as the given pred predicate holds, then yields all following elements, if present.

    example
    asyncIter([1, 2, 3]).dropWhile(v => v < 2)
    result: (2, 3)

    Parameters

    • pred: Pred<Elem>

      a predicate taking an element and its index

    Returns AsyncIter<Elem>

filter

  • Returns an AsyncIter of only those elements for which the given pred predicate returns true

    example
    asyncIter([1, 3, 5, 7]).filter((v, i) => isEven(v + i))
    result: (3, 7)

    Parameters

    • pred: Pred<Elem>

      a predicate for an element, optionally with its index

    Returns AsyncIter<Elem>

filterChanged

  • Returns an AsyncIter that yields elements from this Iter, only when the element is different from the previous value.

    example
    asyncIter([1, 3, 3, 2, 5, 5, 2, 3]).filterChanged()
    result: (1, 3, 2, 5, 2, 3)

    Returns AsyncIter<Elem>

filterNot

  • Returns an AsyncIter of only those elements for which the given pred predicate returns false

    example
    iter.nats.toAsync().filterNot(isEven)
    result: (1, 3, 5, 7, ...)

    Parameters

    • pred: Pred<Elem>

      a predicate for an element, optionally with its index

    Returns AsyncIter<Elem>

filterWithPrevious

  • filterWithPrevious(pred: function): AsyncIter<Elem>
  • Returns an AsyncIter that yields those elements that satify the given pred predicate.

    Parameters

    • pred: function

      a function that take the current element, and the optional previous element and returns a boolean indicating its filter status

        • (current: Elem, previous?: Elem): boolean
        • Parameters

          • current: Elem
          • Optional previous: Elem

          Returns boolean

    Returns AsyncIter<Elem>

flatMap

  • flatMap<R>(flatMapFun: function): AsyncIter<R>
  • Returns an AsyncIter that yields all values from the iterables resulting from applying the given flatMapFun to each element in this AsyncIter.

    example
    asyncIter([1, 2, 3]).flatMap(e => Iter.of(e).repeat(e))
    result: (1, 2, 2, 3, 3, 3)

    Type parameters

    • R

      the elements of the Iterables resulting from flatMapFun

    Parameters

    • flatMapFun: function

      a function taking an element from this Iter and returning an iterable with elements of type R

    Returns AsyncIter<R>

forEach

  • forEach(effect?: Effect<Elem>): Promise<void>
  • Applies the given effect function to each element of the AsyncIter, with its index. Note: eagerly gets all values from the iterable.

    Parameters

    • Optional effect: Effect<Elem>

      a function taking an element of the Iter and optionally its index and performs some side-effect.

    Returns Promise<void>

indicesOf

  • Returns an AsyncIter with the indices of the this AsyncIter where the element equals the given elem value.

    example
    asyncIter('ababba').indicesOf('a')
    result: (0, 2, 5)

    Parameters

    • elem: Elem

      the element to compare to

    Returns AsyncIter<number>

indicesWhere

  • Returns an AsyncIter with the indices of the this AsyncIter at which the element satisfies the given pred predicate.

    example
    asyncIter([1, 2, 5, 6, 4, 3]).indicesWhere(isEven)
    result: (1, 3, 4)

    Parameters

    • pred: Pred<Elem>

      a predicate for an element of this Iter and its index

    Returns AsyncIter<number>

interleave

  • Returns an AsyncIter that repeatedly yields one value of this Iter and then one value from each given iterable, as long as none of the iterables is done.

    example
    asyncIter('abcba').interleave('QWQ').join()
    result: 'aQbWcQ'

    Parameters

    • Rest ...otherIterables: [AnyIterable<Elem>, Array]

      a non-empty list of iterables to interleave with.

    Returns AsyncIter<Elem>

interleaveAll

  • Returns an AsyncIter that repeatedly yields one value of this Iter and then one value from each given iterable, for each iterable as long as it still yields values.

    example
    asyncIter('abcba').interleave('QWQ').join()
    result: 'aQbWcQba'

    Parameters

    • Rest ...otherIterables: NonEmpty<AnyIterable<Elem>>

      a non-empty list of iterables to interleave with.

    Returns AsyncIter<Elem>

interleaveRound

  • Returns an AsyncIter that indefinitely yields one value of this Iter and then one value from each given iterable, starting back at the start of each iterable if it is exhausted.

    example
    asyncIter('abc').interleaveRound('QW').take(10).join()
    result: 'aQbWcQaWbQ'

    Parameters

    • Rest ...otherIterables: NonEmpty<AnyIterable<Elem>>

      a non-empty list of iterables to interleave with.

    Returns AsyncIter<Elem>

intersperse

  • Returns an AsyncIter that yields the elements of this AsyncIter with the given interIter elements as a sepatator

    example
    asyncIter('abc').intersperse('|').join()
    result: 'a|b|c'

    Parameters

    • interIter: AnyIterable<Elem>

      an iterator of elements that is used as a separator

    Returns AsyncIter<Elem>

join

  • join(sep?: string, start?: string, end?: string): Promise<string>
  • Returns a promise resolving to a string starting with the given start element, then each element of this Iter separated by the given sep element, and ending with the end element.

    example
    asyncIter.of([1, 5, 6]).join('<', '|, '>')
    result: '<1|5|6>'

    Parameters

    • Default value sep: string = ""

      the seperator string

    • Default value start: string = ""

      the start string

    • Default value end: string = ""

      the end string

    Returns Promise<string>

map

  • Returns an AsyncIter where the given mapFun is applied to each yielded element, with its index.

    example
    asyncIter([1, 3, 5]).map((value, index) => value + index)
    result: (1, 4, 7)

    Type parameters

    • R

      the result type of the given mapFun

    Parameters

    • mapFun: MapFun<Elem, R>

      a function taking an element of this Iter, and optionally its index, and returning a new element of type R

    Returns AsyncIter<R>

mkGroup

  • Returns an AsyncIter that starts with the startIter elements, then yields all elements of this AsyncIter with the sepIter elements as separators, and ends with the endIter elements.

    example
    asyncIter([1, 3, 4]).mkGroup([10], [100], [90, 80])
    result: (10, 1, 100, 3, 100, 4, 90, 80)

    Parameters

    • Default value startIter: AnyIterable<Elem> = asyncIter.empty()

      the start elements

    • Default value sepIter: AnyIterable<Elem> = asyncIter.empty()

      the separator elements

    • Default value endIter: AnyIterable<Elem> = asyncIter.empty()

      the end elements

    Returns AsyncIter<Elem>

monitor

  • Allows side-effects at any point in the chain, but does not modify the Iter.

    Parameters

    • Default value tag: string = ""

      a tag that can be used when performing the side-effect

    • Default value effect: MonitorEffect<Elem> = defaultMonitorEffect

      the side-effect to perform for each yielded element

      iter.nats.toAsync().monitor("nats").take(3)
      result:
      > nats[0]: 0
      > nats[1]: 1
      > nats[2]: 2

    Returns AsyncIter<Elem>

patchAt

  • patchAt(index: number, remove: number, insert?: undefined | function): AsyncIter<Elem>
  • Returns an AsyncIter where at the given index, the given amount of elements is skipped, and the optional insert function is called to generate an iterable based on the matching element and its index, after which that iterable is yielded.

    example
    asyncIter('abc').patchAt(1, 1, () => 'QW').join()
    result: 'aQWc'

    Parameters

    • index: number

      the index at which to patch

    • remove: number

      the number of elements to skip when a matching element is found

    • Optional insert: undefined | function

      an optional function taking the matching element and its index, and returning an iterable to yield

    Returns AsyncIter<Elem>

patchElem

  • patchElem(elem: Elem, remove: number, insert?: AnyIterable<Elem>, amount?: undefined | number): AsyncIter<Elem>
  • Returns an AsyncIter where for each element of this Iter that equals the given elem, the given amount of elements is skipped, and the optional insert iterable is yielded.

    example
    asyncIter('abcba').patchElem('b', 1, '--').join()
    result: ('a--c--a')

    Parameters

    • elem: Elem

      the element to patch

    • remove: number

      the number of elements to skip when a matching element is found

    • Optional insert: AnyIterable<Elem>

      an optional function taking the matching element and its index, and returning an iterable to yield

    • Optional amount: undefined | number

      an optional amount of elements to patch

    Returns AsyncIter<Elem>

patchWhere

  • patchWhere(pred: Pred<Elem>, remove: number, insert?: undefined | function, amount?: undefined | number): AsyncIter<Elem>
  • Returns an AsyncIter where for each element of this Iter that satisfies pred, the given amount of elements is skipped, and the optional insert function is called to generate an iterable based on the matching element and its index, after which that iterable is yielded.

    example
    asyncIter([0, 1, 5, 2]).patchWhere(isEven, 1, () => [10, 11])
    result: (10, 11, 1, 5, 10, 11)
    example
    asyncIter([0, 1, 5, 2]).patchWhere(isEven, 0, () => [10, 11])
    result: (10, 11, 0, 1, 5, 10, 11, 2)
    example
    asyncIter([0, 1, 5, 2]).patchWhere(isEven, 2)
    result: (5)
    example
    asyncIter([0, 1, 5, 2]).patchWhere(isEven, 1, undefined, 1)
    result: (1, 5, 2)

    Parameters

    • pred: Pred<Elem>

      a predicate for an element of this Iter and its index

    • remove: number

      the number of elements to skip when a matching element is found

    • Optional insert: undefined | function

      an optional function taking the matching element and its index, and returning an iterable to yield

    • Optional amount: undefined | number

      the optional amount of times to perform the patch

    Returns AsyncIter<Elem>

prepend

  • Returns an AsyncIter yielding the given elements before the elements of this Iter are yielded

    example
    asyncIter([1, 3, 5]).prepend(6, 7)
    result: (6, 7, 1, 3, 5)

    Parameters

    • Rest ...elems: NonEmpty<Elem>

      a non-empty list of elements that should be prepended

    Returns AsyncIter<Elem>

reduce

  • Returns a promise resolving to the result of applying the reducer function to each element of this AsyncIter, or returns the otherwise value if this Iter is empty.

    example
    asyncIter([1, 2, 3]).reduce((res, value) => res + value)
    result: 6

    Parameters

    • op: ReduceFun<Elem>

      the reducer function taking the current reducer value and the next element of this AsyncIter, and returning a new value

    • Optional otherwise: OptLazy<Elem>

      specifies how to deal with the potential case that this Iter is empty. There are three cases:

      • not specified / undefined: If this Iter is empty, this function will throw an error
      • (value: T): If this Iter is empty, it will return the given value instead
      • (f: () => T): If this Iter is empty, it will return the value resulting from executing f()

    Returns Promise<Elem>

repeat

  • repeat(times?: undefined | number): AsyncIter<Elem>
  • Repeats the current AsyncIter times amount of times, or indefinately if times is undefined.

    example
    asyncIter([1, 3]).repeat(3)
    result: (1, 3, 1, 3, 1, 3)
    example
    asyncIter([1, 3]).repeat()
    result: (1, 3, 1, 3, 1, ...)

    Parameters

    • Optional times: undefined | number

      the amount of times to repeat this Iter

    Returns AsyncIter<Elem>

sample

  • Returns an AsyncIter that yields only each given nth value of this AsyncIter.

    example
    iter.nats.toAsync().sample(10)
    result: (0, 10, 20, 30, ...)

    Parameters

    • nth: number

      the amount of elements to skip every in between emissions.

    Returns AsyncIter<Elem>

slice

  • slice(from: number, amount: number): AsyncIter<Elem>
  • Returns an AsyncIter that yields the items of this AsyncIter starting at the from index, and then yields the specified amount of items, if present.

    example
    asyncIter([1, 2, 3, 4]).slice(1, 2)
    result: (2, 3)

    Parameters

    • from: number

      the index at which to start yielding values

    • amount: number

      the amount of items to yield

    Returns AsyncIter<Elem>

sliding

  • sliding(size: number, step?: number): AsyncIter<Elem[]>
  • Returns an AsyncIter that yields arrays of the given size of values from this AsyncIter, each shifted that the given step Note: the last window may be smaller than the given size.

    example
    iter.nats.toAsync().sliding(3)
    result: ([0, 1, 2], [3, 4, 5], [6, 7, 8], ...)
    example
    iter.nats.toAsync().sliding(3, 1)
    result: ([0, 1, 2], [1, 2, 3], [2, 3, 4], ...)

    Parameters

    • size: number

      the window size

    • Default value step: number = size

      the amount of elements to shift the next window

    Returns AsyncIter<Elem[]>

splitOnElem

  • Returns an AsyncIter that yields arrays of values of this AsyncIter each time a value that equals given elem is encountered

    example
    asyncIter('a test  foo').splitOnElem(' ')
    result: (['a'], ['t', 'e', 's', 't'], [], ['f', 'o', 'o'])

    Parameters

    • elem: Elem

      the element on which a new split should be made

    Returns AsyncIter<Elem[]>

splitWhere

  • Returns an AsyncIter that yields arrays of values of this AsyncIter each time a value is encountered that satisfied the given pred predicate

    example
    asyncIter([4, 5, 6, 7, 8, 9, 10]).splitWhere(isPrime)
    result: ([4], [6], [8, 9, 10])

    Parameters

    • pred: Pred<Elem>

      a predicate for an element of this AsyncIter and its index

    Returns AsyncIter<Elem[]>

take

  • Returns an AsyncIter that yields the first given amount of elements of this AsyncIter if present, then ends.

    example
    asyncIter([1, 2, 3]).take(2)
    result: (1, 2)

    Parameters

    • amount: number

      the amount of elements to yield

    Returns AsyncIter<Elem>

takeLast

  • Returs an AsyncIter that yields the last amount of elements of this AsyncIter.

    Parameters

    • amount: number

      the amount of elements to yield

      iter.range(0, 10).toAsync().takeLast(3)
      result: (7, 8, 9)

    Returns AsyncIter<Elem>

takeWhile

  • Returns an AsyncIter that yields the items of this AsyncIter as long as the given pred predicate holds, then ends.

    example
    asyncIter([1, 2, 3]).takeWhile(v => v <= 2)
    result: (1, 2)

    Parameters

    • pred: Pred<Elem>

      a predicate taking an element and its index

    Returns AsyncIter<Elem>

toString

  • toString(): string
  • Returns a fixed tag string to void unnecessary evaluation of iterable items.

    Returns string

zip

  • zip<O>(...otherIterables: ZipAnyIters<O>): AsyncIter<Unshift<O, Elem>>
  • Returns an AsyncIter yielding tuples of each next element of this AsyncIter and the provided iterables. If any of the iterables is done, the resulting AsyncIter will end. Note that the type of the first iterable is taken into account, the other iterable elements will be case to any.

    example
    asyncIter([0, 5]).zip(Iter.nats)
    result: ([0, 0], [5, 1])

    Type parameters

    • O: NonEmpty<any>

      the type of the Iter elements of the first given iterable

    Parameters

    • Rest ...otherIterables: ZipAnyIters<O>

      the other iterables to zip

    Returns AsyncIter<Unshift<O, Elem>>

zipAll

  • zipAll<O>(...otherIterables: ZipAnyIters<O>): AsyncIter<OptList<Unshift<O, Elem>>>
  • Returns an AsyncIter containing tuples of each next element of this AsyncIter and the provided iterables. If any of the iterables is done, the resulting values will be undefined. If all iterables are done, the resulting Iter ends. Note that the type of the first iterable is taken into account, the other iterable elements will be case to any.

    example
    asyncIter([1, 5, 6]).zipAll('ac')
    result: ([1, 'a'], [5, 'c'], [6, undefined])

    Type parameters

    • O: NonEmpty<any>

      the type of the Iter elements of the first given iterable

    Parameters

    • Rest ...otherIterables: ZipAnyIters<O>

      the other iterables to zip

    Returns AsyncIter<OptList<Unshift<O, Elem>>>

zipAllWith

  • zipAllWith<O, R>(zipFun: function, ...otherIterables: ZipAnyIters<O>): AsyncIter<R>
  • Returns an AsyncIter yielding the result from applying the given zipFun to the next element of this AsyncIter and each next element of the given iterables. If any of the iterables is done, the element will be undefined. If all iterables are done, the resulting AsyncIter ends. Note that the type of the first iterable is taken into account, however the other iterable elements will be cast to any.

    example
    asyncIter([1, 2, 3]).zipAllWith((a, b) => [b, a], [5, 7])
    result: ([5, 0], [7, 2], [undefined, 3])

    Type parameters

    • O: NonEmpty<any>

      the type of the iterable elements of the first given iterable

    • R

      the result of applying the zipFun function to the next elements of all given iterables

    Parameters

    • zipFun: function

      a function taking one element from this and the given iterables, and returns a resulting element that will be yielded, accepting undefined for each non-present value

        • (t?: Elem, ...others: OptList<O>): R
        • Parameters

          • Optional t: Elem
          • Rest ...others: OptList<O>

          Returns R

    • Rest ...otherIterables: ZipAnyIters<O>

      the other iterables to zip

    Returns AsyncIter<R>

zipWith

  • zipWith<O, R>(zipFun: function, ...otherIterables: ZipAnyIters<O>): AsyncIter<R>
  • Returns an AsyncIter yielding the result from applying the given zipFun to the next element of this AsyncIter and each next element of the given iterables. If any of the iterables is done, the resulting AsyncIter als ends. Note that the type of the first iterable is taken into account, however the other iterable elements will be cast to any.

    example
    asyncIter([1, 2]).zipWith((a, b) => a + b, [5, 2, 3])
    result: (6, 4)

    Type parameters

    • O: NonEmpty<any>

      the type of the Iter elements of the first given iterable

    • R

      the result of applying the zipFun function to the next elements of all given iterables

    Parameters

    • zipFun: function

      a function taking one element from this and the given iterables, and returns a resulting element that will be yielded

        • (t: Elem, ...others: O): R
        • Parameters

          • t: Elem
          • Rest ...others: O

          Returns R

    • Rest ...otherIterables: ZipAnyIters<O>

      the other iterables to zip

    Returns AsyncIter<R>

zipWithIndex

  • Returns an AsyncIter yielding tuples of the elements of this AsyncIter as first elements, and their indices as second element.

    example
    asyncIter('a').repeat(3).zipWithIndex()
    result: (['a', 0], ['a', 1], ['a', 2])

    Returns AsyncIter<[Elem, number]>

Static fromIterable

Generated using TypeDoc