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

x/rimbu/core/main/index.ts>Reducer.createMono

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

Returns a Reducer of which the input, state, and output types are the same.

Examples

Example 1

const sum = Reducer
  .createMono(
    0,
    (current, value) => current + value
  )
const result = Stream.of(1, 2, 3, 2, 1)).reduce(sum)
console.log+(result)
// => 9

Parameters

init: OptLazy<T>
  • the initial state value
next: (
current: T,
next: T,
index: number,
halt: () => void,
) => T
  • 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
optional
stateToResult: (state: T) => T
  • (optional) a function that converts the current state to an output value