<V>(values: Iterable<V>): Effect.Effect<TxHashSet<V>>Creates a TxHashSet from an iterable collection of values.
Example (Creating a transactional hash set from an iterable)
import { Effect, TxHashSet } from "effect"
const program = Effect.gen(function*() {
const fromArray = yield* TxHashSet.fromIterable(["a", "b", "c", "b", "a"])
console.log(yield* TxHashSet.size(fromArray)) // 3
const fromSet = yield* TxHashSet.fromIterable(new Set([1, 2, 3]))
console.log(yield* TxHashSet.size(fromSet)) // 3
const fromString = yield* TxHashSet.fromIterable("hello")
const values = yield* TxHashSet.toHashSet(fromString)
console.log(Array.from(values).sort()) // ["e", "h", "l", "o"]
})export const const fromIterable: <V>(
values: Iterable<V>
) => Effect.Effect<TxHashSet<V>>
Creates a TxHashSet from an iterable collection of values.
Example (Creating a transactional hash set from an iterable)
import { Effect, TxHashSet } from "effect"
const program = Effect.gen(function*() {
const fromArray = yield* TxHashSet.fromIterable(["a", "b", "c", "b", "a"])
console.log(yield* TxHashSet.size(fromArray)) // 3
const fromSet = yield* TxHashSet.fromIterable(new Set([1, 2, 3]))
console.log(yield* TxHashSet.size(fromSet)) // 3
const fromString = yield* TxHashSet.fromIterable("hello")
const values = yield* TxHashSet.toHashSet(fromString)
console.log(Array.from(values).sort()) // ["e", "h", "l", "o"]
})
fromIterable = <function (type parameter) V in <V>(values: Iterable<V>): Effect.Effect<TxHashSet<V>>V>(values: Iterable<V>values: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) V in <V>(values: Iterable<V>): Effect.Effect<TxHashSet<V>>V>): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<interface TxHashSet<in out V>A TxHashSet is a transactional hash set data structure that provides atomic operations on unique values within Effect transactions. It uses an immutable HashSet internally with TxRef for transactional semantics, ensuring all operations are performed atomically.
Details
Mutation operations such as add, remove, and clear update the original TxHashSet and return Effect<void> or Effect<boolean>. Transform operations such as union, intersection, difference, map, and filter create new TxHashSet instances and leave the original TxHashSet unchanged.
Example (Using transactional hash sets)
import { Effect, TxHashSet } from "effect"
const program = Effect.gen(function*() {
// Create a transactional hash set
const txSet = yield* TxHashSet.make("apple", "banana", "cherry")
// Single operations are automatically transactional
yield* TxHashSet.add(txSet, "grape")
const hasApple = yield* TxHashSet.has(txSet, "apple")
console.log(hasApple) // true
// Multi-step atomic operations
yield* Effect.tx(
Effect.gen(function*() {
const hasCherry = yield* TxHashSet.has(txSet, "cherry")
if (hasCherry) {
yield* TxHashSet.remove(txSet, "cherry")
yield* TxHashSet.add(txSet, "orange")
}
})
)
const size = yield* TxHashSet.size(txSet)
console.log(size) // 4
})
The TxHashSet namespace contains type-level utilities and helper types
for working with TxHashSet instances.
Example (Extracting value types inside transactions)
import { Effect, TxHashSet } from "effect"
const program = Effect.gen(function*() {
// Create a transactional color set
const colors = yield* TxHashSet.make("red", "green", "blue")
// Extract the value type for reuse
type Color = TxHashSet.TxHashSet.Value<typeof colors> // string
// Use extracted type in functions
const addColor = (color: Color) => TxHashSet.add(colors, color)
yield* addColor("yellow")
})
TxHashSet<function (type parameter) V in <V>(values: Iterable<V>): Effect.Effect<TxHashSet<V>>V>> =>
import EffectEffect.gen(function*() {
const const hashSet: HashSet.HashSet<V>const hashSet: {
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;
}
hashSet = import HashSetHashSet.fromIterable(values: Iterable<V>values)
const const ref: TxRef.TxRef<HashSet.HashSet<V>>const 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 = yield* import TxRefTxRef.const make: <any>(initial: any) => anyCreates a new TxRef with the specified initial value.
When to use
Use to create a TxRef inside an Effect workflow.
Example (Creating transactional references)
import { Effect, TxRef } from "effect"
const program = Effect.gen(function*() {
// Create a transactional reference with initial value
const counter = yield* TxRef.make(0)
const name = yield* TxRef.make("Alice")
// Use in transactions
yield* Effect.tx(Effect.gen(function*() {
yield* TxRef.set(counter, 42)
yield* TxRef.set(name, "Bob")
}))
console.log(yield* TxRef.get(counter)) // 42
console.log(yield* TxRef.get(name)) // "Bob"
})
make(const hashSet: HashSet.HashSet<V>const hashSet: {
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;
}
hashSet)
return const makeTxHashSet: <V>(
ref: TxRef.TxRef<HashSet.HashSet<V>>
) => TxHashSet<V>
makeTxHashSet(const ref: TxRef.TxRef<HashSet.HashSet<V>>const 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)
})