Hyperlinkv0.8.0-beta.28

TxHashMap

TxHashMap.modifyAtconsteffect/TxHashMap.ts:799
<K, V>(key: K, f: (value: Option.Option<V>) => Option.Option<V>): (
  self: TxHashMap<K, V>
) => Effect.Effect<void>
<K, V>(
  self: TxHashMap<K, V>,
  key: K,
  f: (value: Option.Option<V>) => Option.Option<V>
): Effect.Effect<void>

Updates the value for the specified key using an Option-based update function.

Details

This function mutates the original TxHashMap by updating, adding, or removing the key-value pair based on the function result. It does not return a new TxHashMap reference.

Example (Updating values with Option)

import { Effect, TxHashMap } from "effect"

const program = Effect.gen(function*() {
  const storage = yield* TxHashMap.make<string, string | number>([
    "file1.txt",
    "content1"
  ], ["access_count", 0])

  // Increment existing counter
  yield* TxHashMap.modifyAt(storage, "access_count", (current) =>
    current._tag === "Some" && typeof current.value === "number"
      ? { ...current, value: current.value + 1 }
      : current
  )
  const count1 = yield* TxHashMap.get(storage, "access_count")
  console.log(count1) // Option.some(1)

  // Increment existing counter again
  yield* TxHashMap.modifyAt(storage, "access_count", (current) =>
    current._tag === "Some" && typeof current.value === "number"
      ? { ...current, value: current.value + 1 }
      : current
  )
  const count2 = yield* TxHashMap.get(storage, "access_count")
  console.log(count2) // Option.some(2)

  // Update an existing string entry
  yield* TxHashMap.modifyAt(storage, "file1.txt", (current) =>
    current._tag === "Some" && typeof current.value === "string"
      ? { ...current, value: `${current.value}.bak` }
      : current
  )
  const backup = yield* TxHashMap.get(storage, "file1.txt")
  console.log(backup) // Option.some("content1.bak")
})
combinators
export const modifyAt: {
  <K, V>(
    key: K,
    f: (value: Option.Option<V>) => Option.Option<V>
  ): (self: TxHashMap<K, V>) => Effect.Effect<void>
  <K, V>(
    self: TxHashMap<K, V>,
    key: K,
    f: (value: Option.Option<V>) => Option.Option<V>
  ): Effect.Effect<void>
} = dual(
  3,
  <K, V>(
    self: TxHashMap<K, V>,
    key: K,
    f: (value: Option.Option<V>) => Option.Option<V>
  ): Effect.Effect<void> =>
    Effect.gen(function*() {
      const currentMap = yield* TxRef.get(self.ref)
      const currentValue = HashMap.get(currentMap, key)
      const newValue = f(currentValue)

      if (Option.isSome(newValue)) {
        yield* TxRef.set(self.ref, HashMap.set(currentMap, key, newValue.value))
      } else if (Option.isSome(currentValue)) {
        yield* TxRef.set(self.ref, HashMap.remove(currentMap, key))
      }
    }).pipe(Effect.tx)
)