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.
Latest
interface Reducer.Impl
import { type Reducer } from "https://deno.land/x/rimbu@1.2.1/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: (initHalt: () => void) => S

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

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.

Methods

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

Returns the next state based on the given input values:

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

Returns the output value based on the given state.

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

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

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>
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,
index: number,
halted: boolean,
) => 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.

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

Returns an 'AsyncReducerinstance that produces at mostamount` values.

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

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

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

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.

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.