Skip to main content

namespace Reducer

A Reducer is a stand-alone calculation that takes input values of type I, and, when requested, produces an output value of type O.

Companion type: Reducer<I,O>

Interfaces

NameDescription
Reducer.Impl<I,O,S>The Implementation interface for a Reducer, which also exposes the internal state type.

Classes

NameDescription
BaseA base class that can be used to easily create Reducer instances.

Functions

combineArr

Returns a Reducer that combines multiple input reducers by providing input values to all of them and collecting the outputs in an array.

Definition

function combineArr<T, R extends readonly [unknown, unknown, ...unknown[]]>(...reducers: {
    [K in keyof R]: Reducer<T, R[K]>;
  } & Reducer<T, unknown>[]): Reducer<T, R>;

Type parameters
NameDescription
T
R

Parameters

NameTypeDescription
reducers{
    [K in keyof R]: Reducer<T, R[K]>;
  } & Reducer<T, unknown>[]
2 or more reducers to combine
example
const red = Reducer.combineArr(Reducer.sum, Reducer.average)
console.log(Stream.range({amount: 9 }).reduce(red))
// => [36, 4]

combineObj

Returns a Reducer that combines multiple input reducers by providing input values to all of them and collecting the outputs in the shape of the given object.

Definition

function combineObj<T, R extends {
    readonly [key: string]: unknown;
  }>(reducerObj: {
    readonly [K in keyof R]: Reducer<T, R[K]>;
  } & Record<string, Reducer<T, unknown>>): Reducer<T, R>;

Type parameters
NameDescription
Tthe input type for all the reducers
Rthe result object shape

Parameters

NameTypeDescription
reducerObj{
    readonly [K in keyof R]: Reducer<T, R[K]>;
  } & Record<string, Reducer<T, unknown>>
an object of keys, and reducers corresponding to those keys
example
const red = Reducer.combineObj({
theSum: Reducer.sum,
theAverage: Reducer.average
});
Stream.range({ amount: 9 }).reduce(red);
// => { theSum: 36, theAverage: 4 }

contains

Returns a Reducer that outputs false as long as the given elem has not been encountered in the input values, true otherwise.

Definition

function contains<T>(elem: T, eq?: Eq<T>): Reducer<T, boolean>;

Type parameters
NameDescription
Tthe element type

Parameters

NameTypeDescription
elemTthe element to search for
eqEq<T>(optional) a comparison function that returns true if te two given input values are considered equal
example
console.log(Stream.range({ amount: 10 }).reduce(Reducer.contains(5)))
// => true

create

Returns a Reducer with the given options:

Definition

function create<I, O = I, S = O>(init: OptLazy<S>, next: (current: S, next: I, index: number, halt: () => void) => S, stateToResult: (state: S) => O): Reducer<I, O>;

Type parameters
NameDescription
Ithe input value type
Othe output value type
Sthe internal state type

Parameters

NameTypeDescription
initOptLazy<S>the initial state value
next(current: S, next: I, index: number, halt: () => void) => Sreturns the next state value based on the given inputs:
- current: the current state
- next: the current input value
- index: the input index value
- halt: function that, when called, ensures no more elements are passed to the reducer
stateToResult(state: S) => Oa function that converts the current state to an output value
example
const evenNumberOfOnes = Reducer
.create(
true,
(current, value: number) => value === 1 ? !current : current,
state => state ? 'even' : 'not even')
const result = Stream.of(1, 2, 3, 2, 1)).reduce(evenNumberOfOnes)
console.log+(result)
// => 'even'

createMono

Returns a Reducer of which the input, state, and output types are the same.

Definition

function createMono<T>(init: OptLazy<T>, next: (current: T, next: T, index: number, halt: () => void) => T, stateToResult?: (state: T) => T): Reducer<T>;

Type parameters
NameDescription
Tthe overall value type

Parameters

NameTypeDescription
initOptLazy<T>the initial state value
next(current: T, next: T, index: number, halt: () => void) => Treturns the next state value based on the given inputs:
- current: the current state
- next: the current input value
- index: the input index value
- halt: function that, when called, ensures no more elements are passed to the reducer
stateToResult(state: T) => T(optional) a function that converts the current state to an output value
example
const sum = Reducer
.createMono(
0,
(current, value) => current + value
)
const result = Stream.of(1, 2, 3, 2, 1)).reduce(sum)
console.log+(result)
// => 9

createOutput

Returns a Reducer of which the state and output types are the same.

Definition

function createOutput<I, O = I>(init: OptLazy<O>, next: (current: O, next: I, index: number, halt: () => void) => O, stateToResult?: (state: O) => O): Reducer<I, O>;

Type parameters
NameDescription
Ithe input value type
Othe output value type

Parameters

