Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/rimbu/common/reducer.ts>Reducer.create

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

Returns a Reducer with the given options:

Examples

Example 1

const evenNumberOfOnes = Reducer
  .create(
    true,
    (current, value: number) => value === 1 ? !current : current,
    state => state ? 'even' : 'not even')
const result = Stream.of(1, 2, 3, 2, 1)).reduce(evenNumberOfOnes)
console.log+(result)
// => 'even'

Type Parameters

I
optional
O = I
optional
S = O

Parameters

init: OptLazy<S>
  • the initial state value
next: (
current: S,
next: I,
index: number,
halt: () => void,
) => S
  • returns the next state value based on the given inputs:
  • current: the current state
  • next: the current input value
  • index: the input index value
  • halt: function that, when called, ensures no more elements are passed to the reducer
stateToResult: (state: S) => O
  • a function that converts the current state to an output value