Options
All
  • Public
  • Public/Protected
  • All
Menu

Module ops

Index

Variables

Const and

and: Op<boolean> = every(v => v)

Returns a collector that returns true if all received booleans are true

example
iter([true, false]).collect(Collectors.and)
result: false

Const average

average: Op<number> = Op.create({init: 0,next: (avg, value, index) => avg + (value - avg) / (index + 1)})

Returns a collector that outputs the average of all received numbers

example
iter([1, 4, 4]).collect(Collectors.average)
result: 3

Const count

count: Op<any, number> = Op.create({init: 0,next: (_, __, index) => index + 1})

Returns a collector that outputs the the amount of elements processed

example
iter([1, 3, 5]).collect(Collectors.count)
result: 3

Const hasValue

hasValue: Op<any, boolean> = some(() => true)

Returns a collector that returns true if any value is received

example
iter([1, 2]).collect(Collectors.hasValue)
result: true

Const noValue

noValue: Op<any, boolean> = every(() => false)

Returns a collector that returns true if no value is received

example
iter([1, 2]).collect(Collectors.noValue)
result: false

Const or

or: Op<boolean> = some(v => v)

Returns a collector that returns true if any received booleans is true

example
iter([true, false]).collect(Collectors.or)
result: true

Const product

product: Op<number> = Op.create({init: 1,next: (state, num) => state * num,escape: state => state === 0})

Returns a collector that outputs the product of all received numbers

example
iter([1, 2, 5]).collect(Collectors.product)
result: 10

Const range

range: Op<number, [number, number]> = Op.combine(min(), max())

Returns a collector that outputs a tuple containing the minimum and maximum value of the inputs.

note

throws if an empty input is received.

example
iter([4, 1, 3]).collect(Collectors.range)
result: [1, 4]

Const stringAppend

stringAppend: Op<any, string> = Op.create({init: '',next: (state, elem) => state.concat(String(elem))})

Returns a collector that takes all elements of any type, converts them to a string, and concatenates them.

example
iter([1, 2, 3]).collect(Collectors.stringAppend)
result: '123'

Const stringPrepend

stringPrepend: Op<any, string> = Op.create({init: '',next: (state, elem) => String(elem).concat(state)})

Returns a collector that takes all elements of any type, converts them to a string, and concatenates them in reverse order.

example
iter([1, 2, 3]).collect(Collectors.stringPrepend)
result: '321'

Const sum

sum: Op<number> = Op.create({init: 0,next: (state, num) => state + num})

Returns a collector that outputs the sum of all received numbers

example
iter([1, 2, 5]).collect(Collectors.sum)
result: 8

Functions

choose

  • choose<Elem>(choice: function, otherwise?: OptLazy<Elem>): Op<Elem>
  • Returns a collector that takes the first element of the iterable, and then uses the result of the choice function to decide whether to keep the currently chosen value, or the new element.

    example
    iter(['abCd', 'plCf', 'bcae']).collect(Collectors.choose((chosen, next) => next[2] === chosen[2]))
    result: 'plc'

    Type parameters

    • Elem

      the element type

    Parameters

    • choice: function

      a function taking two elements, and returning false to keep the first, and true to keep the second element

        • (chosen: Elem, next: Elem, index: number): boolean
        • Parameters

          • chosen: Elem
          • next: Elem
          • index: number

          Returns boolean

    • Default value otherwise: OptLazy<Elem> = throwCollectError

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

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

    Returns Op<Elem>

contains

  • contains<Elem>(elem: Elem): Op<Elem, boolean>
  • Returns a collector that returns true if any received element equals given elem.

    example
    iter([1, 3, 4, 7]).collect(Collectors.contains(3))
    result: true

    Type parameters

    • Elem

      the input element type

    Parameters

    • elem: Elem

    Returns Op<Elem, boolean>

containsAny

  • containsAny<Elem>(...elems: Elem[]): Op<Elem, boolean>
  • Returns a collector that returns true if any of the received elements is contained the given elems.

    example
    iter([1, 3, 4, 7]).collect(Collectors.containsAny(3, 20, 10))
    result: true

    Type parameters

    • Elem

      the input element type

    Parameters

    • Rest ...elems: Elem[]

    Returns Op<Elem, boolean>

elemAt

  • elemAt<Elem>(index: number, otherwise?: OptLazy<Elem>): Op<Elem, Elem>
  • Returns a collector that returns the element received at position index. If no such value is received, it tries to get an alternative value from otherwise

    example
    iter('abcdef').collect(Collectors.elemAt(3))
    result: 'd'

    Type parameters

    • Elem

      the input element type

    Parameters

    • index: number
    • Default value otherwise: OptLazy<Elem> = throwCollectError

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

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

    Returns Op<Elem, Elem>

