MutableHashMap.ts
MutableHashMap.ts overview
Section titled “MutableHashMap.ts overview”Stores key/value entries in a mutable hash map.
MutableHashMap updates the same collection in place and supports fast
lookup, insertion, removal, clearing, and iteration. It combines a native
Map for ordinary JavaScript keys with hash buckets for keys that implement
Effect Equal and Hash, so callers can mix reference-based and structural
lookup in the same collection.
Since v2.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”constructors
Section titled “constructors”Creates an empty MutableHashMap.
When to use
Use to create a fresh mutable map before adding entries over time.
Details
Each call returns a new empty map instance.
Example (Creating an empty map)
import { MutableHashMap } from "effect"
const map = MutableHashMap.empty<string, number>()
// Add some entriesMutableHashMap.set(map, "key1", 42)MutableHashMap.set(map, "key2", 100)
console.log(MutableHashMap.size(map)) // 2See
makefor creating a map from explicit entriesfromIterablefor creating a map from an iterable of entries
Signature
declare const empty: <K, V>() => MutableHashMap<K, V>Since v2.0.0
fromIterable
Section titled “fromIterable”Creates a MutableHashMap from an iterable collection of key-value pairs.
When to use
Use to create a mutable hash map from an existing iterable of entries.
Example (Creating a map from an iterable)
import { MutableHashMap } from "effect"
const entries = [ ["apple", 1], ["banana", 2], ["cherry", 3]] as const
const map = MutableHashMap.fromIterable(entries)
console.log(MutableHashMap.get(map, "banana")) // Some(2)console.log(MutableHashMap.size(map)) // 3
// Works with any iterableconst fromMap = MutableHashMap.fromIterable( new Map([ ["x", 10], ["y", 20] ]))console.log(MutableHashMap.get(fromMap, "x")) // Some(10)See
makefor creating a map from explicit entriesemptyfor creating an empty map
Signature
declare const fromIterable: <K, V>(entries: Iterable<readonly [K, V]>) => MutableHashMap<K, V>Since v2.0.0
Creates a MutableHashMap from a variable number of key-value pairs.
When to use
Use to create a mutable hash map from explicit entries known at the call site.
Example (Creating a map from entries)
import { MutableHashMap } from "effect"
const map = MutableHashMap.make(["key1", 42], ["key2", 100], ["key3", 200])
console.log(MutableHashMap.get(map, "key1")) // Some(42)console.log(MutableHashMap.size(map)) // 3See
emptyfor creating an empty mapfromIterablefor creating a map from an iterable of entries
Signature
declare const make: <Entries extends Array<readonly [any, any]>>( ...entries: Entries) => MutableHashMap< Entries[number] extends readonly [infer K, any] ? K : never, Entries[number] extends readonly [any, infer V] ? V : never>Since v2.0.0
elements
Section titled “elements”Looks up a key in the MutableHashMap safely.
When to use
Use to safely read a MutableHashMap value for a key as an Option.
Details
Returns Some(value) when an equal key is present and None when the key is
absent.
Example (Getting a value)
import { MutableHashMap } from "effect"
const map = MutableHashMap.make(["key1", 42], ["key2", 100])
console.log(MutableHashMap.get(map, "key1")) // Some(42)console.log(MutableHashMap.get(map, "key3")) // None
// Pipe-able versionconst getValue = MutableHashMap.get("key1")console.log(getValue(map)) // Some(42)See
hasfor checking only whether a key is presentsetfor inserting or replacing a value by key
Signature
declare const get: { <K>(key: K): <V>(self: MutableHashMap<K, V>) => Option.Option<V> <K, V>(self: MutableHashMap<K, V>, key: K): Option.Option<V>}Since v2.0.0
Checks whether the MutableHashMap contains the specified key.
When to use
Use to test whether a key is present in a MutableHashMap without reading
its value.
Example (Checking for a key)
import { MutableHashMap } from "effect"
const map = MutableHashMap.make(["key1", 42], ["key2", 100])
console.log(MutableHashMap.has(map, "key1")) // trueconsole.log(MutableHashMap.has(map, "key3")) // false
// Pipe-able versionconst hasKey = MutableHashMap.has("key1")console.log(hasKey(map)) // trueSee
getfor reading the value as anOption
Signature
declare const has: { <K>(key: K): <V>(self: MutableHashMap<K, V>) => boolean <K, V>(self: MutableHashMap<K, V>, key: K): boolean}Since v2.0.0
Returns an iterable over the keys in the MutableHashMap.
When to use
Use to iterate over the keys currently stored in a mutable hash map.
Example (Reading keys)
import { MutableHashMap } from "effect"
const map = MutableHashMap.make(["apple", 1], ["banana", 2], ["cherry", 3])
const allKeys = Array.from(MutableHashMap.keys(map))console.log(allKeys) // ["apple", "banana", "cherry"]
// Useful for iteration or validationconst hasRequiredKeys = allKeys.includes("apple") && allKeys.includes("banana")See
valuesfor iterating over stored valueshasfor checking one key without iterating
Signature
declare const keys: <K, V>(self: MutableHashMap<K, V>) => Iterable<K>Since v3.8.0
Returns the number of key-value pairs in the MutableHashMap.
When to use
Use to read how many entries are currently stored in the mutable hash map.
Example (Checking map size)
import { MutableHashMap } from "effect"
const map = MutableHashMap.empty<string, number>()console.log(MutableHashMap.size(map)) // 0
MutableHashMap.set(map, "key1", 42)MutableHashMap.set(map, "key2", 100)console.log(MutableHashMap.size(map)) // 2
MutableHashMap.remove(map, "key1")console.log(MutableHashMap.size(map)) // 1
MutableHashMap.clear(map)console.log(MutableHashMap.size(map)) // 0See
isEmptyfor checking whether the map has no entries
Signature
declare const size: <K, V>(self: MutableHashMap<K, V>) => numberSince v2.0.0
values
Section titled “values”Returns an iterable over the values in the MutableHashMap.
When to use
Use to iterate over the values currently stored in a mutable hash map.
Example (Reading values)
import { MutableHashMap } from "effect"
const map = MutableHashMap.make(["apple", 1], ["banana", 2], ["cherry", 3])
const allValues = Array.from(MutableHashMap.values(map))console.log(allValues) // [1, 2, 3]
// Useful for calculationsconst total = allValues.reduce((sum, value) => sum + value, 0)console.log(total) // 6
// Filter valuesconst largeValues = allValues.filter((value) => value > 1)console.log(largeValues) // [2, 3]See
keysfor iterating over stored keys
Signature
declare const values: <K, V>(self: MutableHashMap<K, V>) => Iterable<V>Since v3.8.0
models
Section titled “models”MutableHashMap (interface)
Section titled “MutableHashMap (interface)”A mutable hash map that stores key-value pairs and supports both referential and Effect structural equality.
When to use
Use as a mutable key-value map when in-place updates are acceptable and keys may rely on Effect structural equality.
Details
Operations mutate the map in place. Keys that implement Equal / Hash can
be looked up structurally; other keys use normal JavaScript reference or
primitive equality.
Example (Using a mutable hash map)
import { MutableHashMap } from "effect"
// Create a mutable hash map with string keys and number valuesconst map: MutableHashMap.MutableHashMap<string, number> = MutableHashMap.empty()
// Add some dataMutableHashMap.set(map, "count", 42)MutableHashMap.set(map, "total", 100)
// Use as iterablefor (const [key, value] of map) { console.log(`${key}: ${value}`)}// Output:// count: 42// total: 100
// Convert to arrayconst entries = Array.from(map)console.log(entries) // [["count", 42], ["total", 100]]See
emptyfor creating an empty mutable hash mapgetfor reading values by keysetfor mutating entries by key
Signature
export interface MutableHashMap<out K, out V> extends Iterable<[K, V]>, Pipeable, Inspectable { readonly [TypeId]: typeof TypeId readonly backing: Map<K, V> readonly buckets: Map<number, NonEmptyArray<K>>}Since v2.0.0
mutations
Section titled “mutations”Removes all key-value pairs from the MutableHashMap, mutating the map in place. The map becomes empty after this operation.
When to use
Use to empty a mutable hash map while keeping the same map instance.
Example (Clearing all entries)
import { MutableHashMap } from "effect"
const map = MutableHashMap.make(["key1", 42], ["key2", 100], ["key3", 200])
console.log(MutableHashMap.size(map)) // 3
// Clear all entriesMutableHashMap.clear(map)
console.log(MutableHashMap.size(map)) // 0console.log(MutableHashMap.has(map, "key1")) // false
// Can still add new entries after clearingMutableHashMap.set(map, "new", 999)console.log(MutableHashMap.size(map)) // 1See
removefor deleting one keyemptyfor creating a fresh empty map
Signature
declare const clear: <K, V>(self: MutableHashMap<K, V>) => MutableHashMap<K, V>Since v2.0.0
modify
Section titled “modify”Updates the value of the specified key within the MutableHashMap if it exists. If the key doesn’t exist, the map remains unchanged.
When to use
Use to transform an existing MutableHashMap value in place without
inserting missing keys.
Example (Modifying existing values)
import { MutableHashMap } from "effect"
const map = MutableHashMap.make(["count", 5], ["total", 100])
// Increment existing valueMutableHashMap.modify(map, "count", (n) => n + 1)console.log(MutableHashMap.get(map, "count")) // Some(6)
// Double existing valueMutableHashMap.modify(map, "total", (n) => n * 2)console.log(MutableHashMap.get(map, "total")) // Some(200)
// Try to modify non-existent key (no effect)MutableHashMap.modify(map, "missing", (n) => n + 1)console.log(MutableHashMap.has(map, "missing")) // false
// Pipe-able versionconst increment = MutableHashMap.modify("count", (n: number) => n + 1)increment(map)See
setfor inserting or replacing a value directlymodifyAtfor handling both missing and existing keys
Signature
declare const modify: { <K, V>(key: K, f: (v: V) => V): (self: MutableHashMap<K, V>) => MutableHashMap<K, V> <K, V>(self: MutableHashMap<K, V>, key: K, f: (v: V) => V): MutableHashMap<K, V>}Since v2.0.0
modifyAt
Section titled “modifyAt”Updates or removes the specified key using a function from the current optional value to the next optional value.
When to use
Use to decide whether to insert, update, or remove a key based on its current optional value.
Example (Updating or removing a key)
import { MutableHashMap, Option } from "effect"
const map = MutableHashMap.make(["count", 5])
// Update existing keyMutableHashMap.modifyAt(map, "count", (option) => Option.map(option, (n) => n * 2))console.log(MutableHashMap.get(map, "count")) // Some(10)
// Add new keyMutableHashMap.modifyAt(map, "new", (option) => (Option.isNone(option) ? Option.some(42) : option))console.log(MutableHashMap.get(map, "new")) // Some(42)
// Remove key by returning NoneMutableHashMap.modifyAt(map, "count", () => Option.none())console.log(MutableHashMap.has(map, "count")) // false
// Conditional updateMutableHashMap.modifyAt( map, "new", (option) => Option.filter(option, (n) => n > 50) // Remove if <= 50)console.log(MutableHashMap.has(map, "new")) // false (42 <= 50)See
modifyfor updating only when the key already existssetfor inserting or replacing directlyremovefor deleting directly
Signature
declare const modifyAt: { <K, V>(key: K, f: (value: Option.Option<V>) => Option.Option<V>): (self: MutableHashMap<K, V>) => MutableHashMap<K, V> <K, V>(self: MutableHashMap<K, V>, key: K, f: (value: Option.Option<V>) => Option.Option<V>): MutableHashMap<K, V>}Since v2.0.0
remove
Section titled “remove”Removes the specified key from the MutableHashMap, mutating the map in place. If the key doesn’t exist, the map remains unchanged.
When to use
Use to delete one key from a mutable hash map in place.
Example (Removing a key)
import { MutableHashMap } from "effect"
const map = MutableHashMap.make(["key1", 42], ["key2", 100], ["key3", 200])
console.log(MutableHashMap.size(map)) // 3
// Remove existing keyMutableHashMap.remove(map, "key2")console.log(MutableHashMap.size(map)) // 2console.log(MutableHashMap.has(map, "key2")) // false
// Remove non-existent key (no effect)MutableHashMap.remove(map, "nonexistent")console.log(MutableHashMap.size(map)) // 2
// Pipe-able versionconst removeKey = MutableHashMap.remove("key1")removeKey(map)console.log(MutableHashMap.size(map)) // 1See
clearfor removing all entriesmodifyAtfor conditionally removing based on the current value
Signature
declare const remove: { <K>(key: K): <V>(self: MutableHashMap<K, V>) => MutableHashMap<K, V> <K, V>(self: MutableHashMap<K, V>, key: K): MutableHashMap<K, V>}Since v2.0.0
Sets a key-value pair in the MutableHashMap, mutating the map in place. If the key already exists, its value is updated.
When to use
Use to insert a new MutableHashMap entry or replace an existing entry in
place.
Example (Setting key-value pairs)
import { MutableHashMap } from "effect"
const map = MutableHashMap.empty<string, number>()
// Add new entriesMutableHashMap.set(map, "key1", 42)MutableHashMap.set(map, "key2", 100)
console.log(MutableHashMap.get(map, "key1")) // Some(42)console.log(MutableHashMap.size(map)) // 2
// Update existing entryMutableHashMap.set(map, "key1", 999)console.log(MutableHashMap.get(map, "key1")) // Some(999)
// Pipe-able versionconst setKey = MutableHashMap.set("key3", 300)setKey(map)console.log(MutableHashMap.size(map)) // 3See
modifyfor updating an existing value with a functionmodifyAtfor setting or removing based on the current optional valueremovefor deleting an entry by key
Signature
declare const set: { <K, V>(key: K, value: V): (self: MutableHashMap<K, V>) => MutableHashMap<K, V> <K, V>(self: MutableHashMap<K, V>, key: K, value: V): MutableHashMap<K, V>}Since v2.0.0
predicates
Section titled “predicates”isEmpty
Section titled “isEmpty”Returns true when the MutableHashMap contains no key-value pairs.
When to use
Use to branch on whether a mutable map currently has any entries.
See
sizefor reading the exact number of entries
Signature
declare const isEmpty: <K, V>(self: MutableHashMap<K, V>) => booleanSince v2.0.0
refinements
Section titled “refinements”isMutableHashMap
Section titled “isMutableHashMap”Checks whether the specified value is a MutableHashMap, false otherwise.
When to use
Use to narrow an unknown value before treating it as a mutable hash map.
Details
The check looks for the MutableHashMap runtime marker.
Gotchas
The check does not validate the key or value types carried by the map.
See
MutableHashMapfor the mutable hash map interface
Signature
declare const isMutableHashMap: <K, V>(value: unknown) => value is MutableHashMap<K, V>Since v4.0.0
traversing
Section titled “traversing”forEach
Section titled “forEach”Runs a callback for each key-value pair in the MutableHashMap.
When to use
Use to run a synchronous side-effecting callback for every key-value pair in an existing mutable map.
Details
Iteration follows the backing map’s order. The callback receives the value
first and the key second, matching Map.prototype.forEach.
See
keysfor iterating only keysvaluesfor iterating only values
Signature
declare const forEach: { <K, V>(f: (value: V, key: K) => void): (self: MutableHashMap<K, V>) => void <K, V>(self: MutableHashMap<K, V>, f: (value: V, key: K) => void): void}Since v2.0.0