Hyperlinkv0.8.0-beta.28

TxHashMap

TxHashMap.setconsteffect/TxHashMap.ts:426
<K, V>(key: K, value: V): (self: TxHashMap<K, V>) => Effect.Effect<void>
<K, V>(self: TxHashMap<K, V>, key: K, value: V): Effect.Effect<void>

Sets the value for the specified key in the TxHashMap.

Details

This function mutates the original TxHashMap by updating its internal state. It does not return a new TxHashMap reference.

Example (Setting values)

import { Effect, TxHashMap } from "effect"

const program = Effect.gen(function*() {
  const inventory = yield* TxHashMap.make(
    ["laptop", 5],
    ["mouse", 20]
  )

  // Update existing item
  yield* TxHashMap.set(inventory, "laptop", 3)
  const laptopStock = yield* TxHashMap.get(inventory, "laptop")
  console.log(laptopStock) // Option.some(3)

  // Add new item
  yield* TxHashMap.set(inventory, "keyboard", 15)
  const keyboardStock = yield* TxHashMap.get(inventory, "keyboard")
  console.log(keyboardStock) // Option.some(15)

  // Use with pipe syntax
  yield* TxHashMap.set("tablet", 8)(inventory)
})
combinators
export const set: {
  <K, V>(key: K, value: V): (self: TxHashMap<K, V>) => Effect.Effect<void>
  <K, V>(self: TxHashMap<K, V>, key: K, value: V): Effect.Effect<void>
} = dual(
  3,
  <K, V>(self: TxHashMap<K, V>, key: K, value: V): Effect.Effect<void> =>
    TxRef.update(self.ref, (map) => HashMap.set(map, key, value))
)