elementsByFreq

  • elementsByFreq<Elem>(): Op<Elem, UniqueDict<number, Elem>>
  • Returns a collector that returns a Map with amount of occurances as keys, and the unique set of elements with that amount of occurrance as values

    example
    iter('adcbcd').collect(Collectors.elementsByFreq())
    result: Map(1 -> Set('a', 'b'), 2 -> Set('d', 'c'))

    Type parameters

    • Elem

      the element type

    Returns Op<Elem, UniqueDict<number, Elem>>

every

  • every<Elem>(pred: Pred<Elem>): Op<Elem, boolean>
  • Returns a collector that returns true if all received element satisfies given pred predicate.

    example
    iter([1, 3, 5, 7]).collect(Collectors.every(isOdd))
    result: true

    Type parameters

    • Elem

      the input element type

    Parameters

    Returns Op<Elem, boolean>

find

  • find<Elem>(pred: Pred<Elem>, otherwise?: OptLazy<Elem>): Op<Elem, Elem>
  • Returns a collector that outputs the first element it encounters that satisfies pred. If no value is found, it tries to get an alternative value from otherwise

    example
    iter.nats.collect(Collectors.find(v => v > 10))
    result: 11

    Type parameters

    • Elem

      the input element type

    Parameters

    • pred: Pred<Elem>

      a predicate over elements Elem

    • Default value otherwise: OptLazy<Elem> = throwCollectError

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

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

    Returns Op<Elem, Elem>

findLast

  • findLast<Elem>(pred: Pred<Elem>, otherwise?: OptLazy<Elem>): Op<Elem, Elem>
  • Returns a collector that outputs the last element it encounters that satisfies pred. If no value is found, it tries to get an alternative value from otherwise

    example
    iter([1, 4, 2, 9, 3, 8]).collect(Collectors.findLast(v => v < 8))
    result: 3

    Type parameters

    • Elem

      the input element type

    Parameters

    • pred: Pred<Elem>

      a predicate over elements Elem

    • Default value otherwise: OptLazy<Elem> = throwCollectError

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

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

    Returns Op<Elem, Elem>

first

  • first<Elem>(otherwise?: OptLazy<Elem>): Op<Elem, Elem>
  • Returns a collector that returns the first element it receives. If no value is received, it tries to get an alternative value from otherwise

    example
    iter('abc').collect(Collectors.first())
    result: 'a'

    Type parameters

    • Elem

      the input element type

    Parameters

    • Default value otherwise: OptLazy<Elem> = throwCollectError

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

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

    Returns Op<Elem, Elem>

groupBy

  • groupBy<Key, Value>(keyFun: function): Op<Value, Dict<Key, Value>>
  • Returns a collector that creates a Dictionary using the given keyFun to generate keys for received elements.

    example
    iter(['foo', 'test', 'bar']).collect(Collectors.groupBy(v => v.length))
    result: Map(3 -> ['foo', 'bar'], 4 -> ['test'])

    Type parameters

    • Key

      the dictionary key type

    • Value

      the dictionary value type

    Parameters

    • keyFun: function

      a function that takes an element V and returns its key K

        • (value: Value, index: number): Key
        • Parameters

          • value: Value
          • index: number

          Returns Key

    Returns Op<Value, Dict<Key, Value>>

groupByGen

  • groupByGen<Key, Value, S>(keyFun: function, buildSeqOp: Op<Value, S>): Op<Value, Map<Key, S>>
  • Returns a collector that groups incoming value by given keyFun, and uses the buildSeqOp to create sequences of those values.

    example
    iter(['foo', 'test', 'bar']).collect(Collectors.groupByGen(v => v.length, Collectors.toSet()))
    result: Map(3 -> Set(['foo', 'bar']), 4 -> Set(['test']))

    Type parameters

    • Key

      the dictionary key type

    • Value

      the dictionary value type

    • S

      the result sequence type

    Parameters

    • keyFun: function

      a function that takes an element V and returns its key K

        • (value: Value, index: number): Key
        • Parameters

          • value: Value
          • index: number

          Returns Key

    • buildSeqOp: Op<Value, S>

      a collector that builds a structure from given elements Value

    Returns Op<Value, Map<Key, S>>

groupByUnique

  • groupByUnique<Key, Value>(keyFun: function): Op<Value, UniqueDict<Key, Value>>
  • Returns a collector that creates a UniqueDictionary using the given keyFun to generate keys for received elements.

    example
    iter(['foo', 'test', 'foo']).collect(Collectors.groupBy(v => v.length))
    result: Map(3 -> Set('foo'), 4 -> Set('test'))

    Type parameters

    • Key

      the dictionary key type

    • Value

      the dictionary value type

    Parameters

    • keyFun: function

      a function that takes an element Value and returns its key Key

        • (value: Value, index: number): Key
        • Parameters

          • value: Value
          • index: number

          Returns Key

    Returns Op<Value, UniqueDict<Key, Value>>

