Hyperlinkv0.8.0-beta.28

TxHashSet

TxHashSet.mapconsteffect/TxHashSet.ts:765
<V, U>(f: (value: V) => U): (
  self: TxHashSet<V>
) => Effect.Effect<TxHashSet<U>>
<V, U>(self: TxHashSet<V>, f: (value: V) => U): Effect.Effect<
  TxHashSet<U>
>

Maps each value in the TxHashSet using the provided function, returning a new TxHashSet.

Example (Mapping values)

import { Effect, TxHashSet } from "effect"

const program = Effect.gen(function*() {
  const numbers = yield* TxHashSet.make(1, 2, 3)
  const doubled = yield* TxHashSet.map(numbers, (n) => n * 2)

  const values = yield* TxHashSet.toHashSet(doubled)
  console.log(Array.from(values).sort()) // [2, 4, 6]
  console.log(yield* TxHashSet.size(doubled)) // 3

  // Mapping can reduce size if function produces duplicates
  const strings = yield* TxHashSet.make("apple", "banana", "cherry")
  const lengths = yield* TxHashSet.map(strings, (s) => s.length)
  const lengthValues = yield* TxHashSet.toHashSet(lengths)
  console.log(Array.from(lengthValues).sort()) // [5, 6] (apple=5, banana=6, cherry=6)
})
mapping
export const map: {
  <V, U>(f: (value: V) => U): (self: TxHashSet<V>) => Effect.Effect<TxHashSet<U>>
  <V, U>(self: TxHashSet<V>, f: (value: V) => U): Effect.Effect<TxHashSet<U>>
} = dual<
  <V, U>(f: (value: V) => U) => (self: TxHashSet<V>) => Effect.Effect<TxHashSet<U>>,
  <V, U>(self: TxHashSet<V>, f: (value: V) => U) => Effect.Effect<TxHashSet<U>>
>(2, <V, U>(self: TxHashSet<V>, f: (value: V) => U) =>
  Effect.gen(function*() {
    const currentSet = yield* TxRef.get(self.ref)
    const mappedSet = HashSet.map(currentSet, f)
    return yield* fromHashSet(mappedSet)
  }).pipe(Effect.tx))