Hyperlinkv0.8.0-beta.28

TxHashMap

TxHashMap.unionconsteffect/TxHashMap.ts:1034
<K1 extends K, K, V1 extends V, V>(other: HashMap.HashMap<K1, V1>): (
  self: TxHashMap<K, V>
) => Effect.Effect<void>
<K1 extends K, K, V1 extends V, V>(
  self: TxHashMap<K, V>,
  other: HashMap.HashMap<K1, V1>
): Effect.Effect<void>

Merges another HashMap into this TxHashMap. If both maps contain the same key, the value from the other map will be used.

Details

This function mutates the original TxHashMap by merging the provided HashMap into it. It does not return a new TxHashMap reference.

Example (Merging HashMaps)

import { Effect, HashMap, TxHashMap } from "effect"

const program = Effect.gen(function*() {
  // Create initial user preferences
  const userPrefs = yield* TxHashMap.make(
    ["theme", "light"],
    ["language", "en"],
    ["notifications", "enabled"]
  )

  // New preferences to merge in
  const newSettings = HashMap.make(
    ["theme", "dark"], // will override existing
    ["timezone", "UTC"], // new setting
    ["sound", "enabled"] // new setting
  )

  // Merge the new settings
  yield* TxHashMap.union(userPrefs, newSettings)

  // Check the merged result
  const theme = yield* TxHashMap.get(userPrefs, "theme")
  console.log(theme) // Option.some("dark") - overridden

  const language = yield* TxHashMap.get(userPrefs, "language")
  console.log(language) // Option.some("en") - preserved

  const timezone = yield* TxHashMap.get(userPrefs, "timezone")
  console.log(timezone) // Option.some("UTC") - newly added

  const size = yield* TxHashMap.size(userPrefs)
  console.log(size) // 5 total settings
})
combinators
export const union: {
  <K1 extends K, K, V1 extends V, V>(
    other: HashMap.HashMap<K1, V1>
  ): (self: TxHashMap<K, V>) => Effect.Effect<void>
  <K1 extends K, K, V1 extends V, V>(
    self: TxHashMap<K, V>,
    other: HashMap.HashMap<K1, V1>
  ): Effect.Effect<void>
} = dual(
  2,
  <K1 extends K, K, V1 extends V, V>(
    self: TxHashMap<K, V>,
    other: HashMap.HashMap<K1, V1>
  ): Effect.Effect<void> => TxRef.update(self.ref, (map) => HashMap.union(map, other))
)