histogram

  • histogram<Elem>(sortBy?: "TOP" | "BOTTOM", amount?: undefined | number): Op<Elem, Histogram<Elem>>
  • Returns a collector that creates a histogram of the received elements. That is, it creates a Map with the elements as keys and the amount of occurrances as values.

    example
    iter('adcbcd').collect(Collectors.histogram())
    result: Map('a' -> 1, 'd' -> 2, 'c' -> 2, 'b' -> 1)

    Type parameters

    • Elem

      the element type

    Parameters

    • Optional sortBy: "TOP" | "BOTTOM"

      if undefined will return the histogram in value order, if value is 'TOP' it will order the histogram from highest to lowest, otherwise it will order the histogram from lowest to highest frequency.

    • Optional amount: undefined | number

      if sortBy is specified, this parameter limits the amount of results

    Returns Op<Elem, Histogram<Elem>>

last

  • last<Elem>(otherwise?: OptLazy<Elem>): Op<Elem, Elem>
  • Returns a collector that returns the last element it receives. If no value is received, it tries to get an alternative value from otherwise

    example
    iter('abc').collect(Collectors.last())
    result: 'c'

    Type parameters

    • Elem

      the input element type

    Parameters

    • Default value otherwise: OptLazy<Elem> = throwCollectError

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

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

    Returns Op<Elem, Elem>

max

  • max(otherwise?: OptLazy<number>): Op<number>
  • Returns a collector that outputs the maximum of all received numbers

    example
    iter([3, 1, 4]).collect(Collectors.max())
    result: 4

    Parameters

    • Default value otherwise: OptLazy<number> = throwCollectError

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

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

    Returns Op<number>

maxBy

  • maxBy<Elem>(toNumber: function, otherwise?: OptLazy<Elem>): Op<Elem>
  • Returns a collector that performs the toNumber function on each element, and returns the element for which the result of toNumber returned the maximum value.

    example
    iter(['aa', 'a', 'aaa']).collect(Collectors.maxBy(w => w.length))
    result: 'aaa'

    Type parameters

    • Elem

      the element type

    Parameters

    • toNumber: function

      a function taking an element an returning a number to compare

        • (value: Elem): number
        • Parameters

          • value: Elem

          Returns number

    • Default value otherwise: OptLazy<Elem> = throwCollectError

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

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

    Returns Op<Elem>

min

  • min(otherwise?: OptLazy<number>): Op<number>
  • Returns a collector that outputs the minimum of all received numbers

    example
    iter([3, 1, 4]).collect(Collectors.min())
    result: 1

    Parameters

    • Default value otherwise: OptLazy<number> = throwCollectError

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

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

    Returns Op<number>

minBy

  • minBy<Elem>(toNumber: function, otherwise?: OptLazy<Elem>): Op<Elem>
  • Returns a collector that performs the toNumber function on each element, and returns the element for which the result of toNumber returned the minimum value.

    example
    iter(['aa', 'a', 'aaa']).collect(Collectors.minBy(w => w.length))
    result: 'a'

    Type parameters

    • Elem

      the element type

    Parameters

    • toNumber: function

      a function taking an element an returning a number to compare

        • (value: Elem): number
        • Parameters

          • value: Elem

          Returns number

    • Default value otherwise: OptLazy<Elem> = throwCollectError

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

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

    Returns Op<Elem>

partition

  • partition<Elem>(pred: Pred<Elem>): Op<Elem, [Elem[], Elem[]]>
  • Returns a collector that creates a tuple of element arrays based on the given pred. The first array are the elements that satisfy pred, and the second array contains those that don't.

    example
    iter([1, 2, 3, 4, 5]).collect(Collectors.partition(isEven))
    result: [[2, 4], [1, 3, 5]]

    Type parameters

    • Elem

      the element type

    Parameters

    • pred: Pred<Elem>

      a predicate over elements Elem

    Returns Op<Elem, [Elem[], Elem[]]>

