Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Iter<Elem>

Enrichment class allowing for manipulation of synchronous iterables.

typeparam

the element type.

Type parameters

  • Elem

Hierarchy

  • Iter

Implements

  • Iterable<Elem>

Index

Methods

__@iterator

  • __@iterator(): Iterator<Elem>

append

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

    example
    iter.of(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 Iter<Elem>

applyCustomOperation

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

    example
    iter.of(1, 2, 3, 4)
      .applyCustomOperation(function*(iterable) {
        for { 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: Iterable<Elem>): Iterator<R>
        • Parameters

          • iterable: Iterable<Elem>

          Returns Iterator<R>

    Returns Iter<R>

collect

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

    typeparam

    the result type of the collector

    example
    iter.of(1, 3, 5).collect(Collectors.sum)
    result: 9

    Type parameters

    • Res

    Parameters

    • op: Op<Elem, Res>

      the collector to apply

    Returns Res

collectIter

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

    example
    iter.of(1, 3, 4).collectIter(Collectors.sum)
    result: (1, 4, 8)

    Type parameters

    • R

      the result type of the collector

    Parameters

    • op: Op<Elem, R>

      the collector to apply

    Returns Iter<R>

concat

  • concat(...otherIterables: NonEmpty<Iterable<Elem>>): Iter<Elem>
  • Returns an Iter yielding the values of this Iter, followed by the values in each of the iterables supplied as arguments.

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

    Parameters

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

      a non-empty list of iterables of the same type

    Returns Iter<Elem>

distinct

  • distinct(): Iter<Elem>
  • Returns an Iter that yields each distinct value of this Iter at most once.

    example
    iter.of(1, 5, 1, 3, 2, 5, 1).distinct()
    result: (1, 5, 3, 2)

    Returns Iter<Elem>

distinctBy

  • distinctBy<K>(keyFun: function): Iter<Elem>
  • Returns an Iter 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
    iter.of('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 Iter<Elem>

drop

  • drop(amount: number): Iter<Elem>
  • Returns an Iter that skips the first given amount of elements of this Iter, then yields all following elements, if present.

    example
    iter.nats.drop(3)
    result: (3, 4, 5, 6, ...)

    Parameters

    • amount: number

      the amount of elements to skip

    Returns Iter<Elem>

dropLast

  • dropLast(amount: number): Iter<Elem>
  • Returs an Iter that skips the last amount of elements of this Iter.

    Parameters

    • amount: number

      the amount of elements to skip

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

    Returns Iter<Elem>

dropWhile

  • dropWhile(pred: Pred<Elem>): Iter<Elem>
  • Returns an Iter that skips items of this Iter as long as the given pred predicate holds, then yields all following elements, if present.

    example
    iter.nats.dropWhile(v => v < 5)
    result: (5, 6, 7, 8, ...)

    Parameters

    • pred: Pred<Elem>

      a predicate taking an element and its index

    Returns Iter<Elem>

filter

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

    example
    iter.of(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 Iter<Elem>

filterChanged

  • filterChanged(): Iter<Elem>
  • Returns an Iter that yields elements from this Iter, only when the element is different from the previous value.

    example
    iter.of(1, 3, 3, 2, 5, 5, 2, 3).filterChanged()
    result: (1, 3, 2, 5, 2, 3)

    Returns Iter<Elem>

filterNot

  • filterNot(pred: Pred<Elem>): Iter<Elem>
  • Returns an Iter of only those elements for which the given pred predicate returns false

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

    Parameters

    • pred: Pred<Elem>

      a predicate for an element, optionally with its index

    Returns Iter<Elem>

filterWithPrevious

  • filterWithPrevious(pred: function): Iter<Elem>
  • Returns an Iter 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 Iter<Elem>

flatMap

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

    example
    iter.nats.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

        • (elem: Elem, index: number): Iterable<R>
        • Parameters

          • elem: Elem
          • index: number

          Returns Iterable<R>

    Returns Iter<R>

forEach

  • forEach(effect?: Effect<Elem>): void
  • Applies the given effect function to each element of the Iter, 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 void

indicesOf

  • indicesOf(elem: Elem): Iter<number>
  • Returns an Iter with the indices of the this Iter where the element equals the given elem value.

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

    Parameters

    • elem: Elem

      the element to compare to

    Returns Iter<number>

indicesWhere

  • indicesWhere(pred: Pred<Elem>): Iter<number>
  • Returns an Iter with the indices of the this Iter at which the element satisfies the given pred predicate.

    example
    iter.of(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 Iter<number>

interleave

  • interleave(...otherIterables: NonEmpty<Iterable<Elem>>): Iter<Elem>
  • Returns an Iter 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
    iter('abcba').interleave('QWQ').join()
    result: 'aQbWcQ'

    Parameters

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

      a non-empty list of iterables to interleave with.

    Returns Iter<Elem>

interleaveAll

  • interleaveAll(...otherIterables: NonEmpty<Iterable<Elem>>): Iter<Elem>
  • Returns an Iter 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
    iter('abcba').interleave('QWQ').join()
    result: 'aQbWcQba'

    Parameters

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

      a non-empty list of iterables to interleave with.

    Returns Iter<Elem>

interleaveRound

  • interleaveRound(...otherIterables: NonEmpty<Iterable<Elem>>): Iter<Elem>
  • Returns an Iter 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
    iter('abc').interleaveRound('QW').take(10).join()
    result: 'aQbWcQaWbQ'

    Parameters

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

      a non-empty list of iterables to interleave with.

    Returns Iter<Elem>

intersperse

  • intersperse(interIter: Iterable<Elem>): Iter<Elem>
  • Returns an Iter that yields the elements of this Iter with the given interIter elements as a sepatator

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

    Parameters

    • interIter: Iterable<Elem>

      an iterator of elements that is used as a separator

    Returns Iter<Elem>

join

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

    example
    iter.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 string

map

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

    example
    iter.of(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 Iter<R>

mkGroup

  • mkGroup(startIter?: Iterable<Elem>, sepIter?: Iterable<Elem>, endIter?: Iterable<Elem>): Iter<Elem>
  • Returns an Iter that starts with the startIter elements, then yields all elements of this Iter with the sepIter elements as separators, and ends with the endIter elements.

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

    Parameters

    • Default value startIter: Iterable<Elem> = iter.empty()

      the start elements

    • Default value sepIter: Iterable<Elem> = iter.empty()

      the separator elements

    • Default value endIter: Iterable<Elem> = iter.empty()

      the end elements

    Returns Iter<Elem>

monitor

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

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

    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

    Returns Iter<Elem>

patchAt

  • patchAt(index: number, remove: number, insert?: undefined | function): Iter<Elem>
  • Returns an Iter 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
    iter('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 Iter<Elem>

patchElem

  • patchElem(elem: Elem, remove: number, insert?: Iterable<Elem>, amount?: undefined | number): Iter<Elem>
  • Returns an Iter 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
    iter('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: Iterable<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 Iter<Elem>

patchWhere

  • patchWhere(pred: Pred<Elem>, remove: number, insert?: undefined | function, amount?: undefined | number): Iter<Elem>
  • Returns an Iter 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
    iter.of(0, 1, 5, 2).patchWhere(isEven, 1, () => [10, 11])
    result: (10, 11, 1, 5, 10, 11)
    example
    iter.of(0, 1, 5, 2).patchWhere(isEven, 0, () => [10, 11])
    result: (10, 11, 0, 1, 5, 10, 11, 2)
    example
    iter.of(0, 1, 5, 2).patchWhere(isEven, 2)
    result: (5)
    example
    iter.of(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 Iter<Elem>

prepend

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

    example
    iter.of(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 Iter<Elem>

reduce

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

    example
    iter.range(0, 5).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 Iter, 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 Elem

repeat

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

    example
    iter.of(1, 3).repeat(3)
    result: (1, 3, 1, 3, 1, 3)
    example
    iter.of(1, 3).repeat()
    result: (1, 3, 1, 3, 1, ...)

    Parameters

    • Optional times: undefined | number

      the amount of times to repeat this Iter

    Returns Iter<Elem>

sample

  • sample(nth: number): Iter<Elem>
  • Returns an Iter that yields only each given nth value of this Iter.

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

    Parameters

    • nth: number

      the amount of elements to skip every in between emissions.

    Returns Iter<Elem>

slice

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

    example
    iter.nats.slice(3, 4)
    result: (3, 4, 5, 6)

    Parameters

    • from: number

      the index at which to start yielding values

    • amount: number

      the maximum amount of items to yield

    Returns Iter<Elem>

sliding

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

    example
    iter.nats.sliding(3)
    result: ([0, 1, 2], [3, 4, 5], [6, 7, 8], ...)
    example
    iter.nats.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 Iter<Elem[]>

splitOnElem

  • splitOnElem(elem: Elem): Iter<Elem[]>
  • Returns an Iter that yields arrays of values of this Iter each time a value that equals given elem is encountered

    example
    iter('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 Iter<Elem[]>

splitWhere

  • splitWhere(pred: Pred<Elem>): Iter<Elem[]>
  • Returns an Iter that yields arrays of values of this Iter each time a value is encountered that satisfied the given pred predicate

    example
    iter.range(4).splitWhere(isPrime)
    result: ([4], [6], [8, 9, 10], [12], ...)

    Parameters

    • pred: Pred<Elem>

      a predicate for an element of this Iter and its index

    Returns Iter<Elem[]>

take

  • take(amount: number): Iter<Elem>
  • Returns an Iter that yields the first given amount of elements of this Iter if present, then ends.

    example
    iter.nats.take(4)
    result: (0, 1, 2, 3)

    Parameters

    • amount: number

      the amount of elements to yield

    Returns Iter<Elem>

takeLast

  • takeLast(amount: number): Iter<Elem>
  • Returs an Iter that yields the last amount of elements of this Iter.

    Parameters

    • amount: number

      the amount of elements to yield

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

    Returns Iter<Elem>

takeWhile

  • takeWhile(pred: Pred<Elem>): Iter<Elem>
  • Returns an Iter that yields the items of this Iter as long as the given pred predicate holds, then ends.

    example
    iter.nats.takeWhile(v => v < 5)
    result: (0, 1, 2, 3, 4)

    Parameters

    • pred: Pred<Elem>

      a predicate taking an element and its index

    Returns Iter<Elem>

toArray

  • toArray(): Elem[]

toAsync

toSet

  • toSet(): Set<Elem>

toString

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

    Returns String

zip

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

    example
    iter.nats.zip([5, 2, 3])
    result: ([0, 5], [1, 2], [3, 3])

    Type parameters

    • O: NonEmpty<any>

      the type of the Iter elements of the first given iterable

    Parameters

    • Rest ...otherIterables: ZipIters<O>

      the other iterables to zip

    Returns Iter<Unshift<O, Elem>>

zipAll

  • zipAll<O>(...otherIterables: ZipIters<O>): Iter<OptList<Unshift<O, Elem>>>
  • Returns an Iter containing tuples of each next element of this Iter 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
    iter.of(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: ZipIters<O>

      the other iterables to zip

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

zipAllWith

  • zipAllWith<O, R>(zipFun: function, ...otherIterables: ZipIters<O>): Iter<R>
  • Returns an Iter yielding the result from applying the given zipFun to the next element of this Iter 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 Iter ends. Note that the type of the first iterable is taken into account, however the other iterable elements will be cast to any.

    example
    iter.range(0, 10, 3).zipAllWith((a, b) => [b, a], [5, 7])
    result: ([5, 0], [7, 10], [undefined, 3])

    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, 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: ZipIters<O>

      the other iterables to zip

    Returns Iter<R>

zipWith

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

    example
    iter.nats.zipWith((a, b) => a + b, [5, 2, 3])
    result: (5, 3, 5)

    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: ZipIters<O>

      the other iterables to zip

    Returns Iter<R>

zipWithIndex

  • zipWithIndex(): Iter<[Elem, number]>
  • Returns an Iter yielding tuples of the elements of this Iter as first elements, and their indices as second element.

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

    Returns Iter<[Elem, number]>

Static fromIterable

  • fromIterable<E>(iterable: Iterable<E>): Iter<E>
  • Returns an Iter yielding items from an iterable

    Type parameters

    • E

      The type of elements the Iterable yields.

    Parameters

    • iterable: Iterable<E>

      the source iterable

    Returns Iter<E>

Generated using TypeDoc