Skip to main content

interface Reducer.Impl<I,O,S>

The Implementation interface for a Reducer, which also exposes the internal state type.

Implemented by: Base<I,O,S>

Type parameters

NameDefaultDescription
Ithe input value type
Othe output value type
Sunknownthe internal state type

Properties

init

A function that produces the initial state value for the reducer algorithm.

Definition

readonly init: (initHalt: () => void) => S;

mapInput

Returns a Reducer instance that converts its input values using given mapFun before passing them to this reducer.

Definition

mapInput: <I2>(mapFun: (value: I2, index: number) => I) => Reducer<I2, O>;

example
Reducer.sum.mapInput(v => v * 2)
// this reducer will double all input values before summing them

Methods

chain

Returns a reducer that applies this reducer and then the nextReducers sequentially on halting of each reducer. It provides the last output value of the active reducer.

Definition

chain<O2 extends O>(nextReducers: StreamSource<OptLazy<Reducer<I, O2>, [O2]>>): Reducer<I, O2>;

Type parameters

NameConstraintsDescription
O2O

Parameters

NameTypeDescription
nextReducersStreamSource<OptLazy<Reducer<I, O2>, [O2]>>an number of reducers consuming and producing the same types as the current reducer.
example
const result = Stream.range({ amount: 6 })
.reduce(
Reducer.sum
.takeInput(3)
.chain(
[v => v > 10 ? Reducer.product : Reducer.sum]
)
)
console.log(result)
// => 21

collectInput

Returns a Reducer instance that converts or filters its input values using given collectFun before passing them to the reducer.

Definition

collectInput<I2>(collectFun: CollectFun<I2, I>): Reducer<I2, O>;

Type parameters

NameDescription
I2the resulting reducer input type

Parameters

NameTypeDescription
collectFunCollectFun<I2, I>a function receiving
- value: the next value
- index: the value index
- skip: a token that, when returned, will not add a value to the resulting collection
- halt: a function that, when called, ensures no next elements are passed
example
Reducer.sum.collectInput((v, _, skip) => v <= 10 ? skip : v * 2)
// this reducer will double all input values larger thant 10 before summing them,
// and will skip all values smaller than 10

compile

Returns a 'runnable' instance of the current reducer specification. This instance maintains its own state and indices, so that the instance only needs to be provided the input values, and output values can be retrieved when needed. The state is kept private.

Definition

compile(): Reducer.Instance<I, O>;

example
const reducer = Reducer.sum.mapOutput(v => v * 2);
const instance = reducer.compile();
instance.next(3);
instance.next(5);
console.log(instance.getOutput());
// => 16

dropInput

Returns a Reducer instance that skips the first given amount of input elements, and will process subsequent elements.

Definition

dropInput(amount: number): Reducer<I, O>;

Parameters

NameTypeDescription
amountnumberthe amount of elements to skip
example
Stream.range({ end: 10 }).reduce(Reducer.sum.dropInput(9))
// => 19

filterInput

Returns a Reducer instance that only passes values to the reducer that satisy the given pred predicate.

Definitions

filterInput<IF extends I>(pred: (value: I, index: number, halt: () => void) => value is IF, options?: {
      negate?: false | undefined;
    }): Reducer<IF, O>;

filterInput<IF extends I>(pred: (value: I, index: number, halt: () => void) => value is IF, options: {
      negate: true;
    }): Reducer<Exclude<I, IF>, O>;

filterInput(pred: (value: I, index: number, halt: () => void) => boolean, options?: {
      negate?: boolean | undefined;
    }): Reducer<I, O>;

Type parameters

NameConstraintsDescription
IFI

Parameters

NameTypeDescription
pred(value: I, index: number, halt: () => void) => value is IFa function that returns true if the value should be passed to the reducer based on the following inputs:
- value: the current input value
- index: the current input index
- halt: function that, when called, ensures no more new values are passed to the reducer
options{
      negate?: false | undefined;
    }
(optional) an object containing the following properties:
- negate: (default: false) when true will invert the given predicate
note

if the predicate is a type guard, the return type is automatically inferred

example
Reducer.sum.filterInput(v => v > 10)
// this reducer will only sum values larger than 10

flatMapInput

Returns a Reducer instance that converts each output value from some source reducer into an arbitrary number of output values using given flatMapFun before passing them to this reducer.

Definition

flatMapInput<I2>(flatMapFun: (value: I2, index: number) => StreamSource<I>): Reducer<I2, O>;

Type parameters

NameDescription
I2the resulting reducer input type

Parameters

NameTypeDescription
flatMapFun(value: I2, index: number) => StreamSource<I>
example
Reducer.sum.flatMapInput(v => [v, v + 1])
// this reducer will add v and v + 1 to the sum for each input value

mapOutput

Returns a Reducer instance that converts its output values using given mapFun.

Definition

mapOutput<O2>(mapFun: (value: O, index: number, halted: boolean) => O2): Reducer<I, O2>;

Type parameters

NameDescription
O2the new output type

Parameters

NameTypeDescription
mapFun(value: O, index: number, halted: boolean) => O2a function that takes the current output value and converts it to a new output value
example
Reducer.sum.mapOutput(String)
// this reducer will convert all its results to string before returning them

next

Returns the next state based on the given input values:

Definition

next(state: S, elem: I, index: number, halt: () => void): S;

Parameters

NameTypeDescription
stateSthe current state
elemIthe current input value
indexnumberthe current input index
halt() => voida function that, when called, ensures no more values are passed to the reducer

sliceInput

Returns a Reducer instance that takes given amount of elements starting at given from index, and ignores other elements.

Definition

sliceInput(from?: number, amount?: number): Reducer<I, O>;

Parameters

NameTypeDescription
fromnumber(default: 0) the index at which to start processing elements
amountnumber(optional) the amount of elements to process, if not given, processes all elements from the from index
example
Stream.range({ end: 10 }).reduce(Reducer.sum.sliceInput(1, 2))
// => 3

stateToResult

Returns the output value based on the given state.

Definition

stateToResult(state: S, index: number, halted: boolean): O;

Parameters

NameTypeDescription
stateSthe current state
indexnumberthe value index
haltedbooleana boolean indicating whether the reducer is halted

takeInput

Returns a Reducer instance that takes at most the given amount of input elements, and will ignore subsequent elements.

Definition

takeInput(amount: number): Reducer<I, O>;

Parameters

NameTypeDescription
amountnumberthe amount of elements to accept
example
Stream.range({ end: 10 }).reduce(Reducer.sum.takeInput(2))
// => 1

takeOutput

Returns an 'AsyncReducerinstance that produces at mostamount` values.

Definition

takeOutput(amount: number): Reducer<I, O>;

Parameters

NameTypeDescription
amountnumberthe maximum amount of values to produce.

takeOutputUntil

Returns a 'Reducerinstance that produces until the givenpred` predicate returns true for the output value.

Definition

takeOutputUntil(pred: (value: O, index: number) => boolean, options?: {
      negate?: boolean;
    }): Reducer<I, O>;

Parameters

NameTypeDescription
pred(value: O, index: number) => booleana function that returns true if the value should be passed to the reducer based on the following inputs:
- value: the current input value
- index: the current input index
- halt: function that, when called, ensures no more new values are passed to the reducer
options{
      negate?: boolean;
    }
(optional) an object containing the following properties:
- negate: (default: false) when true will invert the given predicate