<K, V>(entries: Iterable<readonly [K, V]>): MutableHashMap<K, V>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 iterable
const fromMap = MutableHashMap.fromIterable(new Map([["x", 10], ["y", 20]]))
console.log(MutableHashMap.get(fromMap, "x")) // Some(10)export const const fromIterable: <K, V>(
entries: Iterable<readonly [K, V]>
) => MutableHashMap<K, V>
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 iterable
const fromMap = MutableHashMap.fromIterable(new Map([["x", 10], ["y", 20]]))
console.log(MutableHashMap.get(fromMap, "x")) // Some(10)
fromIterable = <function (type parameter) K in <K, V>(entries: Iterable<readonly [K, V]>): MutableHashMap<K, V>K, function (type parameter) V in <K, V>(entries: Iterable<readonly [K, V]>): MutableHashMap<K, V>V>(entries: Iterable<readonly [K, V]>entries: interface Iterable<T, TReturn = any, TNext = any>Iterable<readonly [function (type parameter) K in <K, V>(entries: Iterable<readonly [K, V]>): MutableHashMap<K, V>K, function (type parameter) V in <K, V>(entries: Iterable<readonly [K, V]>): MutableHashMap<K, V>V]>): interface MutableHashMap<out K, out V>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 values
const map: MutableHashMap.MutableHashMap<string, number> = MutableHashMap
.empty()
// Add some data
MutableHashMap.set(map, "count", 42)
MutableHashMap.set(map, "total", 100)
// Use as iterable
for (const [key, value] of map) {
console.log(`${key}: ${value}`)
}
// Output:
// count: 42
// total: 100
// Convert to array
const entries = Array.from(map)
console.log(entries) // [["count", 42], ["total", 100]]
MutableHashMap<function (type parameter) K in <K, V>(entries: Iterable<readonly [K, V]>): MutableHashMap<K, V>K, function (type parameter) V in <K, V>(entries: Iterable<readonly [K, V]>): MutableHashMap<K, V>V> => {
const const self: MutableHashMap<K, V>const self: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self = const empty: <K, V>() => MutableHashMap<
K,
V
>
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 entries
MutableHashMap.set(map, "key1", 42)
MutableHashMap.set(map, "key2", 100)
console.log(MutableHashMap.size(map)) // 2
empty<function (type parameter) K in <K, V>(entries: Iterable<readonly [K, V]>): MutableHashMap<K, V>K, function (type parameter) V in <K, V>(entries: Iterable<readonly [K, V]>): MutableHashMap<K, V>V>()
for (const [const key: Kkey, const value: Vvalue] of entries: Iterable<readonly [K, V]>entries) {
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>
}
set(const self: MutableHashMap<K, V>const self: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self, const key: Kkey, const value: Vvalue)
}
return const self: MutableHashMap<K, V>const self: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self
}