<A, V, K>(zero: A, f: (accumulator: A, value: V, key: K) => A): (
self: TxHashMap<K, V>
) => Effect.Effect<A>
<K, V, A>(
self: TxHashMap<K, V>,
zero: A,
f: (accumulator: A, value: V, key: K) => A
): Effect.Effect<A>Reduces the TxHashMap entries to a single value by applying a reducer function. Iterates over all key-value pairs and accumulates them into a final result.
Example (Reducing entries)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a sales data map
const sales = yield* TxHashMap.make(
["Q1", 15000],
["Q2", 18000],
["Q3", 22000],
["Q4", 25000]
)
// Calculate total sales
const totalSales = yield* TxHashMap.reduce(
sales,
0,
(total, amount, quarter) => {
console.log(`Adding ${quarter}: ${amount}`)
return total + amount
}
)
console.log(`Total sales: ${totalSales}`) // 80000
// Data-last usage with pipe
const quarterlyReport = yield* sales.pipe(
TxHashMap.reduce(
{ quarters: 0, total: 0, max: 0 },
(report, amount, quarter) => ({
quarters: report.quarters + 1,
total: report.total + amount,
max: Math.max(report.max, amount)
})
)
)
console.log(quarterlyReport) // { quarters: 4, total: 80000, max: 25000 }
// Build a summary string
const summary = yield* TxHashMap.reduce(
sales,
"",
(acc, amount, quarter) => acc + `${quarter}: $${amount.toLocaleString()}\n`
)
console.log(summary)
})export const const reduce: {
<A, V, K>(
zero: A,
f: (accumulator: A, value: V, key: K) => A
): (self: TxHashMap<K, V>) => Effect.Effect<A>
<K, V, A>(
self: TxHashMap<K, V>,
zero: A,
f: (accumulator: A, value: V, key: K) => A
): Effect.Effect<A>
}
Reduces the TxHashMap entries to a single value by applying a reducer function.
Iterates over all key-value pairs and accumulates them into a final result.
Example (Reducing entries)
import { Effect, TxHashMap } from "effect"
const program = Effect.gen(function*() {
// Create a sales data map
const sales = yield* TxHashMap.make(
["Q1", 15000],
["Q2", 18000],
["Q3", 22000],
["Q4", 25000]
)
// Calculate total sales
const totalSales = yield* TxHashMap.reduce(
sales,
0,
(total, amount, quarter) => {
console.log(`Adding ${quarter}: ${amount}`)
return total + amount
}
)
console.log(`Total sales: ${totalSales}`) // 80000
// Data-last usage with pipe
const quarterlyReport = yield* sales.pipe(
TxHashMap.reduce(
{ quarters: 0, total: 0, max: 0 },
(report, amount, quarter) => ({
quarters: report.quarters + 1,
total: report.total + amount,
max: Math.max(report.max, amount)
})
)
)
console.log(quarterlyReport) // { quarters: 4, total: 80000, max: 25000 }
// Build a summary string
const summary = yield* TxHashMap.reduce(
sales,
"",
(acc, amount, quarter) => acc + `${quarter}: $${amount.toLocaleString()}\n`
)
console.log(summary)
})
reduce: {
<function (type parameter) A in <A, V, K>(zero: A, f: (accumulator: A, value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect.Effect<A>A, function (type parameter) V in <A, V, K>(zero: A, f: (accumulator: A, value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect.Effect<A>V, function (type parameter) K in <A, V, K>(zero: A, f: (accumulator: A, value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect.Effect<A>K>(
zero: Azero: function (type parameter) A in <A, V, K>(zero: A, f: (accumulator: A, value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect.Effect<A>A,
f: (accumulator: A, value: V, key: K) => Af: (accumulator: Aaccumulator: function (type parameter) A in <A, V, K>(zero: A, f: (accumulator: A, value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect.Effect<A>A, value: Vvalue: function (type parameter) V in <A, V, K>(zero: A, f: (accumulator: A, value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect.Effect<A>V, key: Kkey: function (type parameter) K in <A, V, K>(zero: A, f: (accumulator: A, value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect.Effect<A>K) => function (type parameter) A in <A, V, K>(zero: A, f: (accumulator: A, value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect.Effect<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>(zero: A, f: (accumulator: A, value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect.Effect<A>K, function (type parameter) V in <A, V, K>(zero: A, f: (accumulator: A, value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect.Effect<A>V>) => import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <A, V, K>(zero: A, f: (accumulator: A, value: V, key: K) => A): (self: TxHashMap<K, V>) => Effect.Effect<A>A>
<function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>K, function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>V, function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<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>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>K, function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>V>,
zero: Azero: function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>A,
f: (accumulator: A, value: V, key: K) => Af: (accumulator: Aaccumulator: function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>A, value: Vvalue: function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>V, key: Kkey: function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>K) => function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>A
): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>A>
} = dual<(...args: Array<any>) => any, <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A) => Effect.Effect<A>>(arity: 3, body: <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A) => Effect.Effect<A>): ((...args: Array<any>) => any) & (<K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A) => Effect.Effect<A>) (+1 overload)Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(
3,
<function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>K, function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>V, function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<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>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>K, function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>V>,
zero: Azero: function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>A,
f: (accumulator: A, value: V, key: K) => Af: (accumulator: Aaccumulator: function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>A, value: Vvalue: function (type parameter) V in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>V, key: Kkey: function (type parameter) K in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>K) => function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>A
): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <K, V, A>(self: TxHashMap<K, V>, zero: A, f: (accumulator: A, value: V, key: K) => A): Effect.Effect<A>A> => 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<K, V>.ref: TxRef.TxRef<HashMap.HashMap<K, V>>(property) TxHashMap<K, 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).Pipeable.pipe<Effect.Effect<HashMap.HashMap<K, V>, never, never>, Effect.Effect<A, never, never>>(this: Effect.Effect<HashMap.HashMap<K, V>, never, never>, ab: (_: Effect.Effect<HashMap.HashMap<K, V>, never, never>) => Effect.Effect<A, never, never>): Effect.Effect<A, never, never> (+21 overloads)pipe(import EffectEffect.const map: {
<A, B>(f: (a: A) => B): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E, R>
<A, E, R, B>(
self: Effect<A, E, R>,
f: (a: A) => B
): Effect<B, E, R>
}
map((map: HashMap.HashMap<K, V>(parameter) map: {
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;
}
map) => import HashMapHashMap.const reduce: {
<Z, V, K>(
zero: Z,
f: (accumulator: Z, value: V, key: K) => Z
): (self: HashMap<K, V>) => Z
<K, V, Z>(
self: HashMap<K, V>,
zero: Z,
f: (accumulator: Z, value: V, key: K) => Z
): Z
}
reduce(map: HashMap.HashMap<K, V>(parameter) map: {
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;
}
map, zero: Azero, f: (accumulator: A, value: V, key: K) => Af)))
)