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

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

Properties

init

The initial state value for the reducer algorithm.

Definition

readonly init: OptLazy<S>;

Methods

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

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.

Definition

filterInput(pred: (value: I, index: number, halt: () => void) => boolean): Reducer<I, O>;

Parameters

NameTypeDescription
pred(value: I, index: number, halt: () => void) => 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
example
Reducer.sum.filterInput(v => v > 10)
// this reducer will only sum values larger than 10

mapInput

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

Definition

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

Type parameters

NameDescription
I2the resulting reducer input type

Parameters

NameTypeDescription
mapFun(value: I2, index: number) => Ia function that returns a new value to pass to the reducer based on the following inputs:
- value: the current input value
- index: the current input index
example
Reducer.sum.mapInput(v => v * 2)
// this reducer will double all input values before summing them

mapOutput

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

Definition

mapOutput<O2>(mapFun: (value: O) => O2): Reducer<I, O2>;

Type parameters

NameDescription
O2the resulting reducer output type

Parameters

NameTypeDescription
mapFun(value: O) => 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): O;

Parameters

NameTypeDescription
stateSthe current state

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