MutableHashMap
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.
Constructors
Signature
declare function empty<K, V>(): MutableHashMap<K, V>;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.
See
Signature
declare function fromIterable<K, V>(entries: Iterable<readonly [K, V]>): MutableHashMap<K, V>;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.
See
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
>;Getters
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.
See
Signature
declare const get: {
<K>(key: K): <V>(self: MutableHashMap<K, V>) => Option<V>;
<K, V>(self: MutableHashMap<K, V>, key: K): Option<V>;
};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.
See
Signature
declare function keys<K, V>(self: MutableHashMap<K, V>): Iterable<K>;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.
See
isEmptyfor checking whether the map has no entries
Signature
declare function size<K, V>(self: MutableHashMap<K, V>): number;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.
See
keysfor iterating over stored keys
Signature
declare function values<K, V>(self: MutableHashMap<K, V>): Iterable<V>;Guards
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 function isMutableHashMap<K, V>(value: unknown): value is MutableHashMap<K, V>;Models
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.
See
Signature
interface MutableHashMap<out K, out V> extends Iterable<[K, V]>, Pipeable, Inspectable {
readonly "~effect/collections/MutableHashMap": "~effect/collections/MutableHashMap";
readonly backing: Map<K, V>;
readonly buckets: Map<number, [K, ...Array<K>]>;
}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.
See
Signature
declare function clear<K, V>(self: MutableHashMap<K, V>): MutableHashMap<K, V>;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.
See
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>;
};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.
See
Signature
declare const modifyAt: {
<K, V>(
key: K,
f: (value: Option<V>) => Option<V>,
): (self: MutableHashMap<K, V>) => MutableHashMap<K, V>;
<K, V>(
self: MutableHashMap<K, V>,
key: K,
f: (value: Option<V>) => Option<V>,
): MutableHashMap<K, V>;
};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.
See
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>;
};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.
See
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>;
};Predicates
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.
See
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;
};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 function isEmpty<K, V>(self: MutableHashMap<K, V>): boolean;Traversing
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
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;
};
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.
See
makefor creating a map from explicit entriesfromIterablefor creating a map from an iterable of entries