NameTypeDescription
initOptLazy<O>the initial state value
next(current: O, next: I, index: number, halt: () => void) => Oreturns the next state value based on the given inputs:
- current: the current state
- next: the current input value
- index: the input index value
- halt: function that, when called, ensures no more elements are passed to the reducer
stateToResult(state: O) => O(optional) a function that converts the current state to an output value
example
const boolToString = Reducer
.createOutput(
'',
(current, value: boolean) => current + (value ? 'T' : 'F')
)
const result = Stream.of(true, false, true)).reduce(boolToString)
console.log+(result)
// => 'TFT'

every

Returns a Reducer that ouputs true as long as all input values satisfy the given pred, false otherwise.

Definition

function every<T>(pred: (value: T, index: number) => boolean): Reducer<T, boolean>;

Type parameters
NameDescription
Tthe element type

Parameters

NameTypeDescription
pred(value: T, index: number) => booleana function taking an input value and its index, and returning true if the value satisfies the predicate
example
console.log(Stream.range{ amount: 10 }).reduce(Reducer.every(v => v < 5))
// => false

join

Returns a Reducer that joins the given input values into a string using the given options.

Definition

function join<T>({ sep, start, end, valueToString, }?: {
    sep?: string | undefined;
    start?: string | undefined;
    end?: string | undefined;
    valueToString?: ((value: T) => string) | undefined;
  }): Reducer<T, string>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
{ sep, start, end, valueToString, }{
    sep?: string | undefined;
    start?: string | undefined;
    end?: string | undefined;
    valueToString?: ((value: T) => string) | undefined;
  }
example
console.log(Stream.of(1, 2, 3).reduce(Reducer.join({ sep: '-' })))
// => '1-2-3'

some

Returns a Reducer that ouputs false as long as no input value satisfies given pred, true otherwise.

Definition

function some<T>(pred: (value: T, index: number) => boolean): Reducer<T, boolean>;

Type parameters
NameDescription
Tthe element type

Parameters

NameTypeDescription
pred(value: T, index: number) => booleana function taking an input value and its index, and returning true if the value satisfies the predicate
example
console.log(Stream.range{ amount: 10 }).reduce(Reducer.some(v => v > 5))
// => true

toArray

Returns a Reducer that collects received input values in an array, and returns a copy of that array as an output value when requested.

Definition

function toArray<T>(): Reducer<T, T[]>;

Type parameters
NameDescription
Tthe element type
example
console.log(Stream.of(1, 2, 3).reduce(Reducer.toArray()))
// => [1, 2, 3]

toJSMap

Returns a Reducer that collects received input tuples into a mutable JS Map, and returns a copy of that map when output is requested.

Definition

function toJSMap<K, V>(): Reducer<[K, V], Map<K, V>>;

Type parameters
NameDescription
Kthe map key type
Vthe map value type
example
console.log(Stream.of([1, 'a'], [2, 'b']).reduce(Reducer.toJSMap()))
// Map { 1 => 'a', 2 => 'b' }

toJSObject

Returns a Reducer that collects 2-tuples containing keys and values into a plain JS object, and returns a copy of that object when output is requested.

Definition

function toJSObject<K extends string |number|symbol, V>():Reducer<[
    K,
    V
  ], Record<K, V>>;

Type parameters
NameDescription
Kthe result object key type
Vthe result object value type
example
console.log(Stream.of(['a', 1], ['b', true]).reduce(Reducer.toJSObject()))
// { a: 1, b: true }

toJSSet

Returns a Reducer that collects received input values into a mutable JS Set, and returns a copy of that map when output is requested.

Definition

function toJSSet<T>(): Reducer<T, Set<T>>;

Type parameters
NameDescription
Tthe element type
example
console.log(Stream.of(1, 2, 3).reduce(Reducer.toJSSet()))
// Set {1, 2, 3}

Constants

NameDescription
andReturns a Reducer that takes boolean values and outputs true if all input values are true, and false otherwise.
averageA Reducer that calculates the average of all given numberic input values.
countReturns a Reducer that remembers the amount of input items provided.
firstReturns a Reducer that remembers the first input value.
firstWhereReturns a Reducer that remembers the first input value for which the given pred function returns true.
isEmptyReturns a Reducer that outputs true if no input values are received, false otherwise.
lastReturns a Reducer that remembers the last input value.
lastWhereReturns a Reducer that remembers the last input value for which the given pred function returns true.
maxReturns a Reducer that remembers the maximum value of the numberic inputs.
maxByReturns a Reducer that remembers the maximum value of the inputs using the given compFun to compare input values
minReturns a Reducer that remembers the minimum value of the numberic inputs.
minByReturns a Reducer that remembers the minimum value of the inputs using the given compFun to compare input values
nonEmptyReturns a Reducer that outputs true if one or more input values are received, false otherwise.
orReturns a Reducer that takes boolean values and outputs true if one or more input values are true, and false otherwise.
productA Reducer that calculates the product of all given numeric input values.
sumA Reducer that sums all given numeric input values.