MutableHashSet
Stores unique values in a mutable hash set.
MutableHashSet updates the same collection in place and supports fast
membership checks, insertion, removal, clearing, and iteration. It is built
on MutableHashMap: each set value is stored as a map key, so uniqueness
follows the same hashing and equality rules as the underlying mutable hash
map.
Constructors
Creates an empty MutableHashSet.
When to use
Use to create a fresh mutable set before adding values over time.
Details
Each call returns a new empty set backed by an empty MutableHashMap.
See
- make for creating a set from explicit values
- fromIterable for creating a set from an iterable of values
- clear for emptying an existing mutable set
Signature
declare function empty<K = never>(): MutableHashSet<K>Example
(Creating an empty set)
import { MutableHashSet } from "effect"
const set = MutableHashSet.empty<string>()
// Add some valuesMutableHashSet.add(set, "apple")MutableHashSet.add(set, "banana")MutableHashSet.add(set, "apple") // Duplicate, no effect
MutableHashSet.size(set) // => 2Array.from(set) // => ["apple", "banana"]fromIterable
Creates a MutableHashSet from an iterable collection of values. Duplicates are automatically removed.
When to use
Use to build a mutable hash set from any iterable of values.
Signature
declare function fromIterable<K = never>(keys: Iterable<K>): MutableHashSet<K>Example
(Creating a set from an iterable)
import { MutableHashSet } from "effect"
const values = ["apple", "banana", "apple", "cherry", "banana"]const set = MutableHashSet.fromIterable(values)
MutableHashSet.size(set) // => 3Array.from(set) // => ["apple", "banana", "cherry"]
// Works with any iterableMutableHashSet.size(MutableHashSet.fromIterable(new Set([1, 2, 3]))) // => 3
// From string charactersArray.from(MutableHashSet.fromIterable("hello")) // => ["h", "e", "l", "o"]Creates a MutableHashSet from a variable number of values. Duplicates are automatically removed.
When to use
Use to build a mutable hash set from explicit values.
Signature
declare function make<Keys extends readonly Array<unknown>>(...keys: Keys): MutableHashSet<Keys[number]>Example
(Creating a set from values)
import { MutableHashSet } from "effect"
const set = MutableHashSet.make("apple", "banana", "apple", "cherry")
MutableHashSet.size(set) // => 3Array.from(set) // => ["apple", "banana", "cherry"]
// With numbersconst numbers = MutableHashSet.make(1, 2, 3, 2, 1)MutableHashSet.size(numbers) // => 3Array.from(numbers) // => [1, 2, 3]
// Mixed typesMutableHashSet.size(MutableHashSet.make("hello", 42, true, "hello")) // => 3Getters
Returns the number of unique values in the MutableHashSet.
When to use
Use to read how many unique values are currently stored in the set.
Signature
declare function size<V>(self: MutableHashSet<V>): numberExample
(Checking set size)
import { MutableHashSet } from "effect"
const set = MutableHashSet.empty<string>()MutableHashSet.size(set) // => 0
MutableHashSet.add(set, "apple")MutableHashSet.add(set, "banana")MutableHashSet.add(set, "apple") // DuplicateMutableHashSet.size(set) // => 2
MutableHashSet.remove(set, "apple")MutableHashSet.size(set) // => 1
MutableHashSet.clear(set)MutableHashSet.size(set) // => 0Guards
isMutableHashSet
Checks whether the specified value is a MutableHashSet, false otherwise.
When to use
Use to narrow an unknown value before treating it as a mutable hash set.
Details
The check looks for the MutableHashSet runtime marker.
Gotchas
Native Set values do not satisfy this check.
See
- MutableHashSet for the mutable hash set interface
Signature
declare function isMutableHashSet<V>(value: unknown): value is MutableHashSet<V>Models
MutableHashSet interface
A mutable hash set for storing unique values with Effect structural equality support.
When to use
Use to store and mutate a collection of unique values with Effect hashing and equality semantics.
Details
Operations mutate the set in place. Values that implement Equal / Hash
can be de-duplicated structurally; other values use normal JavaScript
reference or primitive equality.
Signature
interface MutableHashSet<out V> extends Iterable<V>, Pipeable, Inspectable { readonly "~effect/collections/MutableHashSet": "~effect/collections/MutableHashSet"; readonly keyMap: MutableHashMap<V, boolean>;}Example
(Using a mutable hash set)
import { MutableHashSet } from "effect"
// Create a mutable hash setconst set: MutableHashSet.MutableHashSet<string> = MutableHashSet.make( "apple", "banana")
// Add elementsMutableHashSet.add(set, "cherry")
// Check if elements existMutableHashSet.has(set, "apple") // => trueMutableHashSet.has(set, "grape") // => false
// Collect the iterator valuesArray.from(set) // => ["apple", "banana", "cherry"]
// Get sizeMutableHashSet.size(set) // => 3Mutations
Adds a value to the MutableHashSet, mutating the set in place. If the value already exists, the set remains unchanged.
When to use
Use to insert a value into a mutable set while keeping uniqueness.
Signature
declare const add: { <V>(key: V): (self: MutableHashSet<V>) => MutableHashSet<V>; <V>(self: MutableHashSet<V>, key: V): MutableHashSet<V>;}Example
(Adding values)
import { MutableHashSet } from "effect"
const set = MutableHashSet.empty<string>()
// Add new valuesMutableHashSet.add(set, "apple")MutableHashSet.add(set, "banana")
MutableHashSet.size(set) // => 2MutableHashSet.has(set, "apple") // => true
// Add duplicate (no effect)MutableHashSet.add(set, "apple")MutableHashSet.size(set) // => 2
// Pipe-able versionconst addFruit = MutableHashSet.add("cherry")addFruit(set)MutableHashSet.size(set) // => 3Removes all values from the MutableHashSet, mutating the set in place. The set becomes empty after this operation.
When to use
Use to empty a mutable set while keeping the same set instance.
Signature
declare function clear<V>(self: MutableHashSet<V>): MutableHashSet<V>Example
(Clearing all values)
import { MutableHashSet } from "effect"
const set = MutableHashSet.make("apple", "banana", "cherry")
MutableHashSet.size(set) // => 3
// Clear all valuesMutableHashSet.clear(set)
MutableHashSet.size(set) // => 0MutableHashSet.has(set, "apple") // => falseArray.from(set) // => []
// Can still add new values after clearingMutableHashSet.add(set, "new")MutableHashSet.size(set) // => 1Removes the specified value from the MutableHashSet, mutating the set in place. If the value doesn't exist, the set remains unchanged.
When to use
Use to delete a value from a mutable set if it is present.
Signature
declare const remove: { <V>(key: V): (self: MutableHashSet<V>) => MutableHashSet<V>; <V>(self: MutableHashSet<V>, key: V): MutableHashSet<V>;}Example
(Removing a value)
import { MutableHashSet } from "effect"
const set = MutableHashSet.make("apple", "banana", "cherry")
MutableHashSet.size(set) // => 3
// Remove existing valueMutableHashSet.remove(set, "banana")MutableHashSet.size(set) // => 2MutableHashSet.has(set, "banana") // => false
// Remove non-existent value (no effect)MutableHashSet.remove(set, "grape")MutableHashSet.size(set) // => 2
// Pipe-able versionconst removeFruit = MutableHashSet.remove("apple")removeFruit(set)MutableHashSet.size(set) // => 1Predicates
Checks whether the MutableHashSet contains the specified value.
When to use
Use to test whether a mutable set currently contains a value.
Details
Membership follows the same hashing and equality rules as the underlying
MutableHashMap.
See
Signature
declare const has: { <V>(key: V): (self: MutableHashSet<V>) => boolean; <V>(self: MutableHashSet<V>, key: V): boolean;}Example
(Checking for a value)
import { MutableHashSet } from "effect"
const set = MutableHashSet.make("apple", "banana", "cherry")
MutableHashSet.has(set, "apple") // => trueMutableHashSet.has(set, "grape") // => false
// Pipe-able versionconst hasApple = MutableHashSet.has("apple")hasApple(set) // => true
// Check after addingMutableHashSet.add(set, "grape")MutableHashSet.has(set, "grape") // => true