effect 3.14.2 → 3.14.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Effect.js +1 -1
- package/dist/cjs/Function.js +33 -12
- package/dist/cjs/Function.js.map +1 -1
- package/dist/cjs/HashSet.js +1038 -7
- package/dist/cjs/HashSet.js.map +1 -1
- package/dist/cjs/internal/version.js +1 -1
- package/dist/dts/Data.d.ts +4 -4
- package/dist/dts/Data.d.ts.map +1 -1
- package/dist/dts/Effect.d.ts +1 -1
- package/dist/dts/Function.d.ts +99 -36
- package/dist/dts/Function.d.ts.map +1 -1
- package/dist/dts/HashSet.d.ts +1824 -155
- package/dist/dts/HashSet.d.ts.map +1 -1
- package/dist/dts/index.d.ts +250 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/esm/Effect.js +1 -1
- package/dist/esm/Function.js +33 -12
- package/dist/esm/Function.js.map +1 -1
- package/dist/esm/HashSet.js +1038 -7
- package/dist/esm/HashSet.js.map +1 -1
- package/dist/esm/index.js +250 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/version.js +1 -1
- package/package.json +1 -1
- package/src/Data.ts +4 -4
- package/src/Effect.ts +1 -1
- package/src/Function.ts +99 -36
- package/src/HashSet.ts +1878 -163
- package/src/index.ts +250 -0
- package/src/internal/version.ts +1 -1
package/dist/esm/HashSet.js
CHANGED
|
@@ -1,9 +1,260 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* # HashSet
|
|
3
|
+
*
|
|
4
|
+
* An immutable `HashSet` provides a collection of unique values with efficient
|
|
5
|
+
* lookup, insertion and removal. Once created, a `HashSet` cannot be modified;
|
|
6
|
+
* any operation that would alter the set instead returns a new `HashSet` with
|
|
7
|
+
* the changes. This immutability offers benefits like predictable state
|
|
8
|
+
* management and easier reasoning about your code.
|
|
9
|
+
*
|
|
10
|
+
* ## What Problem Does It Solve?
|
|
11
|
+
*
|
|
12
|
+
* `HashSet` solves the problem of maintaining an unsorted collection where each
|
|
13
|
+
* value appears exactly once, with fast operations for checking membership and
|
|
14
|
+
* adding/removing values.
|
|
15
|
+
*
|
|
16
|
+
* ## When to Use
|
|
17
|
+
*
|
|
18
|
+
* Use `HashSet` when you need:
|
|
19
|
+
*
|
|
20
|
+
* - A collection with no duplicate values
|
|
21
|
+
* - Efficient membership testing (**`O(1)`** average complexity)
|
|
22
|
+
* - Set operations like union, intersection, and difference
|
|
23
|
+
* - An immutable data structure that preserves functional programming patterns
|
|
24
|
+
*
|
|
25
|
+
* ## Advanced Features
|
|
26
|
+
*
|
|
27
|
+
* HashSet provides operations for:
|
|
28
|
+
*
|
|
29
|
+
* - Transforming sets with map and flatMap
|
|
30
|
+
* - Filtering elements with filter
|
|
31
|
+
* - Combining sets with union, intersection and difference
|
|
32
|
+
* - Performance optimizations via mutable operations in controlled contexts
|
|
33
|
+
*
|
|
34
|
+
* ## Performance Characteristics
|
|
35
|
+
*
|
|
36
|
+
* - **Lookup** operations ({@linkcode HashSet.has}): **`O(1)`** average time
|
|
37
|
+
* complexity
|
|
38
|
+
* - **Insertion** operations ({@linkcode HashSet.add}): **`O(1)`** average time
|
|
39
|
+
* complexity
|
|
40
|
+
* - **Removal** operations ({@linkcode HashSet.remove}): **`O(1)`** average time
|
|
41
|
+
* complexity
|
|
42
|
+
* - **Set** operations ({@linkcode HashSet.union},
|
|
43
|
+
* {@linkcode HashSet.intersection}): **`O(n)`** where n is the size of the
|
|
44
|
+
* smaller set
|
|
45
|
+
* - **Iteration**: **`O(n)`** where n is the size of the set
|
|
46
|
+
*
|
|
47
|
+
* The HashSet data structure implements the following traits:
|
|
48
|
+
*
|
|
49
|
+
* - {@link Iterable}: allows iterating over the values in the set
|
|
50
|
+
* - {@link Equal}: allows comparing two sets for value-based equality
|
|
51
|
+
* - {@link Pipeable}: allows chaining operations with the pipe operator
|
|
52
|
+
* - {@link Inspectable}: allows inspecting the contents of the set
|
|
53
|
+
*
|
|
54
|
+
* ## Operations Reference
|
|
55
|
+
*
|
|
56
|
+
* | Category | Operation | Description | Complexity |
|
|
57
|
+
* | ------------ | -------------------------------- | ------------------------------------------- | ---------- |
|
|
58
|
+
* | constructors | {@linkcode HashSet.empty} | Creates an empty HashSet | O(1) |
|
|
59
|
+
* | constructors | {@linkcode HashSet.fromIterable} | Creates a HashSet from an iterable | O(n) |
|
|
60
|
+
* | constructors | {@linkcode HashSet.make} | Creates a HashSet from multiple values | O(n) |
|
|
61
|
+
* | | | | |
|
|
62
|
+
* | elements | {@linkcode HashSet.has} | Checks if a value exists in the set | O(1) avg |
|
|
63
|
+
* | elements | {@linkcode HashSet.some} | Checks if any element satisfies a predicate | O(n) |
|
|
64
|
+
* | elements | {@linkcode HashSet.every} | Checks if all elements satisfy a predicate | O(n) |
|
|
65
|
+
* | elements | {@linkcode HashSet.isSubset} | Checks if a set is a subset of another | O(n) |
|
|
66
|
+
* | | | | |
|
|
67
|
+
* | getters | {@linkcode HashSet.values} | Gets an iterator of all values | O(1) |
|
|
68
|
+
* | getters | {@linkcode HashSet.toValues} | Gets an array of all values | O(n) |
|
|
69
|
+
* | getters | {@linkcode HashSet.size} | Gets the number of elements | O(1) |
|
|
70
|
+
* | | | | |
|
|
71
|
+
* | mutations | {@linkcode HashSet.add} | Adds a value to the set | O(1) avg |
|
|
72
|
+
* | mutations | {@linkcode HashSet.remove} | Removes a value from the set | O(1) avg |
|
|
73
|
+
* | mutations | {@linkcode HashSet.toggle} | Toggles a value's presence | O(1) avg |
|
|
74
|
+
* | | | | |
|
|
75
|
+
* | operations | {@linkcode HashSet.difference} | Computes set difference (A - B) | O(n) |
|
|
76
|
+
* | operations | {@linkcode HashSet.intersection} | Computes set intersection (A ∩ B) | O(n) |
|
|
77
|
+
* | operations | {@linkcode HashSet.union} | Computes set union (A ∪ B) | O(n) |
|
|
78
|
+
* | | | | |
|
|
79
|
+
* | mapping | {@linkcode HashSet.map} | Transforms each element | O(n) |
|
|
80
|
+
* | | | | |
|
|
81
|
+
* | sequencing | {@linkcode HashSet.flatMap} | Transforms and flattens elements | O(n) |
|
|
82
|
+
* | | | | |
|
|
83
|
+
* | traversing | {@linkcode HashSet.forEach} | Applies a function to each element | O(n) |
|
|
84
|
+
* | | | | |
|
|
85
|
+
* | folding | {@linkcode HashSet.reduce} | Reduces the set to a single value | O(n) |
|
|
86
|
+
* | | | | |
|
|
87
|
+
* | filtering | {@linkcode HashSet.filter} | Keeps elements that satisfy a predicate | O(n) |
|
|
88
|
+
* | | | | |
|
|
89
|
+
* | partitioning | {@linkcode HashSet.partition} | Splits into two sets by a predicate | O(n) |
|
|
90
|
+
*
|
|
91
|
+
* ## Notes
|
|
92
|
+
*
|
|
93
|
+
* ### Composability with the Effect Ecosystem:
|
|
94
|
+
*
|
|
95
|
+
* This `HashSet` is designed to work seamlessly within the Effect ecosystem. It
|
|
96
|
+
* implements the {@link Iterable}, {@link Equal}, {@link Pipeable}, and
|
|
97
|
+
* {@link Inspectable} traits from Effect. This ensures compatibility with other
|
|
98
|
+
* Effect data structures and functionalities. For example, you can easily use
|
|
99
|
+
* Effect's `pipe` method to chain operations on the `HashSet`.
|
|
100
|
+
*
|
|
101
|
+
* **Equality of Elements with Effect's {@link Equal `Equal`} Trait:**
|
|
102
|
+
*
|
|
103
|
+
* This `HashSet` relies on Effect's {@link Equal} trait to determine the
|
|
104
|
+
* uniqueness of elements within the set. The way equality is checked depends on
|
|
105
|
+
* the type of the elements:
|
|
106
|
+
*
|
|
107
|
+
* - **Primitive Values:** For primitive JavaScript values like strings, numbers,
|
|
108
|
+
* booleans, `null`, and `undefined`, equality is determined by their value
|
|
109
|
+
* (similar to the `===` operator).
|
|
110
|
+
* - **Objects and Custom Types:** For objects and other custom types, equality is
|
|
111
|
+
* determined by whether those types implement the {@link Equal} interface
|
|
112
|
+
* themselves. If an element type implements `Equal`, the `HashSet` will
|
|
113
|
+
* delegate to that implementation to perform the equality check. This allows
|
|
114
|
+
* you to define custom logic for determining when two instances of your
|
|
115
|
+
* objects should be considered equal based on their properties, rather than
|
|
116
|
+
* just their object identity.
|
|
117
|
+
*
|
|
118
|
+
* ```ts
|
|
119
|
+
* import { Equal, Hash, HashSet } from "effect"
|
|
120
|
+
*
|
|
121
|
+
* class Person implements Equal.Equal {
|
|
122
|
+
* constructor(
|
|
123
|
+
* readonly id: number, // Unique identifier
|
|
124
|
+
* readonly name: string,
|
|
125
|
+
* readonly age: number
|
|
126
|
+
* ) {}
|
|
127
|
+
*
|
|
128
|
+
* // Define equality based on id, name, and age
|
|
129
|
+
* [Equal.symbol](that: Equal.Equal): boolean {
|
|
130
|
+
* if (that instanceof Person) {
|
|
131
|
+
* return (
|
|
132
|
+
* Equal.equals(this.id, that.id) &&
|
|
133
|
+
* Equal.equals(this.name, that.name) &&
|
|
134
|
+
* Equal.equals(this.age, that.age)
|
|
135
|
+
* )
|
|
136
|
+
* }
|
|
137
|
+
* return false
|
|
138
|
+
* }
|
|
139
|
+
*
|
|
140
|
+
* // Generate a hash code based on the unique id
|
|
141
|
+
* [Hash.symbol](): number {
|
|
142
|
+
* return Hash.hash(this.id)
|
|
143
|
+
* }
|
|
144
|
+
* }
|
|
145
|
+
*
|
|
146
|
+
* // Creating a HashSet with objects that implement the Equal interface
|
|
147
|
+
* const set = HashSet.empty().pipe(
|
|
148
|
+
* HashSet.add(new Person(1, "Alice", 30)),
|
|
149
|
+
* HashSet.add(new Person(1, "Alice", 30))
|
|
150
|
+
* )
|
|
151
|
+
*
|
|
152
|
+
* // HashSet recognizes them as equal, so only one element is stored
|
|
153
|
+
* console.log(HashSet.size(set))
|
|
154
|
+
* // Output: 1
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* **Simplifying Equality and Hashing with `Data` and `Schema`:**
|
|
158
|
+
*
|
|
159
|
+
* Effect's {@link Data} and {@link Schema `Schema.Data`} modules offer powerful
|
|
160
|
+
* ways to automatically handle the implementation of both the {@link Equal} and
|
|
161
|
+
* {@link Hash} traits for your custom data structures.
|
|
162
|
+
*
|
|
163
|
+
* - **`Data` Module:** By using constructors like `Data.struct`, `Data.tuple`,
|
|
164
|
+
* `Data.array`, or `Data.case` to define your data types, Effect
|
|
165
|
+
* automatically generates the necessary implementations for value-based
|
|
166
|
+
* equality and consistent hashing. This significantly reduces boilerplate and
|
|
167
|
+
* ensures correctness.
|
|
168
|
+
*
|
|
169
|
+
* ```ts
|
|
170
|
+
* import { HashSet, Data, Equal } from "effect"
|
|
171
|
+
* import assert from "node:assert/strict"
|
|
172
|
+
*
|
|
173
|
+
* // Data.* implements the `Equal` traits for us
|
|
174
|
+
* const person1 = Data.struct({ id: 1, name: "Alice", age: 30 })
|
|
175
|
+
* const person2 = Data.struct({ id: 1, name: "Alice", age: 30 })
|
|
176
|
+
*
|
|
177
|
+
* assert(Equal.equals(person1, person2))
|
|
178
|
+
*
|
|
179
|
+
* const set = HashSet.empty().pipe(
|
|
180
|
+
* HashSet.add(person1),
|
|
181
|
+
* HashSet.add(person2)
|
|
182
|
+
* )
|
|
183
|
+
*
|
|
184
|
+
* // HashSet recognizes them as equal, so only one element is stored
|
|
185
|
+
* console.log(HashSet.size(set)) // Output: 1
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* - **`Schema` Module:** When defining data schemas using the {@link Schema}
|
|
189
|
+
* module, you can use `Schema.Data` to automatically include the `Equal` and
|
|
190
|
+
* `Hash` traits in the decoded objects. This is particularly important when
|
|
191
|
+
* working with `HashSet`. **For decoded objects to be correctly recognized as
|
|
192
|
+
* equal within a `HashSet`, ensure that the schema for those objects is
|
|
193
|
+
* defined using `Schema.Data`.**
|
|
194
|
+
*
|
|
195
|
+
* ```ts
|
|
196
|
+
* import { Equal, HashSet, Schema } from "effect"
|
|
197
|
+
* import assert from "node:assert/strict"
|
|
198
|
+
*
|
|
199
|
+
* // Schema.Data implements the `Equal` traits for us
|
|
200
|
+
* const PersonSchema = Schema.Data(
|
|
201
|
+
* Schema.Struct({
|
|
202
|
+
* id: Schema.Number,
|
|
203
|
+
* name: Schema.String,
|
|
204
|
+
* age: Schema.Number
|
|
205
|
+
* })
|
|
206
|
+
* )
|
|
207
|
+
*
|
|
208
|
+
* const Person = Schema.decode(PersonSchema)
|
|
209
|
+
*
|
|
210
|
+
* const person1 = Person({ id: 1, name: "Alice", age: 30 })
|
|
211
|
+
* const person2 = Person({ id: 1, name: "Alice", age: 30 })
|
|
212
|
+
*
|
|
213
|
+
* assert(Equal.equals(person1, person2)) // Output: true
|
|
214
|
+
*
|
|
215
|
+
* const set = HashSet.empty().pipe(
|
|
216
|
+
* HashSet.add(person1),
|
|
217
|
+
* HashSet.add(person2)
|
|
218
|
+
* )
|
|
219
|
+
*
|
|
220
|
+
* // HashSet thanks to Schema.Data implementation of the `Equal` trait, recognizes the two Person as equal, so only one element is stored
|
|
221
|
+
* console.log(HashSet.size(set)) // Output: 1
|
|
222
|
+
* ```
|
|
223
|
+
*
|
|
224
|
+
* ### Interoperability with the JavaScript Runtime:
|
|
225
|
+
*
|
|
226
|
+
* To interoperate with the regular JavaScript runtime, Effect's `HashSet`
|
|
227
|
+
* provides methods to access its elements in formats readily usable by
|
|
228
|
+
* JavaScript APIs: {@link values `HashSet.values`},
|
|
229
|
+
* {@link toValues `HashSet.toValues`}
|
|
230
|
+
*
|
|
231
|
+
* ```ts
|
|
232
|
+
* import { HashSet } from "effect"
|
|
233
|
+
*
|
|
234
|
+
* const hashSet: HashSet.HashSet<number> = HashSet.make(1, 2, 3)
|
|
235
|
+
*
|
|
236
|
+
* // Using HashSet.values to convert HashSet.HashSet<A> to IterableIterator<A>
|
|
237
|
+
* const iterable: IterableIterator<number> = HashSet.values(hashSet)
|
|
238
|
+
*
|
|
239
|
+
* console.log(...iterable) // Logs: 1 2 3
|
|
240
|
+
*
|
|
241
|
+
* // Using HashSet.toValues to convert HashSet.HashSet<A> to Array<A>
|
|
242
|
+
* const array: Array<number> = HashSet.toValues(hashSet)
|
|
243
|
+
*
|
|
244
|
+
* console.log(array) // Logs: [ 1, 2, 3 ]
|
|
245
|
+
* ```
|
|
246
|
+
*
|
|
247
|
+
* Be mindful of performance implications (both time and space complexity) when
|
|
248
|
+
* frequently converting between Effect's immutable HashSet and mutable
|
|
249
|
+
* JavaScript data structures, especially for large collections.
|
|
250
|
+
*
|
|
251
|
+
* @module HashSet
|
|
2
252
|
* @since 2.0.0
|
|
3
253
|
*/
|
|
4
254
|
import * as HS from "./internal/hashSet.js";
|
|
5
255
|
const TypeId = HS.HashSetTypeId;
|
|
6
256
|
/**
|
|
257
|
+
* @memberof HashSet
|
|
7
258
|
* @since 2.0.0
|
|
8
259
|
* @category refinements
|
|
9
260
|
*/
|
|
@@ -11,43 +262,323 @@ export const isHashSet = HS.isHashSet;
|
|
|
11
262
|
/**
|
|
12
263
|
* Creates an empty `HashSet`.
|
|
13
264
|
*
|
|
265
|
+
* Time complexity: **`O(1)`**
|
|
266
|
+
*
|
|
267
|
+
* @memberof HashSet
|
|
14
268
|
* @since 2.0.0
|
|
15
269
|
* @category constructors
|
|
270
|
+
* @example
|
|
271
|
+
*
|
|
272
|
+
* ```ts
|
|
273
|
+
* import { HashSet, pipe } from "effect"
|
|
274
|
+
*
|
|
275
|
+
* console.log(
|
|
276
|
+
* pipe(
|
|
277
|
+
* // Provide a type argument to create a HashSet of a specific type
|
|
278
|
+
* HashSet.empty<number>(),
|
|
279
|
+
* HashSet.add(1),
|
|
280
|
+
* HashSet.add(1), // Notice the duplicate
|
|
281
|
+
* HashSet.add(2),
|
|
282
|
+
* HashSet.toValues
|
|
283
|
+
* )
|
|
284
|
+
* ) // Output: [1, 2]
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* @see Other `HashSet` constructors are {@link make} {@link fromIterable}
|
|
16
288
|
*/
|
|
17
289
|
export const empty = HS.empty;
|
|
18
290
|
/**
|
|
19
291
|
* Creates a new `HashSet` from an iterable collection of values.
|
|
20
292
|
*
|
|
293
|
+
* Time complexity: **`O(n)`** where n is the number of elements in the iterable
|
|
294
|
+
*
|
|
295
|
+
* @memberof HashSet
|
|
21
296
|
* @since 2.0.0
|
|
22
297
|
* @category constructors
|
|
298
|
+
* @example Creating a HashSet from an {@link Array}
|
|
299
|
+
*
|
|
300
|
+
* ```ts
|
|
301
|
+
* import { HashSet, pipe } from "effect"
|
|
302
|
+
*
|
|
303
|
+
* console.log(
|
|
304
|
+
* pipe(
|
|
305
|
+
* [1, 2, 3, 4, 5, 1, 2, 3], // Array<number> is an Iterable<number>; Note the duplicates.
|
|
306
|
+
* HashSet.fromIterable,
|
|
307
|
+
* HashSet.toValues
|
|
308
|
+
* )
|
|
309
|
+
* ) // Output: [1, 2, 3, 4, 5]
|
|
310
|
+
* ```
|
|
311
|
+
*
|
|
312
|
+
* @example Creating a HashSet from a {@link Set}
|
|
313
|
+
*
|
|
314
|
+
* ```ts
|
|
315
|
+
* import { HashSet, pipe } from "effect"
|
|
316
|
+
*
|
|
317
|
+
* console.log(
|
|
318
|
+
* pipe(
|
|
319
|
+
* new Set(["apple", "banana", "orange", "apple"]), // Set<string> is an Iterable<string>
|
|
320
|
+
* HashSet.fromIterable,
|
|
321
|
+
* HashSet.toValues
|
|
322
|
+
* )
|
|
323
|
+
* ) // Output: ["apple", "banana", "orange"]
|
|
324
|
+
* ```
|
|
325
|
+
*
|
|
326
|
+
* @example Creating a HashSet from a {@link Generator}
|
|
327
|
+
*
|
|
328
|
+
* ```ts
|
|
329
|
+
* import { HashSet } from "effect"
|
|
330
|
+
*
|
|
331
|
+
* // Generator functions return iterables
|
|
332
|
+
* function* fibonacci(n: number): Generator<number, void, unknown> {
|
|
333
|
+
* let [a, b] = [0, 1]
|
|
334
|
+
* for (let i = 0; i < n; i++) {
|
|
335
|
+
* yield a
|
|
336
|
+
* ;[a, b] = [b, a + b]
|
|
337
|
+
* }
|
|
338
|
+
* }
|
|
339
|
+
*
|
|
340
|
+
* // Create a HashSet from the first 10 Fibonacci numbers
|
|
341
|
+
* const fibonacciSet = HashSet.fromIterable(fibonacci(10))
|
|
342
|
+
*
|
|
343
|
+
* console.log(HashSet.toValues(fibonacciSet))
|
|
344
|
+
* // Outputs: [0, 1, 2, 3, 5, 8, 13, 21, 34] but in unsorted order
|
|
345
|
+
* ```
|
|
346
|
+
*
|
|
347
|
+
* @example Creating a HashSet from another {@link HashSet}
|
|
348
|
+
*
|
|
349
|
+
* ```ts
|
|
350
|
+
* import { HashSet, pipe } from "effect"
|
|
351
|
+
*
|
|
352
|
+
* console.log(
|
|
353
|
+
* pipe(
|
|
354
|
+
* // since HashSet implements the Iterable interface, we can use it to create a new HashSet
|
|
355
|
+
* HashSet.make(1, 2, 3, 4),
|
|
356
|
+
* HashSet.fromIterable,
|
|
357
|
+
* HashSet.toValues // turns the HashSet back into an array
|
|
358
|
+
* )
|
|
359
|
+
* ) // Output: [1, 2, 3, 4]
|
|
360
|
+
* ```
|
|
361
|
+
*
|
|
362
|
+
* @example Creating a HashSet from other Effect's data structures like
|
|
363
|
+
* {@link Chunk}
|
|
364
|
+
*
|
|
365
|
+
* ```ts
|
|
366
|
+
* import { Chunk, HashSet, pipe } from "effect"
|
|
367
|
+
*
|
|
368
|
+
* console.log(
|
|
369
|
+
* pipe(
|
|
370
|
+
* Chunk.make(1, 2, 3, 4), // Iterable<number>
|
|
371
|
+
* HashSet.fromIterable,
|
|
372
|
+
* HashSet.toValues // turns the HashSet back into an array
|
|
373
|
+
* )
|
|
374
|
+
* ) // Outputs: [1, 2, 3, 4]
|
|
375
|
+
* ```
|
|
376
|
+
*
|
|
377
|
+
* @see Other `HashSet` constructors are {@link empty} {@link make}
|
|
23
378
|
*/
|
|
24
379
|
export const fromIterable = HS.fromIterable;
|
|
25
380
|
/**
|
|
26
381
|
* Construct a new `HashSet` from a variable number of values.
|
|
27
382
|
*
|
|
383
|
+
* Time complexity: **`O(n)`** where n is the number of elements
|
|
384
|
+
*
|
|
385
|
+
* @memberof HashSet
|
|
28
386
|
* @since 2.0.0
|
|
29
387
|
* @category constructors
|
|
388
|
+
* @example
|
|
389
|
+
*
|
|
390
|
+
* ```ts
|
|
391
|
+
* import { Equal, Hash, HashSet, pipe } from "effect"
|
|
392
|
+
* import assert from "node:assert/strict"
|
|
393
|
+
*
|
|
394
|
+
* class Character implements Equal.Equal {
|
|
395
|
+
* readonly name: string
|
|
396
|
+
* readonly trait: string
|
|
397
|
+
*
|
|
398
|
+
* constructor(name: string, trait: string) {
|
|
399
|
+
* this.name = name
|
|
400
|
+
* this.trait = trait
|
|
401
|
+
* }
|
|
402
|
+
*
|
|
403
|
+
* // Define equality based on name, and trait
|
|
404
|
+
* [Equal.symbol](that: Equal.Equal): boolean {
|
|
405
|
+
* if (that instanceof Character) {
|
|
406
|
+
* return (
|
|
407
|
+
* Equal.equals(this.name, that.name) &&
|
|
408
|
+
* Equal.equals(this.trait, that.trait)
|
|
409
|
+
* )
|
|
410
|
+
* }
|
|
411
|
+
* return false
|
|
412
|
+
* }
|
|
413
|
+
*
|
|
414
|
+
* // Generate a hash code based on the sum of the character's name and trait
|
|
415
|
+
* [Hash.symbol](): number {
|
|
416
|
+
* return Hash.hash(this.name + this.trait)
|
|
417
|
+
* }
|
|
418
|
+
*
|
|
419
|
+
* static readonly of = (name: string, trait: string): Character => {
|
|
420
|
+
* return new Character(name, trait)
|
|
421
|
+
* }
|
|
422
|
+
* }
|
|
423
|
+
*
|
|
424
|
+
* assert.strictEqual(
|
|
425
|
+
* Equal.equals(
|
|
426
|
+
* HashSet.make(
|
|
427
|
+
* Character.of("Alice", "Curious"),
|
|
428
|
+
* Character.of("Alice", "Curious"),
|
|
429
|
+
* Character.of("White Rabbit", "Always late"),
|
|
430
|
+
* Character.of("Mad Hatter", "Tea enthusiast")
|
|
431
|
+
* ),
|
|
432
|
+
* // Is the same as adding each character to an empty set
|
|
433
|
+
* pipe(
|
|
434
|
+
* HashSet.empty(),
|
|
435
|
+
* HashSet.add(Character.of("Alice", "Curious")),
|
|
436
|
+
* HashSet.add(Character.of("Alice", "Curious")), // Alice tried to attend twice!
|
|
437
|
+
* HashSet.add(Character.of("White Rabbit", "Always late")),
|
|
438
|
+
* HashSet.add(Character.of("Mad Hatter", "Tea enthusiast"))
|
|
439
|
+
* )
|
|
440
|
+
* ),
|
|
441
|
+
* true,
|
|
442
|
+
* "`HashSet.make` and `HashSet.empty() + HashSet.add()` should be equal"
|
|
443
|
+
* )
|
|
444
|
+
*
|
|
445
|
+
* assert.strictEqual(
|
|
446
|
+
* Equal.equals(
|
|
447
|
+
* HashSet.make(
|
|
448
|
+
* Character.of("Alice", "Curious"),
|
|
449
|
+
* Character.of("Alice", "Curious"),
|
|
450
|
+
* Character.of("White Rabbit", "Always late"),
|
|
451
|
+
* Character.of("Mad Hatter", "Tea enthusiast")
|
|
452
|
+
* ),
|
|
453
|
+
* HashSet.fromIterable([
|
|
454
|
+
* Character.of("Alice", "Curious"),
|
|
455
|
+
* Character.of("Alice", "Curious"),
|
|
456
|
+
* Character.of("White Rabbit", "Always late"),
|
|
457
|
+
* Character.of("Mad Hatter", "Tea enthusiast")
|
|
458
|
+
* ])
|
|
459
|
+
* ),
|
|
460
|
+
* true,
|
|
461
|
+
* "`HashSet.make` and `HashSet.fromIterable` should be equal"
|
|
462
|
+
* )
|
|
463
|
+
* ```
|
|
464
|
+
*
|
|
465
|
+
* @see Other `HashSet` constructors are {@linkcode fromIterable} {@linkcode empty}
|
|
30
466
|
*/
|
|
31
467
|
export const make = HS.make;
|
|
32
468
|
/**
|
|
33
469
|
* Checks if the specified value exists in the `HashSet`.
|
|
34
470
|
*
|
|
471
|
+
* Time complexity: **`O(1)`** average
|
|
472
|
+
*
|
|
473
|
+
* @memberof HashSet
|
|
35
474
|
* @since 2.0.0
|
|
36
475
|
* @category elements
|
|
476
|
+
* @example **syntax**
|
|
477
|
+
*
|
|
478
|
+
* ```ts
|
|
479
|
+
* import { HashSet, pipe } from "effect"
|
|
480
|
+
*
|
|
481
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
482
|
+
* pipe(HashSet.make(0, 1, 2), HashSet.has(3)) // false
|
|
483
|
+
*
|
|
484
|
+
* // or piped with the pipe function
|
|
485
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.has(3)) // false
|
|
486
|
+
*
|
|
487
|
+
* // or with `data-first` API
|
|
488
|
+
* HashSet.has(HashSet.make(0, 1, 2), 3) // false
|
|
489
|
+
* ```
|
|
490
|
+
*
|
|
491
|
+
* @returns A `boolean` signaling the presence of the value in the HashSet
|
|
492
|
+
* @see Other `HashSet` elements are {@link some} {@link every} {@link isSubset}
|
|
37
493
|
*/
|
|
38
494
|
export const has = HS.has;
|
|
39
495
|
/**
|
|
40
496
|
* Check if a predicate holds true for some `HashSet` element.
|
|
41
497
|
*
|
|
498
|
+
* Time complexity: **`O(n)`** where n is the number of elements in the set
|
|
499
|
+
*
|
|
500
|
+
* @memberof HashSet
|
|
42
501
|
* @since 2.0.0
|
|
43
502
|
* @category elements
|
|
503
|
+
* @example **syntax**
|
|
504
|
+
*
|
|
505
|
+
* ```ts
|
|
506
|
+
* import { HashSet, pipe } from "effect"
|
|
507
|
+
*
|
|
508
|
+
* const set: HashSet.HashSet<number> = HashSet.make(0, 1, 2)
|
|
509
|
+
*
|
|
510
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
511
|
+
* pipe(
|
|
512
|
+
* set,
|
|
513
|
+
* HashSet.some((n) => n > 0)
|
|
514
|
+
* ) // true
|
|
515
|
+
*
|
|
516
|
+
* // or piped with the pipe function
|
|
517
|
+
* set.pipe(HashSet.some((n) => n > 0)) // true
|
|
518
|
+
*
|
|
519
|
+
* // or with `data-first` API
|
|
520
|
+
* HashSet.some(set, (n) => n > 0) // true
|
|
521
|
+
* ```
|
|
522
|
+
*
|
|
523
|
+
* @see Other `HashSet` elements are {@link has} {@link every} {@link isSubset}
|
|
44
524
|
*/
|
|
45
525
|
export const some = HS.some;
|
|
46
526
|
/**
|
|
47
527
|
* Check if a predicate holds true for every `HashSet` element.
|
|
48
528
|
*
|
|
529
|
+
* Time complexity is **`O(n)`** as it needs to traverse the whole HashSet
|
|
530
|
+
* collection
|
|
531
|
+
*
|
|
532
|
+
* @memberof HashSet
|
|
49
533
|
* @since 2.0.0
|
|
50
534
|
* @category elements
|
|
535
|
+
* @example **syntax** with {@link Refinement}
|
|
536
|
+
*
|
|
537
|
+
* ```ts
|
|
538
|
+
* import { HashSet, pipe, Predicate } from "effect"
|
|
539
|
+
*
|
|
540
|
+
* const numberOrString = HashSet.make(1, "1", "one", "uno")
|
|
541
|
+
*
|
|
542
|
+
* // with `data-last`, a.k.a. `pipeable` API and `Refinement`
|
|
543
|
+
* pipe(
|
|
544
|
+
* numberOrString, // HashSet.HashSet<number | string>
|
|
545
|
+
* HashSet.every(Predicate.isString)
|
|
546
|
+
* ) // HashSet.HashSet<string>
|
|
547
|
+
*
|
|
548
|
+
* // or piped with the pipe function and `Refinement`
|
|
549
|
+
* numberOrString // HashSet.HashSet<number | string>
|
|
550
|
+
* .pipe(HashSet.every(Predicate.isString)) // HashSet.HashSet<string>
|
|
551
|
+
*
|
|
552
|
+
* // or with `data-first` API and `Refinement`
|
|
553
|
+
* HashSet.every(
|
|
554
|
+
* numberOrString, // HashSet.HashSet<number | string>
|
|
555
|
+
* Predicate.isString
|
|
556
|
+
* ) // HashSet.HashSet<string>
|
|
557
|
+
* ```
|
|
558
|
+
*
|
|
559
|
+
* @example **syntax** with {@link Predicate}
|
|
560
|
+
*
|
|
561
|
+
* ```ts
|
|
562
|
+
* import { HashSet, pipe } from "effect"
|
|
563
|
+
*
|
|
564
|
+
* const set = HashSet.make(1, 2, 3)
|
|
565
|
+
*
|
|
566
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
567
|
+
* pipe(
|
|
568
|
+
* set,
|
|
569
|
+
* HashSet.every((n) => n >= 0)
|
|
570
|
+
* ) // true
|
|
571
|
+
*
|
|
572
|
+
* // or piped with the pipe function
|
|
573
|
+
* set.pipe(HashSet.every((n) => n >= 0)) // true
|
|
574
|
+
*
|
|
575
|
+
* // or with `data-first` API
|
|
576
|
+
* HashSet.every(set, (n) => n >= 0) // true
|
|
577
|
+
* ```
|
|
578
|
+
*
|
|
579
|
+
* @returns A boolean once it has evaluated that whole collection fulfill the
|
|
580
|
+
* Predicate function
|
|
581
|
+
* @see Other `HashSet` elements are {@link has} {@link some} {@link isSubset}
|
|
51
582
|
*/
|
|
52
583
|
export const every = HS.every;
|
|
53
584
|
/**
|
|
@@ -56,89 +587,393 @@ export const every = HS.every;
|
|
|
56
587
|
*
|
|
57
588
|
* **NOTE**: the hash and equal of both sets must be the same.
|
|
58
589
|
*
|
|
590
|
+
* Time complexity analysis is of **`O(n)`**
|
|
591
|
+
*
|
|
592
|
+
* @memberof HashSet
|
|
59
593
|
* @since 2.0.0
|
|
60
594
|
* @category elements
|
|
595
|
+
* @example **Syntax**
|
|
596
|
+
*
|
|
597
|
+
* ```ts
|
|
598
|
+
* import { HashSet, pipe } from "effect"
|
|
599
|
+
*
|
|
600
|
+
* const set1 = HashSet.make(0, 1)
|
|
601
|
+
* const set2 = HashSet.make(1, 2)
|
|
602
|
+
* const set3 = HashSet.make(0, 1, 2)
|
|
603
|
+
*
|
|
604
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
605
|
+
* pipe(set1, HashSet.isSubset(set2)) // false
|
|
606
|
+
* pipe(set1, HashSet.isSubset(set3)) // true
|
|
607
|
+
*
|
|
608
|
+
* // or piped with the pipe function
|
|
609
|
+
* set1.pipe(HashSet.isSubset(set2)) // false
|
|
610
|
+
* set1.pipe(HashSet.isSubset(set3)) // true
|
|
611
|
+
*
|
|
612
|
+
* // or with `data-first` API
|
|
613
|
+
* HashSet.isSubset(set1, set2) // false
|
|
614
|
+
* HashSet.isSubset(set1, set3) // true)
|
|
615
|
+
* ```
|
|
616
|
+
*
|
|
617
|
+
* @see Other `HashSet` elements are {@link has} {@link some} {@link every}
|
|
61
618
|
*/
|
|
62
619
|
export const isSubset = HS.isSubset;
|
|
63
620
|
/**
|
|
64
621
|
* Returns an `IterableIterator` of the values in the `HashSet`.
|
|
65
622
|
*
|
|
623
|
+
* Time complexity: **`O(1)`**
|
|
624
|
+
*
|
|
625
|
+
* @memberof HashSet
|
|
66
626
|
* @since 2.0.0
|
|
67
627
|
* @category getters
|
|
628
|
+
* @example
|
|
629
|
+
*
|
|
630
|
+
* ```ts
|
|
631
|
+
* import { HashSet, pipe } from "effect"
|
|
632
|
+
*
|
|
633
|
+
* const numberIterable = pipe(
|
|
634
|
+
* HashSet.make(0, 1, 1, 2), // HashSet.HashSet<number>
|
|
635
|
+
* HashSet.values // takes an HashSet<A> and returns an IterableIterator<A>
|
|
636
|
+
* )
|
|
637
|
+
*
|
|
638
|
+
* for (const number of numberIterable) {
|
|
639
|
+
* console.log(number) // it will logs: 0, 1, 2
|
|
640
|
+
* }
|
|
641
|
+
* ```
|
|
642
|
+
*
|
|
643
|
+
* @see Other `HashSet` getters are {@link toValues} {@link size}
|
|
68
644
|
*/
|
|
69
645
|
export const values = HS.values;
|
|
70
646
|
/**
|
|
71
647
|
* Returns an `Array` of the values within the `HashSet`.
|
|
72
648
|
*
|
|
649
|
+
* Time complexity: **`O(n)`** where n is the number of elements in the set
|
|
650
|
+
*
|
|
651
|
+
* @memberof HashSet
|
|
73
652
|
* @since 3.13.0
|
|
74
653
|
* @category getters
|
|
654
|
+
* @example
|
|
655
|
+
*
|
|
656
|
+
* ```ts
|
|
657
|
+
* import { HashSet, pipe } from "effect"
|
|
658
|
+
* import { deepStrictEqual } from "node:assert/strict"
|
|
659
|
+
*
|
|
660
|
+
* deepStrictEqual(
|
|
661
|
+
* pipe(
|
|
662
|
+
* HashSet.make(0, 1, 1, 2), // HashSet<number>
|
|
663
|
+
* HashSet.toValues // takes an HashSet<A> and returns an Array<A>
|
|
664
|
+
* ),
|
|
665
|
+
* Array.of(0, 1, 2)
|
|
666
|
+
* )
|
|
667
|
+
* ```
|
|
668
|
+
*
|
|
669
|
+
* @see Other `HashSet` getters are {@link values} {@link size}
|
|
75
670
|
*/
|
|
76
671
|
export const toValues = self => Array.from(values(self));
|
|
77
672
|
/**
|
|
78
673
|
* Calculates the number of values in the `HashSet`.
|
|
79
674
|
*
|
|
675
|
+
* Time complexity: **`O(1)`**
|
|
676
|
+
*
|
|
677
|
+
* @memberof HashSet
|
|
80
678
|
* @since 2.0.0
|
|
81
679
|
* @category getters
|
|
680
|
+
* @example
|
|
681
|
+
*
|
|
682
|
+
* ```ts
|
|
683
|
+
* import { HashSet, pipe } from "effect"
|
|
684
|
+
* import assert from "node:assert/strict"
|
|
685
|
+
*
|
|
686
|
+
* assert.deepStrictEqual(pipe(HashSet.empty(), HashSet.size), 0)
|
|
687
|
+
*
|
|
688
|
+
* assert.deepStrictEqual(
|
|
689
|
+
* pipe(HashSet.make(1, 2, 2, 3, 4, 3), HashSet.size),
|
|
690
|
+
* 4
|
|
691
|
+
* )
|
|
692
|
+
* ```
|
|
693
|
+
*
|
|
694
|
+
* @see Other `HashSet` getters are {@link values} {@link toValues}
|
|
82
695
|
*/
|
|
83
696
|
export const size = HS.size;
|
|
84
697
|
/**
|
|
85
|
-
*
|
|
698
|
+
* Creates a new mutable version of the `HashSet`
|
|
699
|
+
*
|
|
700
|
+
* When a `HashSet` is mutable, operations like {@link add} and {@link remove}
|
|
701
|
+
* modify the data structure in place instead of creating a new one, which is
|
|
702
|
+
* more efficient when performing multiple operations.
|
|
86
703
|
*
|
|
704
|
+
* @memberof HashSet
|
|
87
705
|
* @since 2.0.0
|
|
706
|
+
* @example
|
|
707
|
+
*
|
|
708
|
+
* ```ts
|
|
709
|
+
* import { HashSet } from "effect"
|
|
710
|
+
* import assert from "node:assert/strict"
|
|
711
|
+
*
|
|
712
|
+
* const UPPER_BOUND = 10_000
|
|
713
|
+
*
|
|
714
|
+
* const immutableSet = HashSet.empty<number>().pipe(HashSet.add(0))
|
|
715
|
+
*
|
|
716
|
+
* // Create a mutable version of the immutableSet
|
|
717
|
+
* const mutableSet = HashSet.beginMutation(immutableSet)
|
|
718
|
+
*
|
|
719
|
+
* for (let i = 1; i < UPPER_BOUND; i++) {
|
|
720
|
+
* // Operations now modify the set in place instead of creating new instances
|
|
721
|
+
* // This is more efficient when making multiple changes
|
|
722
|
+
* const pointerToMutableSet = HashSet.add(mutableSet, i)
|
|
723
|
+
*
|
|
724
|
+
* // the two sets have the same identity, hence `add` is mutating mutableSet and not returning a new HashSet instance
|
|
725
|
+
* assert(Object.is(mutableSet, pointerToMutableSet))
|
|
726
|
+
* assert.equal(HashSet.has(mutableSet, i), true) // `i` is in the mutableSet
|
|
727
|
+
* assert.equal(HashSet.has(immutableSet, i), false) // `i` is not in the immutableSet
|
|
728
|
+
* }
|
|
729
|
+
*
|
|
730
|
+
* const next = UPPER_BOUND + 1
|
|
731
|
+
* // When done, mark the set as immutable again
|
|
732
|
+
* HashSet.endMutation(mutableSet).pipe(
|
|
733
|
+
* HashSet.add(next) // since this returns a new HashSet, it will not be logged as part of the mutableSet
|
|
734
|
+
* )
|
|
735
|
+
* assert.equal(HashSet.has(mutableSet, next), false)
|
|
736
|
+
*
|
|
737
|
+
* console.log(HashSet.toValues(immutableSet)) // [0]
|
|
738
|
+
* console.log(HashSet.toValues(mutableSet).sort((a, b) => a - b)) // [0, 1, 2, 3, ...rest]
|
|
739
|
+
* ```
|
|
740
|
+
*
|
|
741
|
+
* @see Other `HashSet` mutations are {@link add} {@link remove} {@link toggle} {@link endMutation} {@link mutate}
|
|
88
742
|
*/
|
|
89
743
|
export const beginMutation = HS.beginMutation;
|
|
90
744
|
/**
|
|
91
|
-
*
|
|
745
|
+
* Makes the `HashSet` immutable again.
|
|
746
|
+
*
|
|
747
|
+
* After calling `endMutation`, operations like {@link add} and {@link remove}
|
|
748
|
+
* will create new instances of the `HashSet` instead of modifying the existing
|
|
749
|
+
* one.
|
|
92
750
|
*
|
|
751
|
+
* @memberof HashSet
|
|
93
752
|
* @since 2.0.0
|
|
753
|
+
* @example
|
|
754
|
+
*
|
|
755
|
+
* ```ts
|
|
756
|
+
* import { HashSet } from "effect"
|
|
757
|
+
* import assert from "node:assert/strict"
|
|
758
|
+
*
|
|
759
|
+
* // Create a mutable set
|
|
760
|
+
* const mutableSet = HashSet.beginMutation(HashSet.empty<number>())
|
|
761
|
+
*
|
|
762
|
+
* // Add some elements to the mutable set
|
|
763
|
+
* HashSet.add(mutableSet, 1)
|
|
764
|
+
* HashSet.add(mutableSet, 2)
|
|
765
|
+
*
|
|
766
|
+
* // Before endMutation, operations modify the set in place
|
|
767
|
+
* const sameSet = HashSet.add(mutableSet, 3)
|
|
768
|
+
* assert(Object.is(mutableSet, sameSet)) // true - same object reference
|
|
769
|
+
* assert.deepStrictEqual(HashSet.toValues(mutableSet).sort(), [1, 2, 3])
|
|
770
|
+
*
|
|
771
|
+
* // Make the set immutable again
|
|
772
|
+
* const immutableSet = HashSet.endMutation(mutableSet)
|
|
773
|
+
*
|
|
774
|
+
* // endMutation returns the same set instance, now made immutable
|
|
775
|
+
* assert(Object.is(mutableSet, immutableSet)) // true - same object reference
|
|
776
|
+
*
|
|
777
|
+
* // After endMutation, operations create new instances
|
|
778
|
+
* const newSet = HashSet.add(immutableSet, 4)
|
|
779
|
+
* assert(!Object.is(immutableSet, newSet)) // false - different object references
|
|
780
|
+
*
|
|
781
|
+
* // The original set remains unchanged
|
|
782
|
+
* assert.deepStrictEqual(HashSet.toValues(immutableSet).sort(), [1, 2, 3])
|
|
783
|
+
*
|
|
784
|
+
* // The new set contains the added element
|
|
785
|
+
* assert.deepStrictEqual(HashSet.toValues(newSet).sort(), [1, 2, 3, 4])
|
|
786
|
+
* ```
|
|
787
|
+
*
|
|
788
|
+
* @see Other `HashSet` mutations are {@linkcode HashSet.add} {@linkcode HashSet.remove} {@linkcode HashSet.toggle} {@linkcode HashSet.beginMutation} {@linkcode HashSet.mutate}
|
|
94
789
|
*/
|
|
95
790
|
export const endMutation = HS.endMutation;
|
|
96
791
|
/**
|
|
97
792
|
* Mutates the `HashSet` within the context of the provided function.
|
|
98
793
|
*
|
|
794
|
+
* You can consider it a functional abstraction on top of the lower-level
|
|
795
|
+
* mutation primitives of {@linkcode HashSet.beginMutation} `->` `mutable
|
|
796
|
+
* context` `->` {@linkcode HashSet.endMutation}.
|
|
797
|
+
*
|
|
798
|
+
* @memberof HashSet
|
|
99
799
|
* @since 2.0.0
|
|
800
|
+
* @example **Syntax**
|
|
801
|
+
*
|
|
802
|
+
* ```ts
|
|
803
|
+
* import { HashSet, pipe } from "effect"
|
|
804
|
+
*
|
|
805
|
+
* // with data-last, a.k.a. pipeable API
|
|
806
|
+
* pipe(
|
|
807
|
+
* HashSet.make(1, 2, 3),
|
|
808
|
+
* HashSet.mutate((set) => {
|
|
809
|
+
* HashSet.add(set, 4)
|
|
810
|
+
* HashSet.remove(set, 1)
|
|
811
|
+
* })
|
|
812
|
+
* )
|
|
813
|
+
*
|
|
814
|
+
* // or piped with the pipe function
|
|
815
|
+
* HashSet.make(1, 2, 3).pipe(
|
|
816
|
+
* HashSet.mutate((set) => {
|
|
817
|
+
* HashSet.add(set, 4)
|
|
818
|
+
* HashSet.remove(set, 1)
|
|
819
|
+
* })
|
|
820
|
+
* )
|
|
821
|
+
*
|
|
822
|
+
* // or with data-first API
|
|
823
|
+
* HashSet.mutate(HashSet.make(1, 2, 3), (set) => {
|
|
824
|
+
* HashSet.add(set, 4)
|
|
825
|
+
* HashSet.remove(set, 1)
|
|
826
|
+
* })
|
|
827
|
+
* ```
|
|
828
|
+
*
|
|
829
|
+
* @see Other `HashSet` mutations are {@linkcode HashSet.add} {@linkcode HashSet.remove} {@linkcode HashSet.toggle} {@linkcode HashSet.beginMutation} {@linkcode HashSet.endMutation}
|
|
100
830
|
*/
|
|
101
831
|
export const mutate = HS.mutate;
|
|
102
832
|
/**
|
|
103
833
|
* Adds a value to the `HashSet`.
|
|
104
834
|
*
|
|
835
|
+
* Time complexity: **`O(1)`** average
|
|
836
|
+
*
|
|
837
|
+
* @remarks
|
|
838
|
+
* Remember that a `HashSet` is a collection of unique values, so adding a value
|
|
839
|
+
* that already exists in the `HashSet` will not add a duplicate.
|
|
840
|
+
*
|
|
841
|
+
* Remember that HashSet is an immutable data structure, so the `add` function,
|
|
842
|
+
* like all other functions that modify the HashSet, will return a new HashSet
|
|
843
|
+
* with the added value.
|
|
844
|
+
* @memberof HashSet
|
|
105
845
|
* @since 2.0.0
|
|
846
|
+
* @example **Syntax**
|
|
847
|
+
*
|
|
848
|
+
* ```ts
|
|
849
|
+
* import { HashSet, pipe } from "effect"
|
|
850
|
+
*
|
|
851
|
+
* // with data-last, a.k.a. pipeable API
|
|
852
|
+
* pipe(HashSet.empty(), HashSet.add(0), HashSet.add(0))
|
|
853
|
+
*
|
|
854
|
+
* // or piped with the pipe function
|
|
855
|
+
* HashSet.empty().pipe(HashSet.add(0))
|
|
856
|
+
*
|
|
857
|
+
* // or with data-first API
|
|
858
|
+
* HashSet.add(HashSet.empty(), 0)
|
|
859
|
+
* ```
|
|
860
|
+
*
|
|
861
|
+
* @see Other `HashSet` mutations are {@link remove} {@link toggle} {@link beginMutation} {@link endMutation} {@link mutate}
|
|
106
862
|
*/
|
|
107
863
|
export const add = HS.add;
|
|
108
864
|
/**
|
|
109
865
|
* Removes a value from the `HashSet`.
|
|
110
866
|
*
|
|
867
|
+
* Time complexity: **`O(1)`** average
|
|
868
|
+
*
|
|
869
|
+
* @memberof HashSet
|
|
111
870
|
* @since 2.0.0
|
|
871
|
+
* @example **Syntax**
|
|
872
|
+
*
|
|
873
|
+
* ```ts
|
|
874
|
+
* import { HashSet, pipe } from "effect"
|
|
875
|
+
*
|
|
876
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
877
|
+
* pipe(HashSet.make(0, 1, 2), HashSet.remove(0))
|
|
878
|
+
*
|
|
879
|
+
* // or piped with the pipe function
|
|
880
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.remove(0))
|
|
881
|
+
*
|
|
882
|
+
* // or with `data-first` API
|
|
883
|
+
* HashSet.remove(HashSet.make(0, 1, 2), 0)
|
|
884
|
+
* ```
|
|
885
|
+
*
|
|
886
|
+
* @see Other `HashSet` mutations are {@link add} {@link toggle} {@link beginMutation} {@link endMutation} {@link mutate}
|
|
112
887
|
*/
|
|
113
888
|
export const remove = HS.remove;
|
|
114
889
|
/**
|
|
115
|
-
* Computes the set difference between this `HashSet` and the
|
|
116
|
-
* `Iterable<A>`.
|
|
890
|
+
* Computes the set difference `(A - B)` between this `HashSet` and the
|
|
891
|
+
* specified `Iterable<A>`.
|
|
892
|
+
*
|
|
893
|
+
* Time complexity: **`O(n)`** where n is the number of elements in the set
|
|
117
894
|
*
|
|
118
895
|
* **NOTE**: the hash and equal of the values in both the set and the iterable
|
|
119
|
-
* must be the same
|
|
896
|
+
* must be the same; meaning we cannot compute a difference between a `HashSet
|
|
897
|
+
* of bananas` and a `HashSet of elephants` as they are not the same type and
|
|
898
|
+
* won't implement the Equal trait in the same way.
|
|
120
899
|
*
|
|
900
|
+
* @memberof HashSet
|
|
121
901
|
* @since 2.0.0
|
|
902
|
+
* @example **Syntax**
|
|
903
|
+
*
|
|
904
|
+
* ```ts
|
|
905
|
+
* import { HashSet, pipe } from "effect"
|
|
906
|
+
*
|
|
907
|
+
* // with data-last, a.k.a. pipeable API
|
|
908
|
+
* pipe(HashSet.make(1, 2, 3), HashSet.difference(HashSet.make(3, 4, 5)))
|
|
909
|
+
*
|
|
910
|
+
* // or piped with the pipe function
|
|
911
|
+
* HashSet.make(1, 2, 3).pipe(HashSet.difference(HashSet.make(3, 4, 5)))
|
|
912
|
+
*
|
|
913
|
+
* // or with data-first API
|
|
914
|
+
* HashSet.difference(HashSet.make(1, 2, 3), HashSet.make(3, 4, 5))
|
|
915
|
+
* ```
|
|
916
|
+
*
|
|
917
|
+
* @see Other `HashSet` operations are {@link intersection} {@link union}
|
|
122
918
|
*/
|
|
123
919
|
export const difference = HS.difference;
|
|
124
920
|
/**
|
|
125
921
|
* Returns a `HashSet` of values which are present in both this set and that
|
|
126
|
-
* `Iterable<A>`.
|
|
922
|
+
* `Iterable<A>`. Computes set intersection (A ∩ B)
|
|
923
|
+
*
|
|
924
|
+
* Time complexity: **`O(n)`** where n is the number of elements in the smaller
|
|
925
|
+
* set
|
|
127
926
|
*
|
|
128
927
|
* **NOTE**: the hash and equal of the values in both the set and the iterable
|
|
129
928
|
* must be the same.
|
|
130
929
|
*
|
|
930
|
+
* @memberof HashSet
|
|
131
931
|
* @since 2.0.0
|
|
932
|
+
* @example **Syntax**
|
|
933
|
+
*
|
|
934
|
+
* ```ts
|
|
935
|
+
* import { HashSet, pipe } from "effect"
|
|
936
|
+
*
|
|
937
|
+
* // with data-last, a.k.a. pipeable API
|
|
938
|
+
* pipe(HashSet.make(1, 2, 3), HashSet.intersection(HashSet.make(2, 3, 4)))
|
|
939
|
+
*
|
|
940
|
+
* // or piped with the pipe function
|
|
941
|
+
* HashSet.make(1, 2, 3).pipe(HashSet.intersection(HashSet.make(2, 3, 4)))
|
|
942
|
+
*
|
|
943
|
+
* // or with data-first API
|
|
944
|
+
* HashSet.intersection(HashSet.make(1, 2, 3), HashSet.make(2, 3, 4))
|
|
945
|
+
* ```
|
|
946
|
+
*
|
|
947
|
+
* @see Other `HashSet` operations are {@link difference} {@link union}
|
|
132
948
|
*/
|
|
133
949
|
export const intersection = HS.intersection;
|
|
134
950
|
/**
|
|
135
|
-
* Computes the set union `(
|
|
951
|
+
* Computes the set union `( self ∪ that )` between this `HashSet` and the
|
|
136
952
|
* specified `Iterable<A>`.
|
|
137
953
|
*
|
|
954
|
+
* Time complexity: **`O(n)`** where n is the number of elements in the set
|
|
955
|
+
*
|
|
138
956
|
* **NOTE**: the hash and equal of the values in both the set and the iterable
|
|
139
957
|
* must be the same.
|
|
140
958
|
*
|
|
959
|
+
* @memberof HashSet
|
|
141
960
|
* @since 2.0.0
|
|
961
|
+
* @example **Syntax**
|
|
962
|
+
*
|
|
963
|
+
* ```ts
|
|
964
|
+
* import { HashSet, pipe } from "effect"
|
|
965
|
+
*
|
|
966
|
+
* // with data-last, a.k.a. pipeable API
|
|
967
|
+
* pipe(HashSet.make(1, 2, 3), HashSet.union(HashSet.make(3, 4, 5)))
|
|
968
|
+
*
|
|
969
|
+
* // or piped with the pipe function
|
|
970
|
+
* HashSet.make(1, 2, 3).pipe(HashSet.union(HashSet.make(3, 4, 5)))
|
|
971
|
+
*
|
|
972
|
+
* // or with data-first API
|
|
973
|
+
* HashSet.union(HashSet.make(1, 2, 3), HashSet.make(3, 4, 5))
|
|
974
|
+
* ```
|
|
975
|
+
*
|
|
976
|
+
* @see Other `HashSet` operations are {@link difference} {@link intersection}
|
|
142
977
|
*/
|
|
143
978
|
export const union = HS.union;
|
|
144
979
|
/**
|
|
@@ -146,42 +981,189 @@ export const union = HS.union;
|
|
|
146
981
|
* will be removed from the `HashSet`, otherwise the value will be added to the
|
|
147
982
|
* `HashSet`.
|
|
148
983
|
*
|
|
984
|
+
* Time complexity: **`O(1)`** average
|
|
985
|
+
*
|
|
986
|
+
* @memberof HashSet
|
|
149
987
|
* @since 2.0.0
|
|
988
|
+
* @example **Syntax**
|
|
989
|
+
*
|
|
990
|
+
* ```ts
|
|
991
|
+
* import { HashSet, pipe } from "effect"
|
|
992
|
+
*
|
|
993
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
994
|
+
* pipe(HashSet.make(0, 1, 2), HashSet.toggle(0))
|
|
995
|
+
*
|
|
996
|
+
* // or piped with the pipe function
|
|
997
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.toggle(0))
|
|
998
|
+
*
|
|
999
|
+
* // or with `data-first` API
|
|
1000
|
+
* HashSet.toggle(HashSet.make(0, 1, 2), 0)
|
|
1001
|
+
* ```
|
|
1002
|
+
*
|
|
1003
|
+
* @returns A new `HashSet` where the toggled value is being either added or
|
|
1004
|
+
* removed based on the initial `HashSet` state.
|
|
1005
|
+
* @see Other `HashSet` mutations are {@link add} {@link remove} {@link beginMutation} {@link endMutation} {@link mutate}
|
|
150
1006
|
*/
|
|
151
1007
|
export const toggle = HS.toggle;
|
|
152
1008
|
/**
|
|
153
1009
|
* Maps over the values of the `HashSet` using the specified function.
|
|
154
1010
|
*
|
|
1011
|
+
* The time complexity is of **`O(n)`**.
|
|
1012
|
+
*
|
|
1013
|
+
* @memberof HashSet
|
|
155
1014
|
* @since 2.0.0
|
|
156
1015
|
* @category mapping
|
|
1016
|
+
* @example **Syntax**
|
|
1017
|
+
*
|
|
1018
|
+
* ```ts
|
|
1019
|
+
* import { HashSet, pipe } from "effect"
|
|
1020
|
+
*
|
|
1021
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1022
|
+
* pipe(
|
|
1023
|
+
* HashSet.make(0, 1, 2), // HashSet.HashSet<number>
|
|
1024
|
+
* HashSet.map(String) // HashSet.HashSet<string>
|
|
1025
|
+
* )
|
|
1026
|
+
*
|
|
1027
|
+
* // or piped with the pipe method
|
|
1028
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.map(String))
|
|
1029
|
+
*
|
|
1030
|
+
* // or with `data-first` API
|
|
1031
|
+
* HashSet.map(HashSet.make(0, 1, 2), String)
|
|
1032
|
+
* ```
|
|
157
1033
|
*/
|
|
158
1034
|
export const map = HS.map;
|
|
159
1035
|
/**
|
|
160
1036
|
* Chains over the values of the `HashSet` using the specified function.
|
|
161
1037
|
*
|
|
1038
|
+
* The time complexity is of **`O(n)`**.
|
|
1039
|
+
*
|
|
1040
|
+
* @memberof HashSet
|
|
162
1041
|
* @since 2.0.0
|
|
163
1042
|
* @category sequencing
|
|
1043
|
+
* @example **Syntax**
|
|
1044
|
+
*
|
|
1045
|
+
* ```ts
|
|
1046
|
+
* import { HashSet, pipe } from "effect"
|
|
1047
|
+
*
|
|
1048
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1049
|
+
* pipe(
|
|
1050
|
+
* HashSet.make(0, 1, 2), // HashSet.HashSet<number>
|
|
1051
|
+
* HashSet.flatMap((n) => Array.of(String(n))) // HashSet.HashSet<string>
|
|
1052
|
+
* )
|
|
1053
|
+
*
|
|
1054
|
+
* // or piped with the pipe method
|
|
1055
|
+
* HashSet.make(0, 1, 2) // HashSet.HashSet<number>
|
|
1056
|
+
* .pipe(
|
|
1057
|
+
* HashSet.flatMap((n) => Array.of(String(n))) // HashSet.HashSet<string>
|
|
1058
|
+
* )
|
|
1059
|
+
*
|
|
1060
|
+
* // or with `data-first` API
|
|
1061
|
+
* HashSet.flatMap(HashSet.make(0, 1, 2), (n) => Array.of(String(n)))
|
|
1062
|
+
* ```
|
|
164
1063
|
*/
|
|
165
1064
|
export const flatMap = HS.flatMap;
|
|
166
1065
|
/**
|
|
167
1066
|
* Applies the specified function to the values of the `HashSet`.
|
|
168
1067
|
*
|
|
1068
|
+
* The time complexity is of **`O(n)`**.
|
|
1069
|
+
*
|
|
1070
|
+
* @memberof HashSet
|
|
169
1071
|
* @since 2.0.0
|
|
170
1072
|
* @category traversing
|
|
1073
|
+
* @example **Syntax**
|
|
1074
|
+
*
|
|
1075
|
+
* ```ts
|
|
1076
|
+
* import { HashSet, pipe } from "effect"
|
|
1077
|
+
*
|
|
1078
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1079
|
+
* pipe(HashSet.make(0, 1, 2), HashSet.forEach(console.log)) // logs: 0 1 2
|
|
1080
|
+
*
|
|
1081
|
+
* // or piped with the pipe method
|
|
1082
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.forEach(console.log)) // logs: 0 1 2
|
|
1083
|
+
*
|
|
1084
|
+
* // or with `data-first` API
|
|
1085
|
+
* HashSet.forEach(HashSet.make(0, 1, 2), console.log) // logs: 0 1 2
|
|
1086
|
+
* ```
|
|
171
1087
|
*/
|
|
172
1088
|
export const forEach = HS.forEach;
|
|
173
1089
|
/**
|
|
174
1090
|
* Reduces the specified state over the values of the `HashSet`.
|
|
175
1091
|
*
|
|
1092
|
+
* The time complexity is of **`O(n)`**.
|
|
1093
|
+
*
|
|
1094
|
+
* @memberof HashSet
|
|
176
1095
|
* @since 2.0.0
|
|
177
1096
|
* @category folding
|
|
1097
|
+
* @example **Syntax**
|
|
1098
|
+
*
|
|
1099
|
+
* ```ts
|
|
1100
|
+
* import { HashSet, pipe } from "effect"
|
|
1101
|
+
*
|
|
1102
|
+
* const sum = (a: number, b: number): number => a + b
|
|
1103
|
+
*
|
|
1104
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1105
|
+
* pipe(HashSet.make(0, 1, 2), HashSet.reduce(0, sum))
|
|
1106
|
+
*
|
|
1107
|
+
* // or with the pipe method
|
|
1108
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.reduce(0, sum))
|
|
1109
|
+
*
|
|
1110
|
+
* // or with `data-first` API
|
|
1111
|
+
* HashSet.reduce(HashSet.make(0, 1, 2), 0, sum)
|
|
1112
|
+
* ```
|
|
178
1113
|
*/
|
|
179
1114
|
export const reduce = HS.reduce;
|
|
180
1115
|
/**
|
|
181
1116
|
* Filters values out of a `HashSet` using the specified predicate.
|
|
182
1117
|
*
|
|
1118
|
+
* The time complexity is of **`O(n)`**.
|
|
1119
|
+
*
|
|
1120
|
+
* @memberof HashSet
|
|
183
1121
|
* @since 2.0.0
|
|
184
1122
|
* @category filtering
|
|
1123
|
+
* @example **Syntax** with {@link Predicate}
|
|
1124
|
+
*
|
|
1125
|
+
* ```ts
|
|
1126
|
+
* import { HashSet, type Predicate, pipe } from "effect"
|
|
1127
|
+
*
|
|
1128
|
+
* const filterPositiveNumbers: Predicate.Predicate<number> = (n) => n > 0
|
|
1129
|
+
*
|
|
1130
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1131
|
+
* pipe(
|
|
1132
|
+
* HashSet.make(-2, -1, 0, 1, 2),
|
|
1133
|
+
* HashSet.filter(filterPositiveNumbers)
|
|
1134
|
+
* )
|
|
1135
|
+
*
|
|
1136
|
+
* // or with the pipe method
|
|
1137
|
+
* HashSet.make(-2, -1, 0, 1, 2).pipe(HashSet.filter(filterPositiveNumbers))
|
|
1138
|
+
*
|
|
1139
|
+
* // or with `data-first` API
|
|
1140
|
+
* HashSet.filter(HashSet.make(-2, -1, 0, 1, 2), filterPositiveNumbers)
|
|
1141
|
+
* ```
|
|
1142
|
+
*
|
|
1143
|
+
* @example **Syntax** with {@link Refinement}
|
|
1144
|
+
*
|
|
1145
|
+
* ```ts
|
|
1146
|
+
* import { HashSet, pipe } from "effect"
|
|
1147
|
+
*
|
|
1148
|
+
* const stringRefinement = (value: unknown): value is string =>
|
|
1149
|
+
* typeof value === "string"
|
|
1150
|
+
*
|
|
1151
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1152
|
+
* pipe(
|
|
1153
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier"), // // HashSet.HashSet<number | string>
|
|
1154
|
+
* HashSet.filter(stringRefinement)
|
|
1155
|
+
* ) // HashSet.HashSet<string>
|
|
1156
|
+
*
|
|
1157
|
+
* // or with the pipe method
|
|
1158
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier") // HashSet.HashSet<number | string>
|
|
1159
|
+
* .pipe(HashSet.filter(stringRefinement)) // HashSet.HashSet<string>
|
|
1160
|
+
*
|
|
1161
|
+
* // or with `data-first` API
|
|
1162
|
+
* HashSet.filter(
|
|
1163
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier"), // HashSet.HashSet<number | string>
|
|
1164
|
+
* stringRefinement
|
|
1165
|
+
* ) // HashSet.HashSet<string>
|
|
1166
|
+
* ```
|
|
185
1167
|
*/
|
|
186
1168
|
export const filter = HS.filter;
|
|
187
1169
|
/**
|
|
@@ -191,8 +1173,57 @@ export const filter = HS.filter;
|
|
|
191
1173
|
* right side of the resulting `Tuple`, otherwise the value will be placed into
|
|
192
1174
|
* the left side.
|
|
193
1175
|
*
|
|
1176
|
+
* Time complexity is of **`O(n)`**.
|
|
1177
|
+
*
|
|
1178
|
+
* @memberof HashSet
|
|
194
1179
|
* @since 2.0.0
|
|
195
1180
|
* @category partitioning
|
|
1181
|
+
* @example **Syntax** with {@link Predicate}
|
|
1182
|
+
*
|
|
1183
|
+
* ```ts
|
|
1184
|
+
* import { HashSet, pipe, Predicate } from "effect"
|
|
1185
|
+
*
|
|
1186
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1187
|
+
* pipe(
|
|
1188
|
+
* HashSet.make(0, 1, 2, 3, 4, 5),
|
|
1189
|
+
* HashSet.partition((n) => n % 2 === 0)
|
|
1190
|
+
* )
|
|
1191
|
+
*
|
|
1192
|
+
* // or with the pipe method
|
|
1193
|
+
* HashSet.make(0, 1, 2, 3, 4, 5).pipe(
|
|
1194
|
+
* HashSet.partition((n) => n % 2 === 0)
|
|
1195
|
+
* )
|
|
1196
|
+
*
|
|
1197
|
+
* // or with `data-first` API
|
|
1198
|
+
* HashSet.partition(HashSet.make(0, 1, 2, 3, 4, 5), (n) => n % 2 === 0)
|
|
1199
|
+
* ```
|
|
1200
|
+
*
|
|
1201
|
+
* @example **Syntax** with {@link Refinement}
|
|
1202
|
+
*
|
|
1203
|
+
* ```ts
|
|
1204
|
+
* import { HashSet, pipe, Predicate } from "effect"
|
|
1205
|
+
*
|
|
1206
|
+
* const stringRefinement: Predicate.Refinement<string | number, string> = (
|
|
1207
|
+
* value
|
|
1208
|
+
* ) => typeof value === "string"
|
|
1209
|
+
*
|
|
1210
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1211
|
+
* pipe(
|
|
1212
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier"),
|
|
1213
|
+
* HashSet.partition(stringRefinement)
|
|
1214
|
+
* )
|
|
1215
|
+
*
|
|
1216
|
+
* // or with the pipe method
|
|
1217
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier").pipe(
|
|
1218
|
+
* HashSet.partition(stringRefinement)
|
|
1219
|
+
* )
|
|
1220
|
+
*
|
|
1221
|
+
* // or with `data-first` API
|
|
1222
|
+
* HashSet.partition(
|
|
1223
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier"),
|
|
1224
|
+
* stringRefinement
|
|
1225
|
+
* )
|
|
1226
|
+
* ```
|
|
196
1227
|
*/
|
|
197
1228
|
export const partition = HS.partition;
|
|
198
1229
|
//# sourceMappingURL=HashSet.js.map
|