Open History to see the last commit
1/**
2 * Stores unique values in an immutable hash set.
3 *
4 * A `HashSet<A>` contains at most one value for each equality class according
5 * to Effect's `Equal` and `Hash` rules. Membership checks, additions, removals,
6 * and set operations return new sets. This module also includes constructors,
7 * union, intersection, difference, subset checks, mapping, filtering, and
8 * reducing helpers.
9 *
10 * @since 2.0.0
11 */
12
13import type { Equal } from "./Equal.ts"
14import * as Dual from "./Function.ts"
15import type { Inspectable } from "./Inspectable.ts"
16import * as internal from "./internal/hashSet.ts"
17import type { Pipeable } from "./Pipeable.ts"
18import type { Predicate, Refinement } from "./Predicate.ts"
19import type { NoInfer } from "./Types.ts"
20
21const TypeId = internal.HashSetTypeId
22
23/**
24 * A HashSet is an immutable set data structure that provides efficient storage
25 * and retrieval of unique values. It uses a HashMap internally for optimal performance.
26 *
27 * **Example** (Creating and updating a HashSet)
28 *
29 * ```ts
30 * import { HashSet } from "effect"
31 *
32 * // Create a HashSet
33 * const set = HashSet.make("apple", "banana", "cherry")
34 *
35 * // Check membership
36 * console.log(HashSet.has(set, "apple")) // true
37 * console.log(HashSet.has(set, "grape")) // false
38 *
39 * // Add values (returns new HashSet)
40 * const updated = HashSet.add(set, "grape")
41 * console.log(HashSet.size(updated)) // 4
42 *
43 * // Remove values (returns new HashSet)
44 * const smaller = HashSet.remove(set, "banana")
45 * console.log(HashSet.size(smaller)) // 2
46 * ```
47 *
48 * @category models
49 * @since 2.0.0
50 */
51export interface HashSet<out Value> extends Iterable<Value>, Equal, Pipeable, Inspectable {
52 readonly [TypeId]: typeof TypeId
53}
54
55/**
56 * The HashSet namespace contains type-level utilities and helper types
57 * for working with HashSet instances.
58 *
59 * **Example** (Extracting value types from a HashSet)
60 *
61 * ```ts
62 * import { HashSet } from "effect"
63 *
64 * // Create a concrete HashSet for type extraction
65 * const fruits = HashSet.make("apple", "banana", "cherry")
66 *
67 * // Extract the value type for reuse
68 * type Fruit = HashSet.HashSet.Value<typeof fruits> // string
69 *
70 * // Use extracted type in functions
71 * const processFruit = (fruit: Fruit) => {
72 * return `Processing ${fruit}`
73 * }
74 * ```
75 *
76 * @since 2.0.0
77 */
78export declare namespace HashSet {
79 /**
80 * Extracts the element type from a `HashSet`.
81 *
82 * **Details**
83 *
84 * For `HashSet.HashSet<A>`, `HashSet.Value<...>` resolves to `A`.
85 *
86 * **Example** (Extracting a HashSet value type)
87 *
88 * ```ts
89 * import { HashSet } from "effect"
90 *
91 * const numbers = HashSet.make(1, 2, 3, 4, 5)
92 *
93 * // Extract the value type
94 * type NumberType = HashSet.HashSet.Value<typeof numbers> // number
95 *
96 * const processNumber = (n: NumberType) => n * 2
97 * ```
98 *
99 * @category utility types
100 * @since 4.0.0
101 */
102 export type Value<T> = T extends HashSet<infer V> ? V : never
103}
104
105/**
106 * Creates an empty HashSet.
107 *
108 * **Example** (Creating an empty HashSet)
109 *
110 * ```ts
111 * import { HashSet } from "effect"
112 *
113 * const set = HashSet.empty<string>()
114 *
115 * console.log(HashSet.size(set)) // 0
116 * console.log(HashSet.isEmpty(set)) // true
117 *
118 * // Add some values
119 * const withValues = HashSet.add(HashSet.add(set, "hello"), "world")
120 * console.log(HashSet.size(withValues)) // 2
121 * ```
122 *
123 * @category constructors
124 * @since 2.0.0
125 */
126export const empty: <V = never>() => HashSet<V> = internal.empty
127
128/**
129 * Creates a HashSet from a variable number of values.
130 *
131 * **Example** (Creating a HashSet from values)
132 *
133 * ```ts
134 * import { HashSet } from "effect"
135 *
136 * const fruits = HashSet.make("apple", "banana", "cherry")
137 * console.log(HashSet.size(fruits)) // 3
138 *
139 * const numbers = HashSet.make(1, 2, 3, 2, 1) // Duplicates ignored
140 * console.log(HashSet.size(numbers)) // 3
141 *
142 * const mixed = HashSet.make("hello", 42, true)
143 * console.log(HashSet.size(mixed)) // 3
144 * ```
145 *
146 * @category constructors
147 * @since 2.0.0
148 */
149export const make: <Values extends ReadonlyArray<any>>(
150 ...values: Values
151) => HashSet<Values[number]> = internal.make
152
153/**
154 * Creates a HashSet from an iterable collection of values.
155 *
156 * **Example** (Creating a HashSet from an iterable)
157 *
158 * ```ts
159 * import { HashSet } from "effect"
160 *
161 * const fromArray = HashSet.fromIterable(["a", "b", "c", "b", "a"])
162 * console.log(HashSet.size(fromArray)) // 3
163 *
164 * const fromSet = HashSet.fromIterable(new Set([1, 2, 3]))
165 * console.log(HashSet.size(fromSet)) // 3
166 *
167 * const fromString = HashSet.fromIterable("hello")
168 * console.log(Array.from(fromString)) // ["h", "e", "l", "o"]
169 * ```
170 *
171 * @category constructors
172 * @since 2.0.0
173 */
174export const fromIterable: <V>(values: Iterable<V>) => HashSet<V> = internal.fromIterable
175
176/**
177 * Checks whether a value is a HashSet.
178 *
179 * **Example** (Checking for a HashSet)
180 *
181 * ```ts
182 * import { HashSet } from "effect"
183 *
184 * const set = HashSet.make(1, 2, 3)
185 * const array = [1, 2, 3]
186 *
187 * console.log(HashSet.isHashSet(set)) // true
188 * console.log(HashSet.isHashSet(array)) // false
189 * console.log(HashSet.isHashSet(null)) // false
190 * ```
191 *
192 * @category guards
193 * @since 2.0.0
194 */
195export const isHashSet: {
196 <V>(u: Iterable<V>): u is HashSet<V>
197 (u: unknown): u is HashSet<unknown>
198} = internal.isHashSet
199
200/**
201 * Adds a value to the HashSet, returning a new HashSet.
202 *
203 * **Example** (Adding values to a HashSet)
204 *
205 * ```ts
206 * import { HashSet } from "effect"
207 *
208 * const set = HashSet.make("a", "b")
209 * const withC = HashSet.add(set, "c")
210 *
211 * console.log(HashSet.size(set)) // 2 (original unchanged)
212 * console.log(HashSet.size(withC)) // 3
213 * console.log(HashSet.has(withC, "c")) // true
214 *
215 * // Adding existing value has no effect
216 * const same = HashSet.add(set, "a")
217 * console.log(HashSet.size(same)) // 2
218 * ```
219 *
220 * @category mutations
221 * @since 2.0.0
222 */
223export const add: {
224 <V>(value: V): (self: HashSet<V>) => HashSet<V>
225 <V>(self: HashSet<V>, value: V): HashSet<V>
226} = Dual.dual<
227 <V>(value: V) => (self: HashSet<V>) => HashSet<V>,
228 <V>(self: HashSet<V>, value: V) => HashSet<V>
229>(2, internal.add)
230
231/**
232 * Checks whether the HashSet contains the specified value.
233 *
234 * **Example** (Checking HashSet membership)
235 *
236 * ```ts
237 * import { Equal, Hash, HashSet } from "effect"
238 *
239 * // Works with any type that implements Equal
240 *
241 * const set = HashSet.make("apple", "banana", "cherry")
242 *
243 * console.log(HashSet.has(set, "apple")) // true
244 * console.log(HashSet.has(set, "grape")) // false
245 *
246 * class Person implements Equal.Equal {
247 * constructor(readonly name: string) {}
248 *
249 * [Equal.symbol](other: unknown) {
250 * return other instanceof Person && this.name === other.name
251 * }
252 *
253 * [Hash.symbol](): number {
254 * return Hash.string(this.name)
255 * }
256 * }
257 *
258 * const people = HashSet.make(new Person("Alice"), new Person("Bob"))
259 * console.log(HashSet.has(people, new Person("Alice"))) // true
260 * ```
261 *
262 * @category elements
263 * @since 2.0.0
264 */
265export const has: {
266 <V>(value: V): (self: HashSet<V>) => boolean
267 <V>(self: HashSet<V>, value: V): boolean
268} = Dual.dual<
269 <V>(value: V) => (self: HashSet<V>) => boolean,
270 <V>(self: HashSet<V>, value: V) => boolean
271>(2, internal.has)
272
273/**
274 * Removes a value from the HashSet, returning a new HashSet.
275 *
276 * **Example** (Removing values from a HashSet)
277 *
278 * ```ts
279 * import { HashSet } from "effect"
280 *
281 * const set = HashSet.make("a", "b", "c")
282 * const withoutB = HashSet.remove(set, "b")
283 *
284 * console.log(HashSet.size(set)) // 3 (original unchanged)
285 * console.log(HashSet.size(withoutB)) // 2
286 * console.log(HashSet.has(withoutB, "b")) // false
287 *
288 * // Removing non-existent value has no effect
289 * const same = HashSet.remove(set, "d")
290 * console.log(HashSet.size(same)) // 3
291 * ```
292 *
293 * @category mutations
294 * @since 2.0.0
295 */
296export const remove: {
297 <V>(value: V): (self: HashSet<V>) => HashSet<V>
298 <V>(self: HashSet<V>, value: V): HashSet<V>
299} = Dual.dual<
300 <V>(value: V) => (self: HashSet<V>) => HashSet<V>,
301 <V>(self: HashSet<V>, value: V) => HashSet<V>
302>(2, internal.remove)
303
304/**
305 * Returns the number of values in the HashSet.
306 *
307 * **Example** (Getting the HashSet size)
308 *
309 * ```ts
310 * import { HashSet } from "effect"
311 *
312 * const empty = HashSet.empty<string>()
313 * console.log(HashSet.size(empty)) // 0
314 *
315 * const small = HashSet.make("a", "b")
316 * console.log(HashSet.size(small)) // 2
317 *
318 * const withDuplicates = HashSet.fromIterable(["x", "y", "z", "x", "y"])
319 * console.log(HashSet.size(withDuplicates)) // 3
320 * ```
321 *
322 * @category getters
323 * @since 2.0.0
324 */
325export const size: <V>(self: HashSet<V>) => number = internal.size
326
327/**
328 * Checks whether the HashSet is empty.
329 *
330 * **Example** (Checking whether a HashSet is empty)
331 *
332 * ```ts
333 * import { HashSet } from "effect"
334 *
335 * const empty = HashSet.empty<string>()
336 * console.log(HashSet.isEmpty(empty)) // true
337 *
338 * const nonEmpty = HashSet.make("a")
339 * console.log(HashSet.isEmpty(nonEmpty)) // false
340 * ```
341 *
342 * @category getters
343 * @since 4.0.0
344 */
345export const isEmpty: <V>(self: HashSet<V>) => boolean = internal.isEmpty
346
347/**
348 * Creates the union of two HashSets.
349 *
350 * **Example** (Combining HashSets)
351 *
352 * ```ts
353 * import { HashSet } from "effect"
354 *
355 * const set1 = HashSet.make("a", "b")
356 * const set2 = HashSet.make("b", "c")
357 * const combined = HashSet.union(set1, set2)
358 *
359 * console.log(Array.from(combined).sort()) // ["a", "b", "c"]
360 * console.log(HashSet.size(combined)) // 3
361 * ```
362 *
363 * @category combinators
364 * @since 2.0.0
365 */
366export const union: {
367 <V1>(that: HashSet<V1>): <V0>(self: HashSet<V0>) => HashSet<V1 | V0>
368 <V0, V1>(self: HashSet<V0>, that: HashSet<V1>): HashSet<V0 | V1>
369} = Dual.dual<
370 <V1>(that: HashSet<V1>) => <V0>(self: HashSet<V0>) => HashSet<V1 | V0>,
371 <V0, V1>(self: HashSet<V0>, that: HashSet<V1>) => HashSet<V0 | V1>
372>(2, internal.union)
373
374/**
375 * Creates the intersection of two HashSets.
376 *
377 * **Example** (Finding common HashSet values)
378 *
379 * ```ts
380 * import { HashSet } from "effect"
381 *
382 * const set1 = HashSet.make("a", "b", "c")
383 * const set2 = HashSet.make("b", "c", "d")
384 * const common = HashSet.intersection(set1, set2)
385 *
386 * console.log(Array.from(common).sort()) // ["b", "c"]
387 * console.log(HashSet.size(common)) // 2
388 * ```
389 *
390 * @category combinators
391 * @since 2.0.0
392 */
393export const intersection: {
394 <V1>(that: HashSet<V1>): <V0>(self: HashSet<V0>) => HashSet<V1 & V0>
395 <V0, V1>(self: HashSet<V0>, that: HashSet<V1>): HashSet<V0 & V1>
396} = Dual.dual<
397 <V1>(that: HashSet<V1>) => <V0>(self: HashSet<V0>) => HashSet<V1 & V0>,
398 <V0, V1>(self: HashSet<V0>, that: HashSet<V1>) => HashSet<V0 & V1>
399>(2, internal.intersection)
400
401/**
402 * Creates the difference of two HashSets (elements in the first set that are not in the second).
403 *
404 * **Example** (Finding HashSet differences)
405 *
406 * ```ts
407 * import { HashSet } from "effect"
408 *
409 * const set1 = HashSet.make("a", "b", "c")
410 * const set2 = HashSet.make("b", "d")
411 * const diff = HashSet.difference(set1, set2)
412 *
413 * console.log(Array.from(diff).sort()) // ["a", "c"]
414 * console.log(HashSet.size(diff)) // 2
415 * ```
416 *
417 * @category combinators
418 * @since 2.0.0
419 */
420export const difference: {
421 <V1>(that: HashSet<V1>): <V0>(self: HashSet<V0>) => HashSet<V0>
422 <V0, V1>(self: HashSet<V0>, that: HashSet<V1>): HashSet<V0>
423} = Dual.dual<
424 <V1>(that: HashSet<V1>) => <V0>(self: HashSet<V0>) => HashSet<V0>,
425 <V0, V1>(self: HashSet<V0>, that: HashSet<V1>) => HashSet<V0>
426>(2, internal.difference)
427
428/**
429 * Checks whether a HashSet is a subset of another HashSet.
430 *
431 * **Example** (Checking subset relationships)
432 *
433 * ```ts
434 * import { HashSet } from "effect"
435 *
436 * const small = HashSet.make("a", "b")
437 * const large = HashSet.make("a", "b", "c", "d")
438 * const other = HashSet.make("x", "y")
439 *
440 * console.log(HashSet.isSubset(small, large)) // true
441 * console.log(HashSet.isSubset(large, small)) // false
442 * console.log(HashSet.isSubset(small, other)) // false
443 * console.log(HashSet.isSubset(small, small)) // true
444 * ```
445 *
446 * @category elements
447 * @since 2.0.0
448 */
449export const isSubset: {
450 <V1>(that: HashSet<V1>): <V0>(self: HashSet<V0>) => boolean
451 <V0, V1>(self: HashSet<V0>, that: HashSet<V1>): boolean
452} = Dual.dual<
453 <V1>(that: HashSet<V1>) => <V0>(self: HashSet<V0>) => boolean,
454 <V0, V1>(self: HashSet<V0>, that: HashSet<V1>) => boolean
455>(2, internal.isSubset)
456
457/**
458 * Maps each value in the HashSet using the provided function.
459 *
460 * **Example** (Mapping HashSet values)
461 *
462 * ```ts
463 * import { HashSet } from "effect"
464 *
465 * const numbers = HashSet.make(1, 2, 3)
466 * const doubled = HashSet.map(numbers, (n) => n * 2)
467 *
468 * console.log(Array.from(doubled).sort()) // [2, 4, 6]
469 * console.log(HashSet.size(doubled)) // 3
470 *
471 * // Mapping can reduce size if function produces duplicates
472 * const strings = HashSet.make("apple", "banana", "cherry")
473 * const lengths = HashSet.map(strings, (s) => s.length)
474 * console.log(Array.from(lengths).sort()) // [5, 6] (apple=5, banana=6, cherry=6)
475 * ```
476 *
477 * @category mapping
478 * @since 2.0.0
479 */
480export const map: {
481 <V, U>(f: (value: V) => U): (self: HashSet<V>) => HashSet<U>
482 <V, U>(self: HashSet<V>, f: (value: V) => U): HashSet<U>
483} = Dual.dual<
484 <V, U>(f: (value: V) => U) => (self: HashSet<V>) => HashSet<U>,
485 <V, U>(self: HashSet<V>, f: (value: V) => U) => HashSet<U>
486>(2, internal.map)
487
488/**
489 * Filters the HashSet keeping only values that satisfy the predicate.
490 *
491 * **Example** (Filtering HashSet values)
492 *
493 * ```ts
494 * import { HashSet } from "effect"
495 *
496 * const numbers = HashSet.make(1, 2, 3, 4, 5, 6)
497 * const evens = HashSet.filter(numbers, (n) => n % 2 === 0)
498 *
499 * console.log(Array.from(evens).sort()) // [2, 4, 6]
500 * console.log(HashSet.size(evens)) // 3
501 * ```
502 *
503 * @category filtering
504 * @since 2.0.0
505 */
506export const filter: {
507 <V, U extends V>(refinement: Refinement<NoInfer<V>, U>): (self: HashSet<V>) => HashSet<U>
508 <V>(predicate: Predicate<NoInfer<V>>): (self: HashSet<V>) => HashSet<V>
509 <V, U extends V>(self: HashSet<V>, refinement: Refinement<V, U>): HashSet<U>
510 <V>(self: HashSet<V>, predicate: Predicate<V>): HashSet<V>
511} = Dual.dual<
512 {
513 <V, U extends V>(refinement: Refinement<NoInfer<V>, U>): (self: HashSet<V>) => HashSet<U>
514 <V>(predicate: Predicate<NoInfer<V>>): (self: HashSet<V>) => HashSet<V>
515 },
516 {
517 <V, U extends V>(self: HashSet<V>, refinement: Refinement<V, U>): HashSet<U>
518 <V>(self: HashSet<V>, predicate: Predicate<V>): HashSet<V>
519 }
520>(2, internal.filter)
521
522/**
523 * Checks whether at least one value in the HashSet satisfies the predicate.
524 *
525 * **Example** (Testing whether some values match)
526 *
527 * ```ts
528 * import { HashSet } from "effect"
529 *
530 * const numbers = HashSet.make(1, 2, 3, 4, 5)
531 *
532 * console.log(HashSet.some(numbers, (n) => n > 3)) // true
533 * console.log(HashSet.some(numbers, (n) => n > 10)) // false
534 *
535 * const empty = HashSet.empty<number>()
536 * console.log(HashSet.some(empty, (n) => n > 0)) // false
537 * ```
538 *
539 * @category elements
540 * @since 2.0.0
541 */
542export const some: {
543 <V>(predicate: Predicate<V>): (self: HashSet<V>) => boolean
544 <V>(self: HashSet<V>, predicate: Predicate<V>): boolean
545} = Dual.dual<
546 <V>(predicate: Predicate<V>) => (self: HashSet<V>) => boolean,
547 <V>(self: HashSet<V>, predicate: Predicate<V>) => boolean
548>(2, internal.some)
549
550/**
551 * Checks whether all values in the HashSet satisfy the predicate.
552 *
553 * **Example** (Testing whether every value matches)
554 *
555 * ```ts
556 * import { HashSet } from "effect"
557 *
558 * const numbers = HashSet.make(2, 4, 6, 8)
559 *
560 * console.log(HashSet.every(numbers, (n) => n % 2 === 0)) // true
561 * console.log(HashSet.every(numbers, (n) => n > 5)) // false
562 *
563 * const empty = HashSet.empty<number>()
564 * console.log(HashSet.every(empty, (n) => n > 0)) // true (vacuously true)
565 * ```
566 *
567 * @category elements
568 * @since 2.0.0
569 */
570export const every: {
571 <V>(predicate: Predicate<V>): (self: HashSet<V>) => boolean
572 <V>(self: HashSet<V>, predicate: Predicate<V>): boolean
573} = Dual.dual<
574 <V>(predicate: Predicate<V>) => (self: HashSet<V>) => boolean,
575 <V>(self: HashSet<V>, predicate: Predicate<V>) => boolean
576>(2, internal.every)
577
578/**
579 * Reduces the HashSet to a single value by iterating through the values and applying an accumulator function.
580 *
581 * **Example** (Reducing HashSet values)
582 *
583 * ```ts
584 * import { HashSet } from "effect"
585 *
586 * const numbers = HashSet.make(1, 2, 3, 4, 5)
587 * const sum = HashSet.reduce(numbers, 0, (acc, n) => acc + n)
588 *
589 * console.log(sum) // 15
590 *
591 * const strings = HashSet.make("a", "b", "c")
592 * const concatenated = HashSet.reduce(strings, "", (acc, s) => acc + s)
593 * console.log(concatenated) // Order may vary: "abc", "bac", etc.
594 * ```
595 *
596 * @category folding
597 * @since 2.0.0
598 */
599export const reduce: {
600 <V, U>(zero: U, f: (accumulator: U, value: V) => U): (self: HashSet<V>) => U
601 <V, U>(self: HashSet<V>, zero: U, f: (accumulator: U, value: V) => U): U
602} = Dual.dual<
603 <V, U>(zero: U, f: (accumulator: U, value: V) => U) => (self: HashSet<V>) => U,
604 <V, U>(self: HashSet<V>, zero: U, f: (accumulator: U, value: V) => U) => U
605>(3, internal.reduce)