<K, V>(self: TxHashMap<K, V>): Effect.Effect<Array<V>>Returns an array of all values in the TxHashMap.
This is an alias for the values function, providing API consistency with HashMap.
Example (Converting to values)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
const inventory = yield* TxHashMap.make(
["laptop", { price: 999, stock: 5 }],
["mouse", { price: 29, stock: 50 }],
["keyboard", { price: 79, stock: 20 }]
)
// Get all product information
const products = yield* TxHashMap.toValues(inventory)
console.log(products.length) // 3
// Calculate total inventory value
const totalValue = products.reduce(
(sum, product) => sum + (product.price * product.stock),
0
)
console.log(`Total inventory value: $${totalValue}`) // Total inventory value: $8025
// Find products with low stock
const lowStockProducts = products.filter((product) => product.stock < 10)
console.log(`${lowStockProducts.length} product with low stock`) // 1 product with low stock
})export const const toValues: <K, V>(
self: TxHashMap<K, V>
) => Effect.Effect<Array<V>>
Returns an array of all values in the TxHashMap.
This is an alias for the values function, providing API consistency with HashMap.
Example (Converting to values)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
const inventory = yield* TxHashMap.make(
["laptop", { price: 999, stock: 5 }],
["mouse", { price: 29, stock: 50 }],
["keyboard", { price: 79, stock: 20 }]
)
// Get all product information
const products = yield* TxHashMap.toValues(inventory)
console.log(products.length) // 3
// Calculate total inventory value
const totalValue = products.reduce(
(sum, product) => sum + (product.price * product.stock),
0
)
console.log(`Total inventory value: $${totalValue}`) // Total inventory value: $8025
// Find products with low stock
const lowStockProducts = products.filter((product) => product.stock < 10)
console.log(`${lowStockProducts.length} product with low stock`) // 1 product with low stock
})
toValues = <function (type parameter) K in <K, V>(self: TxHashMap<K, V>): Effect.Effect<Array<V>>K, function (type parameter) V in <K, V>(self: TxHashMap<K, V>): Effect.Effect<Array<V>>V>(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>(self: TxHashMap<K, V>): Effect.Effect<Array<V>>K, function (type parameter) V in <K, V>(self: TxHashMap<K, V>): Effect.Effect<Array<V>>V>): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface Array<T>Array<function (type parameter) V in <K, V>(self: TxHashMap<K, V>): Effect.Effect<Array<V>>V>> => const values: <K, V>(
self: TxHashMap<K, V>
) => Effect.Effect<Array<V>>
Returns an array of all values in the TxHashMap.
Example (Reading values)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
const scores = yield* TxHashMap.make(
["alice", 95],
["bob", 87],
["charlie", 92]
)
const allScores = yield* TxHashMap.values(scores)
console.log(allScores.sort((a, b) => a - b)) // [87, 92, 95]
// Calculate average
const average = allScores.reduce((sum, score) => sum + score, 0) /
allScores.length
console.log(average.toFixed(2)) // "91.33"
// Find maximum
const maxScore = Math.max(...allScores)
console.log(maxScore) // 95
})
values(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)