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
Name | Default | Description |
---|---|---|
I | the input value type | |
O | the output value type | |
S | unknown | the internal state type |
Properties
init
A function that produces the initial state value for the reducer algorithm.
init
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.
mapInput
Reducer
instance that converts its input values using given mapFun
before passing them to this reducer.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.
chain
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
Name | Constraints | Description |
---|---|---|
O2 | O |
Parameters
Name | Type | Description |
---|---|---|
nextReducers | StreamSource < OptLazy < Reducer <I, O2>, [O2]>> | an number of reducers consuming and producing the same types as the current reducer. |
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.
collectInput
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
Name | Description |
---|---|
I2 | the resulting reducer input type |
Parameters
Name | Type | Description |
---|---|---|
collectFun | CollectFun<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 |
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.
compile
Definition
compile():
Reducer.Instance
<I, O>;
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.
dropInput
Reducer
instance that skips the first given amount
of input elements, and will process subsequent elements.filterInput
Returns a Reducer
instance that only passes values to the reducer that satisy the given pred
predicate.
filterInput
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
Name | Constraints | Description |
---|---|---|
IF | I |
Parameters
Name | Type | Description |
---|---|---|
pred | (value: I, index: number, halt: () => void) => value is IF | a 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 |
if the predicate is a type guard, the return type is automatically inferred
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.
flatMapInput
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
Name | Description |
---|---|
I2 | the resulting reducer input type |
Parameters
Name | Type | Description |
---|---|---|
flatMapFun | (value: I2, index: number) => StreamSource <I> |
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
.
mapOutput
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
Name | Description |
---|---|
O2 | the new output type |
Parameters
Name | Type | Description |
---|---|---|
mapFun | (value: O, index: number, halted: boolean) => O2 | a function that takes the current output value and converts it to a new output value |
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:
next
sliceInput
Returns a Reducer
instance that takes given amount
of elements starting at given from
index, and ignores other elements.
sliceInput
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
Name | Type | Description |
---|---|---|
from | number | (default: 0) the index at which to start processing elements |
amount | number | (optional) the amount of elements to process, if not given, processes all elements from the from index |
Stream.range({ end: 10 }).reduce(Reducer.sum.sliceInput(1, 2))
// => 3
stateToResult
Returns the output value based on the given state
.
stateToResult
state
.takeInput
Returns a Reducer
instance that takes at most the given amount
of input elements, and will ignore subsequent elements.
takeInput
Reducer
instance that takes at most the given amount
of input elements, and will ignore subsequent elements.takeOutput
Returns an 'AsyncReducerinstance that produces at most
amount` values.
takeOutput
instance that produces at most
amount` values.takeOutputUntil
Returns a 'Reducerinstance that produces until the given
pred` predicate returns true for the output value.
takeOutputUntil
instance that produces until the given
pred` predicate returns true for the output value.Definition
takeOutputUntil(pred: (value: O, index: number) => boolean, options?: {
negate?: boolean;
}):
Reducer
<I, O>;
Parameters
Name | Type | Description |
---|---|---|
pred | (value: O, index: number) => boolean | a 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 |