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.
Reducer.Instance<I,O>A reducer instance that manages its own state based on the reducer definition that was used to create this instance.

Classes

NameDescription
BaseA base class that can be used to easily create Reducer instances.
InstanceImplThe default Reducer.Impl implementation.
InvalidCombineShapeErrorundocumented
ReducerHaltedErrorundocumented

Functions

combine

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

Definition

function combine<T, const S extends Reducer.CombineShape<T>>(shape: S & Reducer.CombineShape<T>): Reducer<T, Reducer.CombineResult<S>>;

Type parameters
NameDescription
Tthe input value type for all the reducers
Sthe desired result shape type

Parameters

NameTypeDescription
shapeS & Reducer.CombineShape<T>a shape defining where reducer outputs will be located in the result. It can consist of a single reducer, an array of shapes, or an object with string keys and shapes as values.
example
const red = Reducer.combine([Reducer.sum, { av: [Reducer.average] }])
console.log(Stream.range({amount: 9 }).reduce(red))
// => [36, { av: [4] }]

constant

Returns a Reducer that always outputs the given value, and does not accept input values.

Definition

function constant<T>(value: OptLazy<T>): Reducer<any, T>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
valueOptLazy<T>

contains

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

Definition

function contains<T, T2 extends T = T>(elem: T2, options?: {
    amount?: number;
    eq?: Eq<T | T2>;
    negate?: boolean;
  }): Reducer<T, boolean>;

Type parameters
NameDescription
Tthe element type
T2

Parameters

NameTypeDescription
elemT2the element to search for
options{
    amount?: number;
    eq?: Eq<T | T2>;
    negate?: boolean;
  }
(optional) an object containing the following properties:
- amount: (detaulf: 1) the amount of elements to find - eq: (default: Eq.objectIs) the Eq instance to use to compare elements - negate: (default: false) when true will invert the given predicate
example
console.log(Stream.range({ amount: 10 }).reduce(Reducer.contains(5)))
// => true

containsSlice

Returns a Reducer that returns true if the input values contain the given slice sequence amount times. Otherwise, returns false.

Definition

function containsSlice<T>(slice: StreamSource<T>, options?: {
    eq?: Eq<T> | undefined;
    amount?: number;
  }): Reducer<T, boolean>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
sliceStreamSource<T>a sequence of elements to match against
options{
    eq?: Eq<T> | undefined;
    amount?: number;
  }
(optional) an object containing the following properties:
- amount: (detaulf: 1) the amount of elements to find - eq: (default: Eq.objectIs) the Eq instance to use to compare elements

create

Returns a Reducer with the given options:

Definition

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

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

Parameters

NameTypeDescription
init(initHalt: () => void) => Sthe 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, index: number, halted: boolean) => 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: (initHalt: () => void) => T, next: (current: T, next: T, index: number, halt: () => void) => T, stateToResult?: (state: T, index: number, halted: boolean) => T): Reducer<T>;

Type parameters
NameDescription
Tthe overall value type

Parameters

NameTypeDescription
init(initHalt: () => void) => Tthe 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, index: number, halted: boolean) => 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: (initHalt: () => void) => O, next: (current: O, next: I, index: number, halt: () => void) => O, stateToResult?: (state: O, index: number, halted: boolean) => O): Reducer<I, O>;

Type parameters
NameDescription
Ithe input value type
Othe output value type

Parameters

NameTypeDescription
init(initHalt: () => void) => Othe 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, index: number, halted: boolean) => 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'

endsWithSlice

Returns a Reducer that returns true if the last input values match the given slice values repeated amount times. Otherwise, returns false.

Definition

function endsWithSlice<T>(slice: StreamSource<T>, options?: {
    eq?: Eq<T> | undefined;
    amount?: number;
  }): Reducer<T, boolean>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
sliceStreamSource<T>a sequence of elements to match against
options{
    eq?: Eq<T> | undefined;
    amount?: number;
  }
(optional) an object containing the following properties:
- amount: (detaulf: 1) the amount of elements to find - eq: (default: Eq.objectIs) the Eq instance to use to compare elements

equals

Returns a Reducer that ouputs true when the received elements match the given other stream source according to the eq instance, false otherwise.

Definition

function equals<T>(other: StreamSource<T>, options?: {
    eq?: Eq<T>;
    negate?: boolean;
  }): Reducer<T, boolean>;

