<K1 extends K, K>(key: K1, hash: number): <V>(
self: HashMap<K, V>
) => Option<V>
<K1 extends K, K, V>(
self: HashMap<K, V>,
key: K1,
hash: number
): Option<V>Looks up the value for the specified key in the HashMap safely using a custom hash.
Example (Looking up values with a hash)
import { Hash, HashMap } from "effect"
// Useful when implementing custom equality for complex keys
const userMap = HashMap.make(
["user123", { name: "Alice", role: "admin" }],
["user456", { name: "Bob", role: "user" }]
)
// Use precomputed hash for performance in hot paths
const userId = "user123"
const precomputedHash = Hash.string(userId)
// Lookup with custom hash (e.g., cached hash value)
const user = HashMap.getHash(userMap, userId, precomputedHash)
console.log(user) // Option.some({ name: "Alice", role: "admin" })
// This avoids recomputing the hash when you already have it
const notFound = HashMap.getHash(userMap, "user999", Hash.string("user999"))
console.log(notFound) // Option.none()export const const getHash: {
<K1 extends K, K>(key: K1, hash: number): <V>(
self: HashMap<K, V>
) => Option<V>
<K1 extends K, K, V>(
self: HashMap<K, V>,
key: K1,
hash: number
): Option<V>
}
Looks up the value for the specified key in the HashMap safely using a custom hash.
Example (Looking up values with a hash)
import { Hash, HashMap } from "effect"
// Useful when implementing custom equality for complex keys
const userMap = HashMap.make(
["user123", { name: "Alice", role: "admin" }],
["user456", { name: "Bob", role: "user" }]
)
// Use precomputed hash for performance in hot paths
const userId = "user123"
const precomputedHash = Hash.string(userId)
// Lookup with custom hash (e.g., cached hash value)
const user = HashMap.getHash(userMap, userId, precomputedHash)
console.log(user) // Option.some({ name: "Alice", role: "admin" })
// This avoids recomputing the hash when you already have it
const notFound = HashMap.getHash(userMap, "user999", Hash.string("user999"))
console.log(notFound) // Option.none()
getHash: {
<function (type parameter) K1 in <K1 extends K, K>(key: K1, hash: number): <V>(self: HashMap<K, V>) => Option<V>K1 extends function (type parameter) K in <K1 extends K, K>(key: K1, hash: number): <V>(self: HashMap<K, V>) => Option<V>K, function (type parameter) K in <K1 extends K, K>(key: K1, hash: number): <V>(self: HashMap<K, V>) => Option<V>K>(key: K1 extends Kkey: function (type parameter) K1 in <K1 extends K, K>(key: K1, hash: number): <V>(self: HashMap<K, V>) => Option<V>K1, hash: numberhash: number): <function (type parameter) V in <V>(self: HashMap<K, V>): Option<V>V>(self: HashMap<K, V>(parameter) self: {
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: interface HashMap<out Key, out Value>A HashMap is an immutable key-value data structure that provides efficient lookup,
insertion, and deletion operations. It uses a Hash Array Mapped Trie (HAMT) internally
for structural sharing and optimal performance.
Example (Using basic HashMap operations)
import { HashMap } from "effect"
// Create a HashMap
const map = HashMap.make(["a", 1], ["b", 2], ["c", 3])
// Access values
const valueA = HashMap.get(map, "a") // Option.some(1)
const valueD = HashMap.get(map, "d") // Option.none()
// Check if key exists
console.log(HashMap.has(map, "b")) // true
// Add/update values (returns new HashMap)
const updated = HashMap.set(map, "d", 4)
console.log(HashMap.size(updated)) // 4
The HashMap namespace contains type-level utilities and helper types
for working with HashMap instances.
Example (Extracting HashMap types)
import { HashMap } from "effect"
// Create a concrete HashMap for type extraction
const inventory = HashMap.make(
["laptop", { quantity: 5, price: 999 }],
["mouse", { quantity: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = HashMap.HashMap.Key<typeof inventory> // string
type Product = HashMap.HashMap.Value<typeof inventory> // { quantity: number, price: number }
type InventoryEntry = HashMap.HashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateInventory = (id: ProductId, product: Product) =>
HashMap.set(inventory, id, product)
const processEntry = ([id, product]: InventoryEntry) =>
`${id}: ${product.quantity} @ $${product.price}`
// Example of extracted types in action
const newProduct: Product = { quantity: 10, price: 199 }
const updatedInventory = updateInventory("tablet", newProduct)
HashMap<function (type parameter) K in <K1 extends K, K>(key: K1, hash: number): <V>(self: HashMap<K, V>) => Option<V>K, function (type parameter) V in <V>(self: HashMap<K, V>): Option<V>V>) => type Option<A> = None<A> | Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) V in <V>(self: HashMap<K, V>): Option<V>V>
<function (type parameter) K1 in <K1 extends K, K, V>(self: HashMap<K, V>, key: K1, hash: number): Option<V>K1 extends function (type parameter) K in <K1 extends K, K, V>(self: HashMap<K, V>, key: K1, hash: number): Option<V>K, function (type parameter) K in <K1 extends K, K, V>(self: HashMap<K, V>, key: K1, hash: number): Option<V>K, function (type parameter) V in <K1 extends K, K, V>(self: HashMap<K, V>, key: K1, hash: number): Option<V>V>(self: HashMap<K, V>(parameter) self: {
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: interface HashMap<out Key, out Value>A HashMap is an immutable key-value data structure that provides efficient lookup,
insertion, and deletion operations. It uses a Hash Array Mapped Trie (HAMT) internally
for structural sharing and optimal performance.
Example (Using basic HashMap operations)
import { HashMap } from "effect"
// Create a HashMap
const map = HashMap.make(["a", 1], ["b", 2], ["c", 3])
// Access values
const valueA = HashMap.get(map, "a") // Option.some(1)
const valueD = HashMap.get(map, "d") // Option.none()
// Check if key exists
console.log(HashMap.has(map, "b")) // true
// Add/update values (returns new HashMap)
const updated = HashMap.set(map, "d", 4)
console.log(HashMap.size(updated)) // 4
The HashMap namespace contains type-level utilities and helper types
for working with HashMap instances.
Example (Extracting HashMap types)
import { HashMap } from "effect"
// Create a concrete HashMap for type extraction
const inventory = HashMap.make(
["laptop", { quantity: 5, price: 999 }],
["mouse", { quantity: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = HashMap.HashMap.Key<typeof inventory> // string
type Product = HashMap.HashMap.Value<typeof inventory> // { quantity: number, price: number }
type InventoryEntry = HashMap.HashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateInventory = (id: ProductId, product: Product) =>
HashMap.set(inventory, id, product)
const processEntry = ([id, product]: InventoryEntry) =>
`${id}: ${product.quantity} @ $${product.price}`
// Example of extracted types in action
const newProduct: Product = { quantity: 10, price: 199 }
const updatedInventory = updateInventory("tablet", newProduct)
HashMap<function (type parameter) K in <K1 extends K, K, V>(self: HashMap<K, V>, key: K1, hash: number): Option<V>K, function (type parameter) V in <K1 extends K, K, V>(self: HashMap<K, V>, key: K1, hash: number): Option<V>V>, key: K1 extends Kkey: function (type parameter) K1 in <K1 extends K, K, V>(self: HashMap<K, V>, key: K1, hash: number): Option<V>K1, hash: numberhash: number): type Option<A> = None<A> | Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) V in <K1 extends K, K, V>(self: HashMap<K, V>, key: K1, hash: number): Option<V>V>
} = import internalinternal.const getHash: anygetHash