function / @typed/fx

Sink.reduce

Reduces values into a single result using a pure function. Pass a Ref<B> (e.g. from Ref.make(initial)); after running, read the result with Ref.get(ref).

Package version
2.0.0-beta.13
Category
Collecting values
Since
1.0.0
Member of
Sink

Import

import { Sink } from "@typed/fx";

Access Sink.reduce through the imported Sink export. Its declaration below describes the member.

This public exposure is a re-export. Its import path is supported; declaration documentation is shared with the other public exposures below.

Signatures

export declare function reduce<A, B, E>(ref: Ref.Ref<B>, f: (b: B, a: A) => B): Sink<A, E, never>;

Why

An existing Ref provides an explicit, independently readable result location for consumer-side aggregation.

Ownership and lifetime

The caller owns the Ref. Each success returns a lazy atomic Ref.update; invoking onSuccess does not update immediately. If the producer executes callback Effects serially, values reduce in that execution order. Concurrent execution is still atomic but may be scheduled in a different order than callback invocation. Failures are ignored and a thrown reducer exception is a defect.

Examples

import { Effect, Ref } from "effect"
import * as Sink from "@typed/fx/Sink"
const program = Effect.gen(function* () {
  const total = yield* Ref.make(0)
  return Sink.reduce(total, (sum, n: number) => sum + n)
})

Other public imports

These import paths expose the same declaration. Each page retains its own public name and signature.

Source