Skip to main content

namespace AsyncReducer

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

Companion type: AsyncReducer<I,O>

Interfaces

NameDescription
AsyncReducer.Impl<I,O,S>undocumented

Classes

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

Functions

combineArr

Returns an AsyncReducer 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]: AsyncReducer<T, R[K]>;
  } & AsyncReducer<T, unknown>[]): AsyncReducer<T, R>;

Type parameters
NameDescription
T
R

Parameters

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

combineObj

Returns an AsyncReducer 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]: AsyncReducer<T, R[K]>;
  } & Record<string, AsyncReducer<T, unknown>>): AsyncReducer<T, R>;

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

Parameters

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

contains

Returns an AsyncReducer 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>): AsyncReducer<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
await AsyncStream.from(Stream.range({ amount: 10 })).reduce(AsyncReducer.contains(5)))
// => true

create

Returns an AsyncReducer with the given options:

Definition

function create<I, O = I, S = O>(init: AsyncOptLazy<S>, next: (current: S, next: I, index: number, halt: () => void) => MaybePromise<S>, stateToResult: (state: S) => MaybePromise<O>, onClose?: (state: S, error?: unknown) => MaybePromise<void>): AsyncReducer<I, O>;

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

Parameters

NameTypeDescription
initAsyncOptLazy<S>the optionally lazy and/or promised initial state value
next(current: S, next: I, index: number, halt: () => void) => MaybePromise<S>returns (potentially asynchronously) 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) => MaybePromise<O>a potentially asynchronous function that converts the current state to an output value
onClose(state: S, error?: unknown) => MaybePromise<void>(optional) a function that will be called when the reducer will no longer receive values

createMono

Returns an AsyncReducer of which the input, state, and output types are the same.

Definition

function createMono<T>(init: AsyncOptLazy<T>, next: (current: T, next: T, index: number, halt: () => void) => MaybePromise<T>, stateToResult?: (state: T) => MaybePromise<T>, onClose?: (state: T, error?: unknown) => MaybePromise<void>): AsyncReducer<T>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
initAsyncOptLazy<T>the optionally lazy and/or promised initial state value
next(current: T, next: T, index: number, halt: () => void) => MaybePromise<T>returns (potentially asynchronously) 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) => MaybePromise<T>a potentially asynchronous function that converts the current state to an output value
onClose(state: T, error?: unknown) => MaybePromise<void>(optional) a function that will be called when the reducer will no longer receive values

createOutput

Returns an AsyncReducer of which the state and output types are the same.

Definition

function createOutput<I, O = I>(init: AsyncOptLazy<O>, next: (current: O, next: I, index: number, halt: () => void) => MaybePromise<O>, stateToResult?: (state: O) => MaybePromise<O>, onClose?: (state: O, error?: unknown) => MaybePromise<void>): AsyncReducer<I, O>;

Type parameters
NameDescription
Ithe input value type
Othe output value type

Parameters

NameTypeDescription
initAsyncOptLazy<O>the optionally lazy and/or promised initial state value
next(current: O, next: I, index: number, halt: () => void) => MaybePromise<O>returns (potentially asynchronously) 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) => MaybePromise<O>a potentially asynchronous function that converts the current state to an output value
onClose(state: O, error?: unknown) => MaybePromise<void>(optional) a function that will be called when the reducer will no longer receive values

every

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

Definition

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

Type parameters
NameDescription
Tthe element type

Parameters

NameTypeDescription
pred(value: T, index: number) => MaybePromise<boolean>a function taking an input value and its index, and returning true if the value satisfies the predicate
example
await AsyncStream.from(Stream.range{ amount: 10 })).reduce(AsyncReducer.every(async v => v < 5))
// => false

from

undocumented

Definition

function from<I, O>(reducer: Reducer<I, O>): AsyncReducer<I, O>;

Type parameters
NameDescription
I
O

Parameters

NameTypeDescription
reducerReducer<I, O>

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;
  }): AsyncReducer<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) => MaybePromise<boolean>): AsyncReducer<T, boolean>;

Type parameters
NameDescription
Tthe element type

Parameters

NameTypeDescription
pred(value: T, index: number) => MaybePromise<boolean>a function taking an input value and its index, and returning true if the value satisfies the predicate
example
await AsyncStream.from(Stream.range{ amount: 10 })).reduce(AsyncReducer.some(async v => v > 5))
// => true

toArray

Returns an AsyncReducer 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>(): AsyncReducer<T, T[]>;

Type parameters
NameDescription
Tthe element type
example
await AsyncStream.of(1, 2, 3).reduce(AsyncReducer.toArray()))
// => [1, 2, 3]

toJSMap

Returns a AsyncReducer 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>(): AsyncReducer<[K, V], Map<K, V>>;

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

toJSObject

Returns an AsyncReducer 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>():AsyncReducer<[K, V], Record<K, V>>;

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

toJSSet

Returns an AsyncReducer 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>(): AsyncReducer<T, Set<T>>;

Type parameters
NameDescription
Tthe element type
example
await AsyncStream.of(1, 2, 3).reduce(AsyncReducer.toJSSet()))
// Set {1, 2, 3}

Constants

NameDescription
andReturns an AsyncReducer 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 an AsyncReducer that remembers the amount of input items provided.
firstReturns an AsyncReducer that remembers the first input value.
firstWhereReturns an AsyncReducer that remembers the first input value for which the given pred function returns true.
isEmptyReturns an AsyncReducer that outputs true if no input values are received, false otherwise.
lastReturns an AsyncReducer that remembers the last input value.
lastWhereReturns an AsyncReducer 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 an AsyncReducer that outputs true if one or more input values are received, false otherwise.
orReturns an AsyncReducer 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.