<A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (
self: TxHashMap<K, V>
) => Effect.Effect<TxHashMap<K, A>>
<K, V, A>(
self: TxHashMap<K, V>,
f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>
): Effect.Effect<TxHashMap<K, A>>Maps each entry effectfully to a TxHashMap and flattens the produced maps.
Details
This function returns a new TxHashMap reference with the flattened results. The original TxHashMap is not modified.
Example (Flat mapping entries)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a department-employee map
const departments = yield* TxHashMap.make(
["engineering", ["alice", "bob"]],
["marketing", ["charlie", "diana"]]
)
// Expand each department into individual employee entries with metadata
const employeeDetails = yield* TxHashMap.flatMap(
departments,
(employees, department) =>
Effect.gen(function*() {
const employeeMap = yield* TxHashMap.empty<
string,
{ department: string; role: string }
>()
for (let i = 0; i < employees.length; i++) {
const employee = employees[i]
const role = i === 0 ? "lead" : "member"
yield* TxHashMap.set(employeeMap, employee, { department, role })
}
return employeeMap
})
)
// Check the flattened result
const alice = yield* TxHashMap.get(employeeDetails, "alice")
console.log(alice) // Option.some({ department: "engineering", role: "lead" })
const charlie = yield* TxHashMap.get(employeeDetails, "charlie")
console.log(charlie) // Option.some({ department: "marketing", role: "lead" })
const size = yield* TxHashMap.size(employeeDetails)
console.log(size) // 4 (all employees)
})export const const flatMap: {
<A, V, K>(
f: (
value: V,
key: K
) => Effect.Effect<TxHashMap<K, A>>
): (
self: TxHashMap<K, V>
) => Effect.Effect<TxHashMap<K, A>>
<K, V, A>(
self: TxHashMap<K, V>,
f: (
value: V,
key: K
) => Effect.Effect<TxHashMap<K, A>>
): Effect.Effect<TxHashMap<K, A>>
}
Maps each entry effectfully to a TxHashMap and flattens the produced maps.
Details
This function returns a new TxHashMap reference with the flattened results.
The original TxHashMap is not modified.
Example (Flat mapping entries)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a department-employee map
const departments = yield* TxHashMap.make(
["engineering", ["alice", "bob"]],
["marketing", ["charlie", "diana"]]
)
// Expand each department into individual employee entries with metadata
const employeeDetails = yield* TxHashMap.flatMap(
departments,
(employees, department) =>
Effect.gen(function*() {
const employeeMap = yield* TxHashMap.empty<
string,
{ department: string; role: string }
>()
for (let i = 0; i < employees.length; i++) {
const employee = employees[i]
const role = i === 0 ? "lead" : "member"
yield* TxHashMap.set(employeeMap, employee, { department, role })
}
return employeeMap
})
)
// Check the flattened result
const alice = yield* TxHashMap.get(employeeDetails, "alice")
console.log(alice) // Option.some({ department: "engineering", role: "lead" })
const charlie = yield* TxHashMap.get(employeeDetails, "charlie")
console.log(charlie) // Option.some({ department: "marketing", role: "lead" })
const size = yield* TxHashMap.size(employeeDetails)
console.log(size) // 4 (all employees)
})
flatMap: {
<function (type parameter) A in <A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (self: TxHashMap<K, V>) => Effect.Effect<TxHashMap<K, A>>A, function (type parameter) V in <A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (self: TxHashMap<K, V>) => Effect.Effect<TxHashMap<K, A>>V, function (type parameter) K in <A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (self: TxHashMap<K, V>) => Effect.Effect<TxHashMap<K, A>>K>(
f: (
value: V,
key: K
) => Effect.Effect<TxHashMap<K, A>>
f: (value: Vvalue: function (type parameter) V in <A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (self: TxHashMap<K, V>) => Effect.Effect<TxHashMap<K, A>>V, key: Kkey: function (type parameter) K in <A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (self: TxHashMap<K, V>) => Effect.Effect<TxHashMap<K, A>>K) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface TxHashMap<in out K, in out V>A TxHashMap is a transactional hash map data structure that provides atomic operations
on key-value pairs within Effect transactions. It uses an immutable HashMap internally
with TxRef for transactional semantics, ensuring all operations are performed atomically.
Example (Using transactional hash maps)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional hash map
const txMap = yield* TxHashMap.make(["user1", "Alice"], ["user2", "Bob"])
// Single operations are automatically transactional
yield* TxHashMap.set(txMap, "user3", "Charlie")
const user = yield* TxHashMap.get(txMap, "user1")
console.log(user) // Option.some("Alice")
// Multi-step atomic operations
yield* Effect.tx(
Effect.gen(function*() {
const currentUser = yield* TxHashMap.get(txMap, "user1")
if (currentUser._tag === "Some") {
yield* TxHashMap.set(txMap, "user1", currentUser.value + "_updated")
yield* TxHashMap.remove(txMap, "user2")
}
})
)
const size = yield* TxHashMap.size(txMap)
console.log(size) // 2
})
The TxHashMap namespace contains type-level utilities and helper types
for working with TxHashMap instances.
Example (Reusing extracted TxHashMap types)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional inventory map
const inventory = yield* TxHashMap.make(
["laptop", { stock: 5, price: 999 }],
["mouse", { stock: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = TxHashMap.TxHashMap.Key<typeof inventory> // string
type Product = TxHashMap.TxHashMap.Value<typeof inventory> // { stock: number, price: number }
type InventoryEntry = TxHashMap.TxHashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateStock = (id: ProductId, newStock: number) =>
TxHashMap.modify(
inventory,
id,
(product) => ({ ...product, stock: newStock })
)
yield* updateStock("laptop", 3)
})
TxHashMap<function (type parameter) K in <A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (self: TxHashMap<K, V>) => Effect.Effect<TxHashMap<K, A>>K, function (type parameter) A in <A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (self: TxHashMap<K, V>) => Effect.Effect<TxHashMap<K, A>>A>>
): (self: TxHashMap<K, V>(parameter) self: {
ref: TxRef.TxRef<HashMap.HashMap<K, V>>;
toString: () => string;
toJSON: () => unknown;
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; <…;
}
self: interface TxHashMap<in out K, in out V>A TxHashMap is a transactional hash map data structure that provides atomic operations
on key-value pairs within Effect transactions. It uses an immutable HashMap internally
with TxRef for transactional semantics, ensuring all operations are performed atomically.
Example (Using transactional hash maps)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional hash map
const txMap = yield* TxHashMap.make(["user1", "Alice"], ["user2", "Bob"])
// Single operations are automatically transactional
yield* TxHashMap.set(txMap, "user3", "Charlie")
const user = yield* TxHashMap.get(txMap, "user1")
console.log(user) // Option.some("Alice")
// Multi-step atomic operations
yield* Effect.tx(
Effect.gen(function*() {
const currentUser = yield* TxHashMap.get(txMap, "user1")
if (currentUser._tag === "Some") {
yield* TxHashMap.set(txMap, "user1", currentUser.value + "_updated")
yield* TxHashMap.remove(txMap, "user2")
}
})
)
const size = yield* TxHashMap.size(txMap)
console.log(size) // 2
})
The TxHashMap namespace contains type-level utilities and helper types
for working with TxHashMap instances.
Example (Reusing extracted TxHashMap types)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional inventory map
const inventory = yield* TxHashMap.make(
["laptop", { stock: 5, price: 999 }],
["mouse", { stock: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = TxHashMap.TxHashMap.Key<typeof inventory> // string
type Product = TxHashMap.TxHashMap.Value<typeof inventory> // { stock: number, price: number }
type InventoryEntry = TxHashMap.TxHashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateStock = (id: ProductId, newStock: number) =>
TxHashMap.modify(
inventory,
id,
(product) => ({ ...product, stock: newStock })
)
yield* updateStock("laptop", 3)
})
TxHashMap<function (type parameter) K in <A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (self: TxHashMap<K, V>) => Effect.Effect<TxHashMap<K, A>>K, function (type parameter) V in <A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (self: TxHashMap<K, V>) => Effect.Effect<TxHashMap<K, A>>V>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface TxHashMap<in out K, in out V>A TxHashMap is a transactional hash map data structure that provides atomic operations
on key-value pairs within Effect transactions. It uses an immutable HashMap internally
with TxRef for transactional semantics, ensuring all operations are performed atomically.
Example (Using transactional hash maps)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional hash map
const txMap = yield* TxHashMap.make(["user1", "Alice"], ["user2", "Bob"])
// Single operations are automatically transactional
yield* TxHashMap.set(txMap, "user3", "Charlie")
const user = yield* TxHashMap.get(txMap, "user1")
console.log(user) // Option.some("Alice")
// Multi-step atomic operations
yield* Effect.tx(
Effect.gen(function*() {
const currentUser = yield* TxHashMap.get(txMap, "user1")
if (currentUser._tag === "Some") {
yield* TxHashMap.set(txMap, "user1", currentUser.value + "_updated")
yield* TxHashMap.remove(txMap, "user2")
}
})
)
const size = yield* TxHashMap.size(txMap)
console.log(size) // 2
})
The TxHashMap namespace contains type-level utilities and helper types
for working with TxHashMap instances.
Example (Reusing extracted TxHashMap types)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional inventory map
const inventory = yield* TxHashMap.make(
["laptop", { stock: 5, price: 999 }],
["mouse", { stock: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = TxHashMap.TxHashMap.Key<typeof inventory> // string
type Product = TxHashMap.TxHashMap.Value<typeof inventory> // { stock: number, price: number }
type InventoryEntry = TxHashMap.TxHashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateStock = (id: ProductId, newStock: number) =>
TxHashMap.modify(
inventory,
id,
(product) => ({ ...product, stock: newStock })
)
yield* updateStock("laptop", 3)
})
TxHashMap<function (type parameter) K in <A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (self: TxHashMap<K, V>) => Effect.Effect<TxHashMap<K, A>>K, function (type parameter) A in <A, V, K>(f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): (self: TxHashMap<K, V>) => Effect.Effect<TxHashMap<K, A>>A>>
<function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>K, function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>V, function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>A>(
self: TxHashMap<K, V>(parameter) self: {
ref: TxRef.TxRef<HashMap.HashMap<K, V>>;
toString: () => string;
toJSON: () => unknown;
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; <…;
}
self: interface TxHashMap<in out K, in out V>A TxHashMap is a transactional hash map data structure that provides atomic operations
on key-value pairs within Effect transactions. It uses an immutable HashMap internally
with TxRef for transactional semantics, ensuring all operations are performed atomically.
Example (Using transactional hash maps)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional hash map
const txMap = yield* TxHashMap.make(["user1", "Alice"], ["user2", "Bob"])
// Single operations are automatically transactional
yield* TxHashMap.set(txMap, "user3", "Charlie")
const user = yield* TxHashMap.get(txMap, "user1")
console.log(user) // Option.some("Alice")
// Multi-step atomic operations
yield* Effect.tx(
Effect.gen(function*() {
const currentUser = yield* TxHashMap.get(txMap, "user1")
if (currentUser._tag === "Some") {
yield* TxHashMap.set(txMap, "user1", currentUser.value + "_updated")
yield* TxHashMap.remove(txMap, "user2")
}
})
)
const size = yield* TxHashMap.size(txMap)
console.log(size) // 2
})
The TxHashMap namespace contains type-level utilities and helper types
for working with TxHashMap instances.
Example (Reusing extracted TxHashMap types)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional inventory map
const inventory = yield* TxHashMap.make(
["laptop", { stock: 5, price: 999 }],
["mouse", { stock: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = TxHashMap.TxHashMap.Key<typeof inventory> // string
type Product = TxHashMap.TxHashMap.Value<typeof inventory> // { stock: number, price: number }
type InventoryEntry = TxHashMap.TxHashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateStock = (id: ProductId, newStock: number) =>
TxHashMap.modify(
inventory,
id,
(product) => ({ ...product, stock: newStock })
)
yield* updateStock("laptop", 3)
})
TxHashMap<function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>K, function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>V>,
f: (
value: V,
key: K
) => Effect.Effect<TxHashMap<K, A>>
f: (value: Vvalue: function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>V, key: Kkey: function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>K) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface TxHashMap<in out K, in out V>A TxHashMap is a transactional hash map data structure that provides atomic operations
on key-value pairs within Effect transactions. It uses an immutable HashMap internally
with TxRef for transactional semantics, ensuring all operations are performed atomically.
Example (Using transactional hash maps)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional hash map
const txMap = yield* TxHashMap.make(["user1", "Alice"], ["user2", "Bob"])
// Single operations are automatically transactional
yield* TxHashMap.set(txMap, "user3", "Charlie")
const user = yield* TxHashMap.get(txMap, "user1")
console.log(user) // Option.some("Alice")
// Multi-step atomic operations
yield* Effect.tx(
Effect.gen(function*() {
const currentUser = yield* TxHashMap.get(txMap, "user1")
if (currentUser._tag === "Some") {
yield* TxHashMap.set(txMap, "user1", currentUser.value + "_updated")
yield* TxHashMap.remove(txMap, "user2")
}
})
)
const size = yield* TxHashMap.size(txMap)
console.log(size) // 2
})
The TxHashMap namespace contains type-level utilities and helper types
for working with TxHashMap instances.
Example (Reusing extracted TxHashMap types)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional inventory map
const inventory = yield* TxHashMap.make(
["laptop", { stock: 5, price: 999 }],
["mouse", { stock: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = TxHashMap.TxHashMap.Key<typeof inventory> // string
type Product = TxHashMap.TxHashMap.Value<typeof inventory> // { stock: number, price: number }
type InventoryEntry = TxHashMap.TxHashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateStock = (id: ProductId, newStock: number) =>
TxHashMap.modify(
inventory,
id,
(product) => ({ ...product, stock: newStock })
)
yield* updateStock("laptop", 3)
})
TxHashMap<function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>K, function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>A>>
): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface TxHashMap<in out K, in out V>A TxHashMap is a transactional hash map data structure that provides atomic operations
on key-value pairs within Effect transactions. It uses an immutable HashMap internally
with TxRef for transactional semantics, ensuring all operations are performed atomically.
Example (Using transactional hash maps)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional hash map
const txMap = yield* TxHashMap.make(["user1", "Alice"], ["user2", "Bob"])
// Single operations are automatically transactional
yield* TxHashMap.set(txMap, "user3", "Charlie")
const user = yield* TxHashMap.get(txMap, "user1")
console.log(user) // Option.some("Alice")
// Multi-step atomic operations
yield* Effect.tx(
Effect.gen(function*() {
const currentUser = yield* TxHashMap.get(txMap, "user1")
if (currentUser._tag === "Some") {
yield* TxHashMap.set(txMap, "user1", currentUser.value + "_updated")
yield* TxHashMap.remove(txMap, "user2")
}
})
)
const size = yield* TxHashMap.size(txMap)
console.log(size) // 2
})
The TxHashMap namespace contains type-level utilities and helper types
for working with TxHashMap instances.
Example (Reusing extracted TxHashMap types)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional inventory map
const inventory = yield* TxHashMap.make(
["laptop", { stock: 5, price: 999 }],
["mouse", { stock: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = TxHashMap.TxHashMap.Key<typeof inventory> // string
type Product = TxHashMap.TxHashMap.Value<typeof inventory> // { stock: number, price: number }
type InventoryEntry = TxHashMap.TxHashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateStock = (id: ProductId, newStock: number) =>
TxHashMap.modify(
inventory,
id,
(product) => ({ ...product, stock: newStock })
)
yield* updateStock("laptop", 3)
})
TxHashMap<function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>K, function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>A>>
} = import dualdual(
2,
<function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>K, function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>V, function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>A>(
self: TxHashMap<K, V>(parameter) self: {
ref: TxRef.TxRef<HashMap.HashMap<K, V>>;
toString: () => string;
toJSON: () => unknown;
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; <…;
}
self: interface TxHashMap<in out K, in out V>A TxHashMap is a transactional hash map data structure that provides atomic operations
on key-value pairs within Effect transactions. It uses an immutable HashMap internally
with TxRef for transactional semantics, ensuring all operations are performed atomically.
Example (Using transactional hash maps)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional hash map
const txMap = yield* TxHashMap.make(["user1", "Alice"], ["user2", "Bob"])
// Single operations are automatically transactional
yield* TxHashMap.set(txMap, "user3", "Charlie")
const user = yield* TxHashMap.get(txMap, "user1")
console.log(user) // Option.some("Alice")
// Multi-step atomic operations
yield* Effect.tx(
Effect.gen(function*() {
const currentUser = yield* TxHashMap.get(txMap, "user1")
if (currentUser._tag === "Some") {
yield* TxHashMap.set(txMap, "user1", currentUser.value + "_updated")
yield* TxHashMap.remove(txMap, "user2")
}
})
)
const size = yield* TxHashMap.size(txMap)
console.log(size) // 2
})
The TxHashMap namespace contains type-level utilities and helper types
for working with TxHashMap instances.
Example (Reusing extracted TxHashMap types)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional inventory map
const inventory = yield* TxHashMap.make(
["laptop", { stock: 5, price: 999 }],
["mouse", { stock: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = TxHashMap.TxHashMap.Key<typeof inventory> // string
type Product = TxHashMap.TxHashMap.Value<typeof inventory> // { stock: number, price: number }
type InventoryEntry = TxHashMap.TxHashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateStock = (id: ProductId, newStock: number) =>
TxHashMap.modify(
inventory,
id,
(product) => ({ ...product, stock: newStock })
)
yield* updateStock("laptop", 3)
})
TxHashMap<function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>K, function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>V>,
f: (
value: V,
key: K
) => Effect.Effect<TxHashMap<K, A>>
f: (value: Vvalue: function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>V, key: Kkey: function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>K) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface TxHashMap<in out K, in out V>A TxHashMap is a transactional hash map data structure that provides atomic operations
on key-value pairs within Effect transactions. It uses an immutable HashMap internally
with TxRef for transactional semantics, ensuring all operations are performed atomically.
Example (Using transactional hash maps)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional hash map
const txMap = yield* TxHashMap.make(["user1", "Alice"], ["user2", "Bob"])
// Single operations are automatically transactional
yield* TxHashMap.set(txMap, "user3", "Charlie")
const user = yield* TxHashMap.get(txMap, "user1")
console.log(user) // Option.some("Alice")
// Multi-step atomic operations
yield* Effect.tx(
Effect.gen(function*() {
const currentUser = yield* TxHashMap.get(txMap, "user1")
if (currentUser._tag === "Some") {
yield* TxHashMap.set(txMap, "user1", currentUser.value + "_updated")
yield* TxHashMap.remove(txMap, "user2")
}
})
)
const size = yield* TxHashMap.size(txMap)
console.log(size) // 2
})
The TxHashMap namespace contains type-level utilities and helper types
for working with TxHashMap instances.
Example (Reusing extracted TxHashMap types)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional inventory map
const inventory = yield* TxHashMap.make(
["laptop", { stock: 5, price: 999 }],
["mouse", { stock: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = TxHashMap.TxHashMap.Key<typeof inventory> // string
type Product = TxHashMap.TxHashMap.Value<typeof inventory> // { stock: number, price: number }
type InventoryEntry = TxHashMap.TxHashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateStock = (id: ProductId, newStock: number) =>
TxHashMap.modify(
inventory,
id,
(product) => ({ ...product, stock: newStock })
)
yield* updateStock("laptop", 3)
})
TxHashMap<function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>K, function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>A>>
): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface TxHashMap<in out K, in out V>A TxHashMap is a transactional hash map data structure that provides atomic operations
on key-value pairs within Effect transactions. It uses an immutable HashMap internally
with TxRef for transactional semantics, ensuring all operations are performed atomically.
Example (Using transactional hash maps)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional hash map
const txMap = yield* TxHashMap.make(["user1", "Alice"], ["user2", "Bob"])
// Single operations are automatically transactional
yield* TxHashMap.set(txMap, "user3", "Charlie")
const user = yield* TxHashMap.get(txMap, "user1")
console.log(user) // Option.some("Alice")
// Multi-step atomic operations
yield* Effect.tx(
Effect.gen(function*() {
const currentUser = yield* TxHashMap.get(txMap, "user1")
if (currentUser._tag === "Some") {
yield* TxHashMap.set(txMap, "user1", currentUser.value + "_updated")
yield* TxHashMap.remove(txMap, "user2")
}
})
)
const size = yield* TxHashMap.size(txMap)
console.log(size) // 2
})
The TxHashMap namespace contains type-level utilities and helper types
for working with TxHashMap instances.
Example (Reusing extracted TxHashMap types)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a transactional inventory map
const inventory = yield* TxHashMap.make(
["laptop", { stock: 5, price: 999 }],
["mouse", { stock: 20, price: 29 }]
)
// Extract types for reuse
type ProductId = TxHashMap.TxHashMap.Key<typeof inventory> // string
type Product = TxHashMap.TxHashMap.Value<typeof inventory> // { stock: number, price: number }
type InventoryEntry = TxHashMap.TxHashMap.Entry<typeof inventory> // [string, Product]
// Use extracted types in functions
const updateStock = (id: ProductId, newStock: number) =>
TxHashMap.modify(
inventory,
id,
(product) => ({ ...product, stock: newStock })
)
yield* updateStock("laptop", 3)
})
TxHashMap<function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>K, function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>A>> =>
import EffectEffect.gen(function*() {
const const currentMap: HashMap.HashMap<K, V>const currentMap: {
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;
}
currentMap = yield* import TxRefTxRef.const get: <A>(
self: TxRef<A>
) => Effect.Effect<A>
Reads the current value of the TxRef.
When to use
Use to read the current value of a TxRef.
Example (Reading transactional references)
import { Effect, TxRef } from "effect"
const program = Effect.gen(function*() {
const counter = yield* TxRef.make(42)
// Read the value within a transaction
const value = yield* Effect.tx(
TxRef.get(counter)
)
console.log(value) // 42
})
get(self: TxHashMap<K, V>(parameter) self: {
ref: TxRef.TxRef<HashMap.HashMap<K, V>>;
toString: () => string;
toJSON: () => unknown;
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; <…;
}
self.TxHashMap<in out K, in out V>.ref: TxRef.TxRef<HashMap.HashMap<K, V>>(property) TxHashMap<in out K, in out V>.ref: {
version: number;
pending: Map<unknown, () => void>;
value: A;
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; <…;
}
ref)
const const result: TxHashMap<K, A>const result: {
ref: TxRef.TxRef<HashMap.HashMap<K, V>>;
toString: () => string;
toJSON: () => unknown;
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; <…;
}
result = yield* const empty: <K, V>() => Effect.Effect<
TxHashMap<K, V>
>
Creates an empty TxHashMap.
Example (Creating an empty map)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create an empty transactional hash map
const emptyMap = yield* TxHashMap.empty<string, number>()
// Verify it's empty
const isEmpty = yield* TxHashMap.isEmpty(emptyMap)
console.log(isEmpty) // true
const size = yield* TxHashMap.size(emptyMap)
console.log(size) // 0
// Start adding elements
yield* TxHashMap.set(emptyMap, "first", 1)
const newSize = yield* TxHashMap.size(emptyMap)
console.log(newSize) // 1
})
empty<function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>K, function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, f: (value: V, key: K) => Effect.Effect<TxHashMap<K, A>>): Effect.Effect<TxHashMap<K, A>>A>()
const const mapEntries: anymapEntries = import HashMapHashMap.toEntries(const currentMap: HashMap.HashMap<K, V>const currentMap: {
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;
}
currentMap)
for (const [const key: anykey, const value: anyvalue] of const mapEntries: anymapEntries) {
const const newMap: TxHashMap<K, A>const newMap: {
ref: TxRef.TxRef<HashMap.HashMap<K, V>>;
toString: () => string;
toJSON: () => unknown;
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; <…;
}
newMap = yield* f: (
value: V,
key: K
) => Effect.Effect<TxHashMap<K, A>>
f(const value: anyvalue, const key: anykey)
const const newEntries: anynewEntries = yield* const entries: <K, V>(
self: TxHashMap<K, V>
) => Effect.Effect<Array<readonly [K, V]>>
Returns an array of all key-value pairs in the TxHashMap.
Example (Reading entries)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
const config = yield* TxHashMap.make(
["host", "localhost"],
["port", "3000"],
["ssl", "false"]
)
const allEntries = yield* TxHashMap.entries(config)
const sortedEntries = allEntries.toSorted(([left], [right]) => left.localeCompare(right))
console.log(sortedEntries)
// [["host", "localhost"], ["port", "3000"], ["ssl", "false"]]
// Process configuration entries
for (const [key, value] of sortedEntries) {
console.log(`${key}=${value}`)
}
// host=localhost
// port=3000
// ssl=false
})
entries(const newMap: TxHashMap<K, A>const newMap: {
ref: TxRef.TxRef<HashMap.HashMap<K, V>>;
toString: () => string;
toJSON: () => unknown;
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; <…;
}
newMap)
yield* const setMany: {
<K1 extends K, K, V1 extends V, V>(
entries: Iterable<readonly [K1, V1]>
): (
self: TxHashMap<K, V>
) => Effect.Effect<void>
<K1 extends K, K, V1 extends V, V>(
self: TxHashMap<K, V>,
entries: Iterable<readonly [K1, V1]>
): Effect.Effect<void>
}
setMany(const result: TxHashMap<K, A>const result: {
ref: TxRef.TxRef<HashMap.HashMap<K, V>>;
toString: () => string;
toJSON: () => unknown;
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; <…;
}
result, const newEntries: anynewEntries)
}
return const result: TxHashMap<K, A>const result: {
ref: TxRef.TxRef<HashMap.HashMap<K, V>>;
toString: () => string;
toJSON: () => unknown;
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; <…;
}
result
}).pipe(import EffectEffect.tx)
)