Hyperlinkv0.8.0-beta.28

TxHashSet

TxHashSet.reduceconsteffect/TxHashSet.ts:857
<V, U>(zero: U, f: (accumulator: U, value: V) => U): (
  self: TxHashSet<V>
) => Effect.Effect<U>
<V, U>(
  self: TxHashSet<V>,
  zero: U,
  f: (accumulator: U, value: V) => U
): Effect.Effect<U>

Reduces the TxHashSet to a single value by iterating through the values and applying an accumulator function.

Example (Reducing values)

import { Effect, TxHashSet } from "effect"

const program = Effect.gen(function*() {
  const numbers = yield* TxHashSet.make(1, 2, 3, 4, 5)
  const sum = yield* TxHashSet.reduce(numbers, 0, (acc, n) => acc + n)

  console.log(sum) // 15

  const strings = yield* TxHashSet.make("a", "b", "c")
  const concatenated = yield* TxHashSet.reduce(strings, "", (acc, s) => acc + s)
  console.log(concatenated) // Order may vary: "abc", "bac", etc.
})
folding
export const reduce: {
  <V, U>(
    zero: U,
    f: (accumulator: U, value: V) => U
  ): (self: TxHashSet<V>) => Effect.Effect<U>
  <V, U>(
    self: TxHashSet<V>,
    zero: U,
    f: (accumulator: U, value: V) => U
  ): Effect.Effect<U>
} = dual<
  <V, U>(
    zero: U,
    f: (accumulator: U, value: V) => U
  ) => (self: TxHashSet<V>) => Effect.Effect<U>,
  <V, U>(
    self: TxHashSet<V>,
    zero: U,
    f: (accumulator: U, value: V) => U
  ) => Effect.Effect<U>
>(3, <V, U>(self: TxHashSet<V>, zero: U, f: (accumulator: U, value: V) => U) =>
  Effect.gen(function*() {
    const set = yield* TxRef.get(self.ref)
    return HashSet.reduce(set, zero, f)
  }))