Skip to main content
Module

x/rimbu/mod.ts>Reducer.Impl

Rimbu is a TypeScript library focused on immutable, performant, and type-safe collections and other tools.
Go to Latest
interface Reducer.Impl
import { type Reducer } from "https://deno.land/x/rimbu@1.1.0/mod.ts";
const { Impl } = Reducer;

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

Type Parameters

I
O
optional
S = unknown

Properties

readonly
init: () => S

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

Methods

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

Returns the next state based on the given input values:

stateToResult(state: S): O

Returns the output value based on the given state.

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

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

pipe<O1>(nextReducer1: Reducer<O, O1>): Reducer<I, O1>

Returns a Reducer instance that first applies this reducer, and then applies the given next reducer to each output produced by the previous reducer.

pipe<O1, O2>(nextReducer1: Reducer<O, O1>, nextReducer2: Reducer<O1, O2>): Reducer<I, O2>
pipe<O1, O2, O3>(
nextReducer1: Reducer<O, O1>,
nextReducer2: Reducer<O1, O2>,
nextReducer3: Reducer<O2, O3>,
): Reducer<I, O3>
pipe<O1, O2, O3, O4>(
nextReducer1: Reducer<O, O1>,
nextReducer2: Reducer<O1, O2>,
nextReducer3: Reducer<O2, O3>,
nextReducer4: Reducer<O3, O4>,
): Reducer<I, O4>
pipe<O1, O2, O3, O4, O5>(
nextReducer1: Reducer<O, O1>,
nextReducer2: Reducer<O1, O2>,
nextReducer3: Reducer<O2, O3>,
nextReducer4: Reducer<O3, O4>,
nextReducer5: Reducer<O4, O5>,
): Reducer<I, O5>
chain<O1>(nextReducer1: OptLazy<Reducer<I, O1>, [O]>): Reducer<I, O1>

Returns a reducer that applies the given nextReducers sequentially after this reducer has halted, and moving on to the next provided reducer until it is halted. Optionally, it provides the last output value of the previous reducer.

chain<O1, O2>(nextReducer1: OptLazy<Reducer<I, O1>, [O]>, nextReducer2: OptLazy<Reducer<I, O2>, [O1]>): Reducer<I, O2>
chain<O1, O2, O3>(
nextReducer1: OptLazy<Reducer<I, O1>, [O]>,
nextReducer2: OptLazy<Reducer<I, O2>, [O1]>,
nextReducer3: OptLazy<Reducer<I, O3>, [O2]>,
): Reducer<I, O3>
chain<O1, O2, O3, O4>(
nextReducer1: OptLazy<Reducer<I, O1>, [O]>,
nextReducer2: OptLazy<Reducer<I, O2>, [O1]>,
nextReducer3: OptLazy<Reducer<I, O3>, [O2]>,
nextReducer4: OptLazy<Reducer<I, O4>, [O3]>,
): Reducer<I, O4>
chain<O1, O2, O3, O4, O5>(
nextReducer1: OptLazy<Reducer<I, O1>, [O]>,
nextReducer2: OptLazy<Reducer<I, O2>, [O1]>,
nextReducer3: OptLazy<Reducer<I, O3>, [O2]>,
nextReducer4: OptLazy<Reducer<I, O4>, [O3]>,
nextReducer5: OptLazy<Reducer<I, O5>, [O4]>,
): Reducer<I, O5>
compile(): Reducer.Instance<I, O>

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.