Type parameters
NameDescription
Tthe element type

Parameters

NameTypeDescription
otherStreamSource<T>a stream source containg elements to match against
options{
    eq?: Eq<T>;
    negate?: boolean;
  }
(optional) an object containing the following properties:
- eq: (default: Eq.objectIs) the Eq instance to use to compare elements - negate: (default: false) when true will invert the given predicate

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, options?: {
    negate?: 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
options{
    negate?: boolean;
  }
(optional) an object containing the following properties:
- negate: (default: false) when true will invert the given predicate
example
console.log(Stream.range{ amount: 10 }).reduce(Reducer.every(v => v < 5))
// => false

fold

Returns a Reducer that uses the given init and next values to fold the input values into result values.

Definition

function fold<T, R>(init: OptLazy<R>, next: (current: R, value: T, index: number, halt: () => void) => R): Reducer<T, R>;

Type parameters
NameDescription
Tthe input type
Rthe output type

Parameters

NameTypeDescription
initOptLazy<R>an (optionally lazy) initial result value
next(current: R, value: T, index: number, halt: () => void) => Ra function taking the following arguments:
- current - the current result value
- value - the next input value
- index: the input index value
- halt: function that, when called, ensures no more elements are passed to the reducer

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
Tthe input element type

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, options?: {
    negate?: 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
options{
    negate?: boolean;
  }
(optional) an object containing the following properties:
- negate: (default: false) when true will invert the given predicate
example
console.log(Stream.range{ amount: 10 }).reduce(Reducer.some(v => v > 5))
// => true

startsWithSlice

Returns a Reducer that returns true if the first input values match the given slice values repeated amount times. Otherwise, returns false.

Definition

function startsWithSlice<T>(slice: StreamSource<T>, options?: {
    eq?: Eq<T> | undefined;
    amount?: number;
  }): Reducer<T, boolean>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
sliceStreamSource<T>a sequence of elements to match against
options{
    eq?: Eq<T> | undefined;
    amount?: number;
  }
(optional) an object containing the following properties:
- amount: (detaulf: 1) the amount of elements to find - eq: (default: Eq.objectIs) the Eq instance to use to compare elements

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>(options?: {
    reversed?: boolean;
  }): Reducer<T, T[]>;

Type parameters
NameDescription
Tthe element type

Parameters

NameTypeDescription
options{
    reversed?: boolean;
  }
(optional) object specifying the following properties
- reversed: (optional) when true will create a reversed array
example
console.log(Stream.of(1, 2, 3).reduce(Reducer.toArray()))
// => [1, 2, 3]
console.log(Stream.of(1, 2, 3).reduce(Reducer.toArray({ reversed: true })))
// => [3, 2, 1]

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<readonly [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' }

toJSMultiMap

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

Definition

function toJSMultiMap<K, V>(): Reducer<readonly [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<readonly [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
andA 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.
countA Reducer that remembers the amount of input items provided.
firstReturns a Reducer that remembers the first input value.
groupByReturns a Reducer that uses the valueToKey function to calculate a key for each value, and feeds the tuple of the key and the value to the collector reducer. Finally, it returns the output of the collector. If no collector is given, the default collector will return a JS multimap of the type Map<K, V[]>.
isEmptyA Reducer that outputs true if no input values are received, false otherwise.
lastReturns a Reducer that remembers the last input value.
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
nonEmptyA Reducer that outputs true if one or more input values are received, false otherwise.
orA Reducer that takes boolean values and outputs true if one or more input values are true, and false otherwise.
partitionReturns a Reducer that splits the incoming values into two separate outputs based on the given pred predicate. Values for which the predicate is true are fed into the collectorTrue reducer, and other values are fed into the collectorFalse instance. If no collectors are provided the values are collected into arrays.
pipeReturns a Reducer instance that first applies this reducer, and then applies the given next reducer to each output produced by the previous reducer.
productA Reducer that calculates the product of all given numeric input values.
raceReturns a Reducer that feeds incoming values to all reducers in the provided reducers source, and halts when the first reducer in the array is halted and returns the output of that reducer. Returns the otherwise value if no reducer is yet halted.
singleReturns a Reducer that only produces an output value when having receives exactly one input value, otherwise will return the otherwise value or undefined.
sumA Reducer that sums all given numeric input values.