HashSet
Stores unique values in an immutable hash set.
A HashSet<A> contains at most one value for each equality class according
to Effect's Equal and Hash rules. Membership checks, additions, removals,
and set operations return new sets. This module also includes constructors,
union, intersection, difference, subset checks, mapping, filtering, and
reducing helpers.
Combinators
Creates the difference of two HashSets (elements in the first set that are not in the second).
Signature
declare const difference: { <V1>(that: HashSet<V1>): <V0>(self: HashSet<V0>) => HashSet<V0>; <V0, V1>(self: HashSet<V0>, that: HashSet<V1>): HashSet<V0>;}Example Finding HashSet differences
import { HashSet } from "effect"
HashSet.difference(HashSet.make("a", "b", "c"), HashSet.make("b", "d")) // => HashSet.make("a", "c")Creates the intersection of two HashSets.
Signature
declare const intersection: { <V1>(that: HashSet<V1>): <V0>(self: HashSet<V0>) => HashSet<V1 & V0>; <V0, V1>(self: HashSet<V0>, that: HashSet<V1>): HashSet<V0 & V1>;}Example Finding common HashSet values
import { HashSet } from "effect"
HashSet.intersection(HashSet.make("a", "b", "c"), HashSet.make("b", "c", "d")) // => HashSet.make("b", "c")Creates the union of two HashSets.
Signature
declare const union: { <V1>(that: HashSet<V1>): <V0>(self: HashSet<V0>) => HashSet<V1 | V0>; <V0, V1>(self: HashSet<V0>, that: HashSet<V1>): HashSet<V0 | V1>;}Example Combining HashSets
import { HashSet } from "effect"
HashSet.union(HashSet.make("a", "b"), HashSet.make("b", "c")) // => HashSet.make("a", "b", "c")Constructors
Creates an empty HashSet.
Signature
declare const empty: <V = never>() => HashSet<V>Example Creating an empty HashSet
import { HashSet } from "effect"
const set = HashSet.empty<string>()
HashSet.size(set) // => 0HashSet.isEmpty(set) // => true
// Add some valuesconst withValues = HashSet.add(HashSet.add(set, "hello"), "world")withValues // => HashSet.make("hello", "world")Creates a HashSet from an iterable collection of values.
Signature
declare const fromIterable: <V>(values: Iterable<V>) => HashSet<V>Example Creating a HashSet from an iterable
import { HashSet } from "effect"
HashSet.fromIterable(["a", "b", "c", "b", "a"]) // => HashSet.make("a", "b", "c")
HashSet.fromIterable(new Set([1, 2, 3])) // => HashSet.make(1, 2, 3)
HashSet.fromIterable("hello") // => HashSet.make("h", "e", "l", "o")Creates a HashSet from a variable number of values.
Signature
declare const make: <Values extends ReadonlyArray<any>>(...values: Values) => HashSet<Values[number]>Example Creating a HashSet from values
import { HashSet } from "effect"
HashSet.make("apple", "banana", "cherry") // => HashSet.make("apple", "banana", "cherry")
HashSet.make(1, 2, 3, 2, 1) // => HashSet.make(1, 2, 3)
HashSet.make("hello", 42, true) // => HashSet.make("hello", 42, true)Filtering
Filters the HashSet keeping only values that satisfy the predicate.
Signature
declare const filter: { <V, U>(refinement: Refinement<NoInfer<V>, U>): (self: HashSet<V>) => HashSet<U>; <V>(predicate: Predicate<NoInfer<V>>): (self: HashSet<V>) => HashSet<V>; <V, U>(self: HashSet<V>, refinement: Refinement<V, U>): HashSet<U>; <V>(self: HashSet<V>, predicate: Predicate<V>): HashSet<V>;}Example Filtering HashSet values
import { HashSet } from "effect"
HashSet.filter(HashSet.make(1, 2, 3, 4, 5, 6), (n) => n % 2 === 0) // => HashSet.make(2, 4, 6)Folding
Reduces the HashSet to a single value by iterating through the values and applying an accumulator function.
Signature
declare const reduce: { <V, U>(zero: U, f: (accumulator: U, value: V) => U): (self: HashSet<V>) => U; <V, U>(self: HashSet<V>, zero: U, f: (accumulator: U, value: V) => U): U;}Example Reducing HashSet values
import { HashSet } from "effect"
const numbers = HashSet.make(1, 2, 3, 4, 5)HashSet.reduce(numbers, 0, (acc, n) => acc + n) // => 15Getters
Returns the number of values in the HashSet.
Signature
declare const size: <V>(self: HashSet<V>) => numberExample Getting the HashSet size
import { HashSet } from "effect"
HashSet.size(HashSet.empty<string>()) // => 0
HashSet.size(HashSet.make("a", "b")) // => 2
HashSet.size(HashSet.fromIterable(["x", "y", "z", "x", "y"])) // => 3Guards
Checks whether a value is a HashSet.
Signature
declare const isHashSet: { <V>(u: Iterable<V>): u is HashSet<V>; (u: unknown): u is HashSet<unknown>;}Example Checking for a HashSet
import { HashSet } from "effect"
const set = HashSet.make(1, 2, 3)const array = [1, 2, 3]
HashSet.isHashSet(set) // => trueHashSet.isHashSet(array) // => falseHashSet.isHashSet(null) // => falseMapping
Maps each value in the HashSet using the provided function.
Signature
declare const map: { <V, U>(f: (value: V) => U): (self: HashSet<V>) => HashSet<U>; <V, U>(self: HashSet<V>, f: (value: V) => U): HashSet<U>;}Example Mapping HashSet values
import { HashSet } from "effect"
const numbers = HashSet.make(1, 2, 3)const doubled = HashSet.map(numbers, (n) => n * 2)
doubled // => HashSet.make(2, 4, 6)
// Mapping can reduce size if function produces duplicatesconst strings = HashSet.make("apple", "banana", "cherry")const lengths = HashSet.map(strings, (s) => s.length)lengths // => HashSet.make(5, 6)Models
A HashSet is an immutable set data structure that provides efficient storage and retrieval of unique values. It uses a HashMap internally for optimal performance.
Signature
interface HashSet<out Value> extends Iterable<Value>, Equal, Pipeable, Inspectable { readonly "~effect/HashSet": "~effect/HashSet";}Example Creating and updating a HashSet
import { HashSet } from "effect"
// Create a HashSetconst set = HashSet.make("apple", "banana", "cherry")
// Check membershipHashSet.has(set, "apple") // => trueHashSet.has(set, "grape") // => false
// Add values (returns new HashSet)const updated = HashSet.add(set, "grape")updated // => HashSet.make("apple", "banana", "cherry", "grape")
// Remove values (returns new HashSet)const smaller = HashSet.remove(set, "banana")smaller // => HashSet.make("apple", "cherry")Mutations
Adds a value to the HashSet, returning a new HashSet.
Signature
declare const add: { <V>(value: V): (self: HashSet<V>) => HashSet<V>; <V>(self: HashSet<V>, value: V): HashSet<V>;}Example Adding values to a HashSet
import { HashSet } from "effect"
const set = HashSet.make("a", "b")const withC = HashSet.add(set, "c")
set // => HashSet.make("a", "b")withC // => HashSet.make("a", "b", "c")HashSet.has(withC, "c") // => true
// Adding existing value has no effectHashSet.add(set, "a") // => HashSet.make("a", "b")Removes a value from the HashSet, returning a new HashSet.
Signature
declare const remove: { <V>(value: V): (self: HashSet<V>) => HashSet<V>; <V>(self: HashSet<V>, value: V): HashSet<V>;}Example Removing values from a HashSet
import { HashSet } from "effect"
const set = HashSet.make("a", "b", "c")const withoutB = HashSet.remove(set, "b")
set // => HashSet.make("a", "b", "c")withoutB // => HashSet.make("a", "c")HashSet.has(withoutB, "b") // => false
// Removing non-existent value has no effectHashSet.remove(set, "d") // => HashSet.make("a", "b", "c")Other
The HashSet namespace contains type-level utilities and helper types for working with HashSet instances.
Example Extracting value types from a HashSet
import { HashSet } from "effect"
// Create a concrete HashSet for type extractionconst fruits = HashSet.make("apple", "banana", "cherry")
// Extract the value type for reusetype Fruit = HashSet.HashSet.Value<typeof fruits> // string
// Use extracted type in functionsconst processFruit = (fruit: Fruit) => { return `Processing ${fruit}`}processFruit("apple") // => "Processing apple"Predicates
Checks whether all values in the HashSet satisfy the predicate.
Signature
declare const every: { <V>(predicate: Predicate<V>): (self: HashSet<V>) => boolean; <V>(self: HashSet<V>, predicate: Predicate<V>): boolean;}Example Testing whether every value matches
import { HashSet } from "effect"
const numbers = HashSet.make(2, 4, 6, 8)
HashSet.every(numbers, (n) => n % 2 === 0) // => trueHashSet.every(numbers, (n) => n > 5) // => false
HashSet.every(HashSet.empty<number>(), (n) => n > 0) // => trueChecks whether the HashSet contains the specified value.
Signature
declare const has: { <V>(value: V): (self: HashSet<V>) => boolean; <V>(self: HashSet<V>, value: V): boolean;}Example Checking HashSet membership
import { Equal, Hash, HashSet } from "effect"
// Works with any type that implements Equal
const set = HashSet.make("apple", "banana", "cherry")
HashSet.has(set, "apple") // => trueHashSet.has(set, "grape") // => false
class Person implements Equal.Equal { constructor(readonly name: string) {}
[Equal.symbol](other: unknown) { return other instanceof Person && this.name === other.name }
[Hash.symbol](): number { return Hash.string(this.name) }}
const people = HashSet.make(new Person("Alice"), new Person("Bob"))HashSet.has(people, new Person("Alice")) // => trueChecks whether the HashSet is empty.
Signature
declare const isEmpty: <V>(self: HashSet<V>) => booleanExample Checking whether a HashSet is empty
import { HashSet } from "effect"
HashSet.isEmpty(HashSet.empty<string>()) // => true
HashSet.isEmpty(HashSet.make("a")) // => falseChecks whether a HashSet is a subset of another HashSet.
Signature
declare const isSubset: { <V1>(that: HashSet<V1>): <V0>(self: HashSet<V0>) => boolean; <V0, V1>(self: HashSet<V0>, that: HashSet<V1>): boolean;}Example Checking subset relationships
import { HashSet } from "effect"
const small = HashSet.make("a", "b")const large = HashSet.make("a", "b", "c", "d")const other = HashSet.make("x", "y")
HashSet.isSubset(small, large) // => trueHashSet.isSubset(large, small) // => falseHashSet.isSubset(small, other) // => falseHashSet.isSubset(small, small) // => trueChecks whether at least one value in the HashSet satisfies the predicate.
Signature
declare const some: { <V>(predicate: Predicate<V>): (self: HashSet<V>) => boolean; <V>(self: HashSet<V>, predicate: Predicate<V>): boolean;}Example Testing whether some values match
import { HashSet } from "effect"
const numbers = HashSet.make(1, 2, 3, 4, 5)
HashSet.some(numbers, (n) => n > 3) // => trueHashSet.some(numbers, (n) => n > 10) // => false
HashSet.some(HashSet.empty<number>(), (n) => n > 0) // => false