partitionGen

  • partitionGen<Elem, S>(pred: Pred<Elem>, buildSeqOp: Op<Elem, S>): Op<Elem, [S, S]>
  • Returns a collector that creates a tuple of structures build with buildSeqOp based on the given pred. The first structure contains the elements that satisfy pred, and the second structure contains those that don't.

    example
    iter([1, 2, 3, 4, 5]).collect(Collectors.partitionGen(isEven, Collectors.toSet()))
    result: [Set([2, 4]), Set([1, 3, 5])]

    Type parameters

    • Elem

      the element type

    • S

    Parameters

    • pred: Pred<Elem>

      a predicate over elements Elem

    • buildSeqOp: Op<Elem, S>

      a collector that builds a structure from given elements Elem

    Returns Op<Elem, [S, S]>

rangeBy

  • rangeBy<Elem>(toNumber: function): Op<Elem, [Elem, Elem]>
  • Returns a collector that performs the toNumber function on each element, and returns a tuple of the elements for which the function returned the minumum and maximum values.

    note

    throws an error when the input is empty

    example
    iter(['aa', 'a', 'aaa']).collect(Collectors.rangeBy(w => w.length))
    result: ['a', 'aaa']

    Type parameters

    • Elem

    Parameters

    • toNumber: function

      a function taking an element an returning a number to compare

        • (value: Elem): number
        • Parameters

          • value: Elem

          Returns number

    Returns Op<Elem, [Elem, Elem]>

some

  • some<Elem>(pred: Pred<Elem>): Op<Elem, boolean>
  • Returns a collector that returns true if any received element satisfies given pred predicate.

    example
    iter([1, 3, 5, 7]).collect(Collectors.some(isEven))
    result: false

    Type parameters

    • Elem

      the input element type

    Parameters

    Returns Op<Elem, boolean>

splitAt

  • splitAt<Elem>(index: number): Op<Elem, [Elem[], Elem[]]>
  • Returns a collector that creates a tuple of element arrays where the first array contains the elements upto the given 'index', and the second array contains the other elements.

    example
    iter([1, 2, 3, 4, 5]).collect(Collectors.splitAt(3))
    result: [[1, 2, 3], [4, 5]]

    Type parameters

    • Elem

      the element type

    Parameters

    • index: number

      the index at which to start the second array

    Returns Op<Elem, [Elem[], Elem[]]>

splitAtGen

  • splitAtGen<Elem, S>(index: number, buildSeqOp: Op<Elem, S>): Op<Elem, [S, S]>
  • Returns a collector that creates a tuple of structures build by buildSeqOp where the first structure contains the elements upto the given 'index', and the second structure contains the other elements.

    example
    iter([1, 2, 3, 4, 5]).collect(Collectors.splitAtGen(3, Collectors.toSet()))
    result: [Set([1, 2, 3]), Set([4, 5])]

    Type parameters

    • Elem

      the element type

    • S

    Parameters

    • index: number

      the index at which to start the second array

    • buildSeqOp: Op<Elem, S>

    Returns Op<Elem, [S, S]>

toArray

  • toArray<Elem>(reversed?: boolean, target?: Elem[]): Op<Elem, Elem[]>
  • Returns a collector that created a potentially reversed array from the input elements.

    Type parameters

    • Elem

      the element type

    Parameters

    • Default value reversed: boolean = false

      if true will prepend elements to the target instead of append

    • Optional target: Elem[]

      an optional existing array to which the elements will be added

    Returns Op<Elem, Elem[]>

toMap

  • toMap<Key, Value>(target?: Map<Key, Value>): Op<[Key, Value], Map<Key, Value>>
  • Returns a collector that creates a Map from the received tuples of type [K, V]. Note: modifies the target Map

    example
    iter([['a', 1], ['b', 5]]).collect(Collectors.toMap())
    result: Map(a -> 1, b -> 5)

    Type parameters

    • Key

      the map key type

    • Value

      the map value type

    Parameters

    • Optional target: Map<Key, Value>

      the target Map

    Returns Op<[Key, Value], Map<Key, Value>>

toObject

  • toObject<V>(target?: undefined | __type): Op<[string, V], object>
  • Returns a collector that takes tuple elements of [string, V] and returns an object with those keys and values. Note: mutates the target object

    example
    iter([['foo', 1], ['bar', true]]).collect(Collectors.toObject())
    result: { foo: 1, bar: true}

    Type parameters

    • V

      the value type

    Parameters

    • Optional target: undefined | __type

      a target object to add the properties to

    Returns Op<[string, V], object>

toSet

  • toSet<Elem>(target?: Set<Elem>): Op<Elem, Set<Elem>>
  • Returns a collector that creates a Set from the received elements. Note: modifies the target Set

    example
    iter([1, 3, 5, 3, 1]).collect(Collectors.toSet())
    result: Set(1, 3, 5)

    Type parameters

    • Elem

      the element type

    Parameters

    • Optional target: Set<Elem>

      the target Set

    Returns Op<Elem, Set<Elem>>

Generated using TypeDoc