effect 3.14.2 → 3.14.4
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 +1062 -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 +1867 -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 +1062 -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 +1921 -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 ({@link module:HashSet.has}): **`O(1)`** average time
|
|
37
|
+
* complexity
|
|
38
|
+
* - **Insertion** operations ({@link module:HashSet.add}): **`O(1)`** average time
|
|
39
|
+
* complexity
|
|
40
|
+
* - **Removal** operations ({@link module:HashSet.remove}): **`O(1)`** average
|
|
41
|
+
* time complexity
|
|
42
|
+
* - **Set** operations ({@link module:HashSet.union},
|
|
43
|
+
* {@link module: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 | {@link module:HashSet.empty} | Creates an empty HashSet | O(1) |
|
|
59
|
+
* | constructors | {@link module:HashSet.fromIterable} | Creates a HashSet from an iterable | O(n) |
|
|
60
|
+
* | constructors | {@link module:HashSet.make} | Creates a HashSet from multiple values | O(n) |
|
|
61
|
+
* | | | | |
|
|
62
|
+
* | elements | {@link module:HashSet.has} | Checks if a value exists in the set | O(1) avg |
|
|
63
|
+
* | elements | {@link module:HashSet.some} | Checks if any element satisfies a predicate | O(n) |
|
|
64
|
+
* | elements | {@link module:HashSet.every} | Checks if all elements satisfy a predicate | O(n) |
|
|
65
|
+
* | elements | {@link module:HashSet.isSubset} | Checks if a set is a subset of another | O(n) |
|
|
66
|
+
* | | | | |
|
|
67
|
+
* | getters | {@link module:HashSet.values} | Gets an iterator of all values | O(1) |
|
|
68
|
+
* | getters | {@link module:HashSet.toValues} | Gets an array of all values | O(n) |
|
|
69
|
+
* | getters | {@link module:HashSet.size} | Gets the number of elements | O(1) |
|
|
70
|
+
* | | | | |
|
|
71
|
+
* | mutations | {@link module:HashSet.add} | Adds a value to the set | O(1) avg |
|
|
72
|
+
* | mutations | {@link module:HashSet.remove} | Removes a value from the set | O(1) avg |
|
|
73
|
+
* | mutations | {@link module:HashSet.toggle} | Toggles a value's presence | O(1) avg |
|
|
74
|
+
* | | | | |
|
|
75
|
+
* | operations | {@link module:HashSet.difference} | Computes set difference (A - B) | O(n) |
|
|
76
|
+
* | operations | {@link module:HashSet.intersection} | Computes set intersection (A ∩ B) | O(n) |
|
|
77
|
+
* | operations | {@link module:HashSet.union} | Computes set union (A ∪ B) | O(n) |
|
|
78
|
+
* | | | | |
|
|
79
|
+
* | mapping | {@link module:HashSet.map} | Transforms each element | O(n) |
|
|
80
|
+
* | | | | |
|
|
81
|
+
* | sequencing | {@link module:HashSet.flatMap} | Transforms and flattens elements | O(n) |
|
|
82
|
+
* | | | | |
|
|
83
|
+
* | traversing | {@link module:HashSet.forEach} | Applies a function to each element | O(n) |
|
|
84
|
+
* | | | | |
|
|
85
|
+
* | folding | {@link module:HashSet.reduce} | Reduces the set to a single value | O(n) |
|
|
86
|
+
* | | | | |
|
|
87
|
+
* | filtering | {@link module:HashSet.filter} | Keeps elements that satisfy a predicate | O(n) |
|
|
88
|
+
* | | | | |
|
|
89
|
+
* | partitioning | {@link module: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,331 @@ 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 module:HashSet.make} {@link module:HashSet.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
|
|
299
|
+
*
|
|
300
|
+
* ```ts
|
|
301
|
+
* // Creating a HashSet from an Array
|
|
302
|
+
* import { HashSet, pipe } from "effect"
|
|
303
|
+
*
|
|
304
|
+
* console.log(
|
|
305
|
+
* pipe(
|
|
306
|
+
* [1, 2, 3, 4, 5, 1, 2, 3], // Array<number> is an Iterable<number>; Note the duplicates.
|
|
307
|
+
* HashSet.fromIterable,
|
|
308
|
+
* HashSet.toValues
|
|
309
|
+
* )
|
|
310
|
+
* ) // Output: [1, 2, 3, 4, 5]
|
|
311
|
+
* ```
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
*
|
|
315
|
+
* ```ts
|
|
316
|
+
* // Creating a HashSet from a Set
|
|
317
|
+
* import { HashSet, pipe } from "effect"
|
|
318
|
+
*
|
|
319
|
+
* console.log(
|
|
320
|
+
* pipe(
|
|
321
|
+
* new Set(["apple", "banana", "orange", "apple"]), // Set<string> is an Iterable<string>
|
|
322
|
+
* HashSet.fromIterable,
|
|
323
|
+
* HashSet.toValues
|
|
324
|
+
* )
|
|
325
|
+
* ) // Output: ["apple", "banana", "orange"]
|
|
326
|
+
* ```
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
*
|
|
330
|
+
* ```ts
|
|
331
|
+
* // Creating a HashSet from a Generator
|
|
332
|
+
* import { HashSet } from "effect"
|
|
333
|
+
*
|
|
334
|
+
* // Generator functions return iterables
|
|
335
|
+
* function* fibonacci(n: number): Generator<number, void, unknown> {
|
|
336
|
+
* let [a, b] = [0, 1]
|
|
337
|
+
* for (let i = 0; i < n; i++) {
|
|
338
|
+
* yield a
|
|
339
|
+
* ;[a, b] = [b, a + b]
|
|
340
|
+
* }
|
|
341
|
+
* }
|
|
342
|
+
*
|
|
343
|
+
* // Create a HashSet from the first 10 Fibonacci numbers
|
|
344
|
+
* const fibonacciSet = HashSet.fromIterable(fibonacci(10))
|
|
345
|
+
*
|
|
346
|
+
* console.log(HashSet.toValues(fibonacciSet))
|
|
347
|
+
* // Outputs: [0, 1, 2, 3, 5, 8, 13, 21, 34] but in unsorted order
|
|
348
|
+
* ```
|
|
349
|
+
*
|
|
350
|
+
* @example
|
|
351
|
+
*
|
|
352
|
+
* ```ts
|
|
353
|
+
* // Creating a HashSet from another HashSet
|
|
354
|
+
* import { HashSet, pipe } from "effect"
|
|
355
|
+
*
|
|
356
|
+
* console.log(
|
|
357
|
+
* pipe(
|
|
358
|
+
* // since HashSet implements the Iterable interface, we can use it to create a new HashSet
|
|
359
|
+
* HashSet.make(1, 2, 3, 4),
|
|
360
|
+
* HashSet.fromIterable,
|
|
361
|
+
* HashSet.toValues // turns the HashSet back into an array
|
|
362
|
+
* )
|
|
363
|
+
* ) // Output: [1, 2, 3, 4]
|
|
364
|
+
* ```
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
*
|
|
368
|
+
* ```ts
|
|
369
|
+
* // Creating a HashSet from other Effect's data structures like Chunk
|
|
370
|
+
* import { Chunk, HashSet, pipe } from "effect"
|
|
371
|
+
*
|
|
372
|
+
* console.log(
|
|
373
|
+
* pipe(
|
|
374
|
+
* Chunk.make(1, 2, 3, 4), // Iterable<number>
|
|
375
|
+
* HashSet.fromIterable,
|
|
376
|
+
* HashSet.toValues // turns the HashSet back into an array
|
|
377
|
+
* )
|
|
378
|
+
* ) // Outputs: [1, 2, 3, 4]
|
|
379
|
+
* ```
|
|
380
|
+
*
|
|
381
|
+
* @see Other `HashSet` constructors are {@link module:HashSet.empty} {@link module:HashSet.make}
|
|
23
382
|
*/
|
|
24
383
|
export const fromIterable = HS.fromIterable;
|
|
25
384
|
/**
|
|
26
385
|
* Construct a new `HashSet` from a variable number of values.
|
|
27
386
|
*
|
|
387
|
+
* Time complexity: **`O(n)`** where n is the number of elements
|
|
388
|
+
*
|
|
389
|
+
* @memberof HashSet
|
|
28
390
|
* @since 2.0.0
|
|
29
391
|
* @category constructors
|
|
392
|
+
* @example
|
|
393
|
+
*
|
|
394
|
+
* ```ts
|
|
395
|
+
* import { Equal, Hash, HashSet, pipe } from "effect"
|
|
396
|
+
* import assert from "node:assert/strict"
|
|
397
|
+
*
|
|
398
|
+
* class Character implements Equal.Equal {
|
|
399
|
+
* readonly name: string
|
|
400
|
+
* readonly trait: string
|
|
401
|
+
*
|
|
402
|
+
* constructor(name: string, trait: string) {
|
|
403
|
+
* this.name = name
|
|
404
|
+
* this.trait = trait
|
|
405
|
+
* }
|
|
406
|
+
*
|
|
407
|
+
* // Define equality based on name, and trait
|
|
408
|
+
* [Equal.symbol](that: Equal.Equal): boolean {
|
|
409
|
+
* if (that instanceof Character) {
|
|
410
|
+
* return (
|
|
411
|
+
* Equal.equals(this.name, that.name) &&
|
|
412
|
+
* Equal.equals(this.trait, that.trait)
|
|
413
|
+
* )
|
|
414
|
+
* }
|
|
415
|
+
* return false
|
|
416
|
+
* }
|
|
417
|
+
*
|
|
418
|
+
* // Generate a hash code based on the sum of the character's name and trait
|
|
419
|
+
* [Hash.symbol](): number {
|
|
420
|
+
* return Hash.hash(this.name + this.trait)
|
|
421
|
+
* }
|
|
422
|
+
*
|
|
423
|
+
* static readonly of = (name: string, trait: string): Character => {
|
|
424
|
+
* return new Character(name, trait)
|
|
425
|
+
* }
|
|
426
|
+
* }
|
|
427
|
+
*
|
|
428
|
+
* assert.strictEqual(
|
|
429
|
+
* Equal.equals(
|
|
430
|
+
* HashSet.make(
|
|
431
|
+
* Character.of("Alice", "Curious"),
|
|
432
|
+
* Character.of("Alice", "Curious"),
|
|
433
|
+
* Character.of("White Rabbit", "Always late"),
|
|
434
|
+
* Character.of("Mad Hatter", "Tea enthusiast")
|
|
435
|
+
* ),
|
|
436
|
+
* // Is the same as adding each character to an empty set
|
|
437
|
+
* pipe(
|
|
438
|
+
* HashSet.empty(),
|
|
439
|
+
* HashSet.add(Character.of("Alice", "Curious")),
|
|
440
|
+
* HashSet.add(Character.of("Alice", "Curious")), // Alice tried to attend twice!
|
|
441
|
+
* HashSet.add(Character.of("White Rabbit", "Always late")),
|
|
442
|
+
* HashSet.add(Character.of("Mad Hatter", "Tea enthusiast"))
|
|
443
|
+
* )
|
|
444
|
+
* ),
|
|
445
|
+
* true,
|
|
446
|
+
* "`HashSet.make` and `HashSet.empty() + HashSet.add()` should be equal"
|
|
447
|
+
* )
|
|
448
|
+
*
|
|
449
|
+
* assert.strictEqual(
|
|
450
|
+
* Equal.equals(
|
|
451
|
+
* HashSet.make(
|
|
452
|
+
* Character.of("Alice", "Curious"),
|
|
453
|
+
* Character.of("Alice", "Curious"),
|
|
454
|
+
* Character.of("White Rabbit", "Always late"),
|
|
455
|
+
* Character.of("Mad Hatter", "Tea enthusiast")
|
|
456
|
+
* ),
|
|
457
|
+
* HashSet.fromIterable([
|
|
458
|
+
* Character.of("Alice", "Curious"),
|
|
459
|
+
* Character.of("Alice", "Curious"),
|
|
460
|
+
* Character.of("White Rabbit", "Always late"),
|
|
461
|
+
* Character.of("Mad Hatter", "Tea enthusiast")
|
|
462
|
+
* ])
|
|
463
|
+
* ),
|
|
464
|
+
* true,
|
|
465
|
+
* "`HashSet.make` and `HashSet.fromIterable` should be equal"
|
|
466
|
+
* )
|
|
467
|
+
* ```
|
|
468
|
+
*
|
|
469
|
+
* @see Other `HashSet` constructors are {@link module:HashSet.fromIterable} {@link module:HashSet.empty}
|
|
30
470
|
*/
|
|
31
471
|
export const make = HS.make;
|
|
32
472
|
/**
|
|
33
473
|
* Checks if the specified value exists in the `HashSet`.
|
|
34
474
|
*
|
|
475
|
+
* Time complexity: **`O(1)`** average
|
|
476
|
+
*
|
|
477
|
+
* @memberof HashSet
|
|
35
478
|
* @since 2.0.0
|
|
36
479
|
* @category elements
|
|
480
|
+
* @example
|
|
481
|
+
*
|
|
482
|
+
* ```ts
|
|
483
|
+
* // Syntax
|
|
484
|
+
* import { HashSet, pipe } from "effect"
|
|
485
|
+
*
|
|
486
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
487
|
+
* pipe(HashSet.make(0, 1, 2), HashSet.has(3)) // false
|
|
488
|
+
*
|
|
489
|
+
* // or piped with the pipe function
|
|
490
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.has(3)) // false
|
|
491
|
+
*
|
|
492
|
+
* // or with `data-first` API
|
|
493
|
+
* HashSet.has(HashSet.make(0, 1, 2), 3) // false
|
|
494
|
+
* ```
|
|
495
|
+
*
|
|
496
|
+
* @returns A `boolean` signaling the presence of the value in the HashSet
|
|
497
|
+
* @see Other `HashSet` elements are {@link module:HashSet.some} {@link module:HashSet.every} {@link module:HashSet.isSubset}
|
|
37
498
|
*/
|
|
38
499
|
export const has = HS.has;
|
|
39
500
|
/**
|
|
40
501
|
* Check if a predicate holds true for some `HashSet` element.
|
|
41
502
|
*
|
|
503
|
+
* Time complexity: **`O(n)`** where n is the number of elements in the set
|
|
504
|
+
*
|
|
505
|
+
* @memberof HashSet
|
|
42
506
|
* @since 2.0.0
|
|
43
507
|
* @category elements
|
|
508
|
+
* @example
|
|
509
|
+
*
|
|
510
|
+
* ```ts
|
|
511
|
+
* // Syntax
|
|
512
|
+
* import { HashSet, pipe } from "effect"
|
|
513
|
+
*
|
|
514
|
+
* const set: HashSet.HashSet<number> = HashSet.make(0, 1, 2)
|
|
515
|
+
*
|
|
516
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
517
|
+
* pipe(
|
|
518
|
+
* set,
|
|
519
|
+
* HashSet.some((n) => n > 0)
|
|
520
|
+
* ) // true
|
|
521
|
+
*
|
|
522
|
+
* // or piped with the pipe function
|
|
523
|
+
* set.pipe(HashSet.some((n) => n > 0)) // true
|
|
524
|
+
*
|
|
525
|
+
* // or with `data-first` API
|
|
526
|
+
* HashSet.some(set, (n) => n > 0) // true
|
|
527
|
+
* ```
|
|
528
|
+
*
|
|
529
|
+
* @see Other `HashSet` elements are {@link module:HashSet.has} {@link module:HashSet.every} {@link module:HashSet.isSubset}
|
|
44
530
|
*/
|
|
45
531
|
export const some = HS.some;
|
|
46
532
|
/**
|
|
47
533
|
* Check if a predicate holds true for every `HashSet` element.
|
|
48
534
|
*
|
|
535
|
+
* Time complexity is **`O(n)`** as it needs to traverse the whole HashSet
|
|
536
|
+
* collection
|
|
537
|
+
*
|
|
538
|
+
* @memberof HashSet
|
|
49
539
|
* @since 2.0.0
|
|
50
540
|
* @category elements
|
|
541
|
+
* @example
|
|
542
|
+
*
|
|
543
|
+
* ```ts
|
|
544
|
+
* // Syntax with Refinement
|
|
545
|
+
* import { HashSet, pipe, Predicate } from "effect"
|
|
546
|
+
*
|
|
547
|
+
* const numberOrString = HashSet.make(1, "1", "one", "uno")
|
|
548
|
+
*
|
|
549
|
+
* // with `data-last`, a.k.a. `pipeable` API and `Refinement`
|
|
550
|
+
* pipe(
|
|
551
|
+
* numberOrString, // HashSet.HashSet<number | string>
|
|
552
|
+
* HashSet.every(Predicate.isString)
|
|
553
|
+
* ) // HashSet.HashSet<string>
|
|
554
|
+
*
|
|
555
|
+
* // or piped with the pipe function and `Refinement`
|
|
556
|
+
* numberOrString // HashSet.HashSet<number | string>
|
|
557
|
+
* .pipe(HashSet.every(Predicate.isString)) // HashSet.HashSet<string>
|
|
558
|
+
*
|
|
559
|
+
* // or with `data-first` API and `Refinement`
|
|
560
|
+
* HashSet.every(
|
|
561
|
+
* numberOrString, // HashSet.HashSet<number | string>
|
|
562
|
+
* Predicate.isString
|
|
563
|
+
* ) // HashSet.HashSet<string>
|
|
564
|
+
* ```
|
|
565
|
+
*
|
|
566
|
+
* @example
|
|
567
|
+
*
|
|
568
|
+
* ```ts
|
|
569
|
+
* // Syntax with Predicate
|
|
570
|
+
* import { HashSet, pipe } from "effect"
|
|
571
|
+
*
|
|
572
|
+
* const set = HashSet.make(1, 2, 3)
|
|
573
|
+
*
|
|
574
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
575
|
+
* pipe(
|
|
576
|
+
* set,
|
|
577
|
+
* HashSet.every((n) => n >= 0)
|
|
578
|
+
* ) // true
|
|
579
|
+
*
|
|
580
|
+
* // or piped with the pipe function
|
|
581
|
+
* set.pipe(HashSet.every((n) => n >= 0)) // true
|
|
582
|
+
*
|
|
583
|
+
* // or with `data-first` API
|
|
584
|
+
* HashSet.every(set, (n) => n >= 0) // true
|
|
585
|
+
* ```
|
|
586
|
+
*
|
|
587
|
+
* @returns A boolean once it has evaluated that whole collection fulfill the
|
|
588
|
+
* Predicate function
|
|
589
|
+
* @see Other `HashSet` elements are {@link module:HashSet.has} {@link module:HashSet.some} {@link module:HashSet.isSubset}
|
|
51
590
|
*/
|
|
52
591
|
export const every = HS.every;
|
|
53
592
|
/**
|
|
@@ -56,89 +595,400 @@ export const every = HS.every;
|
|
|
56
595
|
*
|
|
57
596
|
* **NOTE**: the hash and equal of both sets must be the same.
|
|
58
597
|
*
|
|
598
|
+
* Time complexity analysis is of **`O(n)`**
|
|
599
|
+
*
|
|
600
|
+
* @memberof HashSet
|
|
59
601
|
* @since 2.0.0
|
|
60
602
|
* @category elements
|
|
603
|
+
* @example
|
|
604
|
+
*
|
|
605
|
+
* ```ts
|
|
606
|
+
* // Syntax
|
|
607
|
+
* import { HashSet, pipe } from "effect"
|
|
608
|
+
*
|
|
609
|
+
* const set1 = HashSet.make(0, 1)
|
|
610
|
+
* const set2 = HashSet.make(1, 2)
|
|
611
|
+
* const set3 = HashSet.make(0, 1, 2)
|
|
612
|
+
*
|
|
613
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
614
|
+
* pipe(set1, HashSet.isSubset(set2)) // false
|
|
615
|
+
* pipe(set1, HashSet.isSubset(set3)) // true
|
|
616
|
+
*
|
|
617
|
+
* // or piped with the pipe function
|
|
618
|
+
* set1.pipe(HashSet.isSubset(set2)) // false
|
|
619
|
+
* set1.pipe(HashSet.isSubset(set3)) // true
|
|
620
|
+
*
|
|
621
|
+
* // or with `data-first` API
|
|
622
|
+
* HashSet.isSubset(set1, set2) // false
|
|
623
|
+
* HashSet.isSubset(set1, set3) // true)
|
|
624
|
+
* ```
|
|
625
|
+
*
|
|
626
|
+
* @see Other `HashSet` elements are {@link module:HashSet.has} {@link module:HashSet.some} {@link module:HashSet.every}
|
|
61
627
|
*/
|
|
62
628
|
export const isSubset = HS.isSubset;
|
|
63
629
|
/**
|
|
64
630
|
* Returns an `IterableIterator` of the values in the `HashSet`.
|
|
65
631
|
*
|
|
632
|
+
* Time complexity: **`O(1)`**
|
|
633
|
+
*
|
|
634
|
+
* @memberof HashSet
|
|
66
635
|
* @since 2.0.0
|
|
67
636
|
* @category getters
|
|
637
|
+
* @example
|
|
638
|
+
*
|
|
639
|
+
* ```ts
|
|
640
|
+
* import { HashSet, pipe } from "effect"
|
|
641
|
+
*
|
|
642
|
+
* const numberIterable = pipe(
|
|
643
|
+
* HashSet.make(0, 1, 1, 2), // HashSet.HashSet<number>
|
|
644
|
+
* HashSet.values // takes an HashSet<A> and returns an IterableIterator<A>
|
|
645
|
+
* )
|
|
646
|
+
*
|
|
647
|
+
* for (const number of numberIterable) {
|
|
648
|
+
* console.log(number) // it will logs: 0, 1, 2
|
|
649
|
+
* }
|
|
650
|
+
* ```
|
|
651
|
+
*
|
|
652
|
+
* @see Other `HashSet` getters are {@link module:HashSet.toValues} {@link module:HashSet.size}
|
|
68
653
|
*/
|
|
69
654
|
export const values = HS.values;
|
|
70
655
|
/**
|
|
71
656
|
* Returns an `Array` of the values within the `HashSet`.
|
|
72
657
|
*
|
|
658
|
+
* Time complexity: **`O(n)`** where n is the number of elements in the set
|
|
659
|
+
*
|
|
660
|
+
* @memberof HashSet
|
|
73
661
|
* @since 3.13.0
|
|
74
662
|
* @category getters
|
|
663
|
+
* @example
|
|
664
|
+
*
|
|
665
|
+
* ```ts
|
|
666
|
+
* import { HashSet, pipe } from "effect"
|
|
667
|
+
* import { deepStrictEqual } from "node:assert/strict"
|
|
668
|
+
*
|
|
669
|
+
* deepStrictEqual(
|
|
670
|
+
* pipe(
|
|
671
|
+
* HashSet.make(0, 1, 1, 2), // HashSet<number>
|
|
672
|
+
* HashSet.toValues // takes an HashSet<A> and returns an Array<A>
|
|
673
|
+
* ),
|
|
674
|
+
* Array.of(0, 1, 2)
|
|
675
|
+
* )
|
|
676
|
+
* ```
|
|
677
|
+
*
|
|
678
|
+
* @see Other `HashSet` getters are {@link module:HashSet.values} {@link module:HashSet.size}
|
|
75
679
|
*/
|
|
76
680
|
export const toValues = self => Array.from(values(self));
|
|
77
681
|
/**
|
|
78
682
|
* Calculates the number of values in the `HashSet`.
|
|
79
683
|
*
|
|
684
|
+
* Time complexity: **`O(1)`**
|
|
685
|
+
*
|
|
686
|
+
* @memberof HashSet
|
|
80
687
|
* @since 2.0.0
|
|
81
688
|
* @category getters
|
|
689
|
+
* @example
|
|
690
|
+
*
|
|
691
|
+
* ```ts
|
|
692
|
+
* import { HashSet, pipe } from "effect"
|
|
693
|
+
* import assert from "node:assert/strict"
|
|
694
|
+
*
|
|
695
|
+
* assert.deepStrictEqual(pipe(HashSet.empty(), HashSet.size), 0)
|
|
696
|
+
*
|
|
697
|
+
* assert.deepStrictEqual(
|
|
698
|
+
* pipe(HashSet.make(1, 2, 2, 3, 4, 3), HashSet.size),
|
|
699
|
+
* 4
|
|
700
|
+
* )
|
|
701
|
+
* ```
|
|
702
|
+
*
|
|
703
|
+
* @see Other `HashSet` getters are {@link module:HashSet.values} {@link module:HashSet.toValues}
|
|
82
704
|
*/
|
|
83
705
|
export const size = HS.size;
|
|
84
706
|
/**
|
|
85
|
-
*
|
|
707
|
+
* Creates a new mutable version of the `HashSet`
|
|
708
|
+
*
|
|
709
|
+
* When a `HashSet` is mutable, operations like {@link add} and {@link remove}
|
|
710
|
+
* modify the data structure in place instead of creating a new one, which is
|
|
711
|
+
* more efficient when performing multiple operations.
|
|
86
712
|
*
|
|
713
|
+
* @memberof HashSet
|
|
87
714
|
* @since 2.0.0
|
|
715
|
+
* @example
|
|
716
|
+
*
|
|
717
|
+
* ```ts
|
|
718
|
+
* import { HashSet } from "effect"
|
|
719
|
+
* import assert from "node:assert/strict"
|
|
720
|
+
*
|
|
721
|
+
* const UPPER_BOUND = 10_000
|
|
722
|
+
*
|
|
723
|
+
* const immutableSet = HashSet.empty<number>().pipe(HashSet.add(0))
|
|
724
|
+
*
|
|
725
|
+
* // Create a mutable version of the immutableSet
|
|
726
|
+
* const mutableSet = HashSet.beginMutation(immutableSet)
|
|
727
|
+
*
|
|
728
|
+
* for (let i = 1; i < UPPER_BOUND; i++) {
|
|
729
|
+
* // Operations now modify the set in place instead of creating new instances
|
|
730
|
+
* // This is more efficient when making multiple changes
|
|
731
|
+
* const pointerToMutableSet = HashSet.add(mutableSet, i)
|
|
732
|
+
*
|
|
733
|
+
* // the two sets have the same identity, hence `add` is mutating mutableSet and not returning a new HashSet instance
|
|
734
|
+
* assert(Object.is(mutableSet, pointerToMutableSet))
|
|
735
|
+
* assert.equal(HashSet.has(mutableSet, i), true) // `i` is in the mutableSet
|
|
736
|
+
* assert.equal(HashSet.has(immutableSet, i), false) // `i` is not in the immutableSet
|
|
737
|
+
* }
|
|
738
|
+
*
|
|
739
|
+
* const next = UPPER_BOUND + 1
|
|
740
|
+
* // When done, mark the set as immutable again
|
|
741
|
+
* HashSet.endMutation(mutableSet).pipe(
|
|
742
|
+
* HashSet.add(next) // since this returns a new HashSet, it will not be logged as part of the mutableSet
|
|
743
|
+
* )
|
|
744
|
+
* assert.equal(HashSet.has(mutableSet, next), false)
|
|
745
|
+
*
|
|
746
|
+
* console.log(HashSet.toValues(immutableSet)) // [0]
|
|
747
|
+
* console.log(HashSet.toValues(mutableSet).sort((a, b) => a - b)) // [0, 1, 2, 3, ...rest]
|
|
748
|
+
* ```
|
|
749
|
+
*
|
|
750
|
+
* @see Other `HashSet` mutations are {@link module:HashSet.add} {@link module:HashSet.remove} {@link module:HashSet.toggle} {@link module:HashSet.endMutation} {@link module:HashSet.mutate}
|
|
88
751
|
*/
|
|
89
752
|
export const beginMutation = HS.beginMutation;
|
|
90
753
|
/**
|
|
91
|
-
*
|
|
754
|
+
* Makes the `HashSet` immutable again.
|
|
755
|
+
*
|
|
756
|
+
* After calling `endMutation`, operations like {@link add} and {@link remove}
|
|
757
|
+
* will create new instances of the `HashSet` instead of modifying the existing
|
|
758
|
+
* one.
|
|
92
759
|
*
|
|
760
|
+
* @memberof HashSet
|
|
93
761
|
* @since 2.0.0
|
|
762
|
+
* @example
|
|
763
|
+
*
|
|
764
|
+
* ```ts
|
|
765
|
+
* import { HashSet } from "effect"
|
|
766
|
+
* import assert from "node:assert/strict"
|
|
767
|
+
*
|
|
768
|
+
* // Create a mutable set
|
|
769
|
+
* const mutableSet = HashSet.beginMutation(HashSet.empty<number>())
|
|
770
|
+
*
|
|
771
|
+
* // Add some elements to the mutable set
|
|
772
|
+
* HashSet.add(mutableSet, 1)
|
|
773
|
+
* HashSet.add(mutableSet, 2)
|
|
774
|
+
*
|
|
775
|
+
* // Before endMutation, operations modify the set in place
|
|
776
|
+
* const sameSet = HashSet.add(mutableSet, 3)
|
|
777
|
+
* assert(Object.is(mutableSet, sameSet)) // true - same object reference
|
|
778
|
+
* assert.deepStrictEqual(HashSet.toValues(mutableSet).sort(), [1, 2, 3])
|
|
779
|
+
*
|
|
780
|
+
* // Make the set immutable again
|
|
781
|
+
* const immutableSet = HashSet.endMutation(mutableSet)
|
|
782
|
+
*
|
|
783
|
+
* // endMutation returns the same set instance, now made immutable
|
|
784
|
+
* assert(Object.is(mutableSet, immutableSet)) // true - same object reference
|
|
785
|
+
*
|
|
786
|
+
* // After endMutation, operations create new instances
|
|
787
|
+
* const newSet = HashSet.add(immutableSet, 4)
|
|
788
|
+
* assert(!Object.is(immutableSet, newSet)) // false - different object references
|
|
789
|
+
*
|
|
790
|
+
* // The original set remains unchanged
|
|
791
|
+
* assert.deepStrictEqual(HashSet.toValues(immutableSet).sort(), [1, 2, 3])
|
|
792
|
+
*
|
|
793
|
+
* // The new set contains the added element
|
|
794
|
+
* assert.deepStrictEqual(HashSet.toValues(newSet).sort(), [1, 2, 3, 4])
|
|
795
|
+
* ```
|
|
796
|
+
*
|
|
797
|
+
* @see Other `HashSet` mutations are {@link module:HashSet.add} {@link module:HashSet.remove} {@link module:HashSet.toggle} {@link module:HashSet.beginMutation} {@link module:HashSet.mutate}
|
|
94
798
|
*/
|
|
95
799
|
export const endMutation = HS.endMutation;
|
|
96
800
|
/**
|
|
97
801
|
* Mutates the `HashSet` within the context of the provided function.
|
|
98
802
|
*
|
|
803
|
+
* You can consider it a functional abstraction on top of the lower-level
|
|
804
|
+
* mutation primitives of {@link module:HashSet.beginMutation} `->` `mutable
|
|
805
|
+
* context` `->` {@link HashSet.endMutation}.
|
|
806
|
+
*
|
|
807
|
+
* @memberof HashSet
|
|
99
808
|
* @since 2.0.0
|
|
809
|
+
* @example
|
|
810
|
+
*
|
|
811
|
+
* ```ts
|
|
812
|
+
* // Syntax
|
|
813
|
+
* import { HashSet, pipe } from "effect"
|
|
814
|
+
*
|
|
815
|
+
* // with data-last, a.k.a. pipeable API
|
|
816
|
+
* pipe(
|
|
817
|
+
* HashSet.make(1, 2, 3),
|
|
818
|
+
* HashSet.mutate((set) => {
|
|
819
|
+
* HashSet.add(set, 4)
|
|
820
|
+
* HashSet.remove(set, 1)
|
|
821
|
+
* })
|
|
822
|
+
* )
|
|
823
|
+
*
|
|
824
|
+
* // or piped with the pipe function
|
|
825
|
+
* HashSet.make(1, 2, 3).pipe(
|
|
826
|
+
* HashSet.mutate((set) => {
|
|
827
|
+
* HashSet.add(set, 4)
|
|
828
|
+
* HashSet.remove(set, 1)
|
|
829
|
+
* })
|
|
830
|
+
* )
|
|
831
|
+
*
|
|
832
|
+
* // or with data-first API
|
|
833
|
+
* HashSet.mutate(HashSet.make(1, 2, 3), (set) => {
|
|
834
|
+
* HashSet.add(set, 4)
|
|
835
|
+
* HashSet.remove(set, 1)
|
|
836
|
+
* })
|
|
837
|
+
* ```
|
|
838
|
+
*
|
|
839
|
+
* @see Other `HashSet` mutations are {@link module:HashSet.add} {@link module:HashSet.remove} {@link module:HashSet.toggle} {@link module:HashSet.beginMutation} {@link module:HashSet.endMutation}
|
|
100
840
|
*/
|
|
101
841
|
export const mutate = HS.mutate;
|
|
102
842
|
/**
|
|
103
843
|
* Adds a value to the `HashSet`.
|
|
104
844
|
*
|
|
845
|
+
* Time complexity: **`O(1)`** average
|
|
846
|
+
*
|
|
847
|
+
* @remarks
|
|
848
|
+
* Remember that a `HashSet` is a collection of unique values, so adding a value
|
|
849
|
+
* that already exists in the `HashSet` will not add a duplicate.
|
|
850
|
+
*
|
|
851
|
+
* Remember that HashSet is an immutable data structure, so the `add` function,
|
|
852
|
+
* like all other functions that modify the HashSet, will return a new HashSet
|
|
853
|
+
* with the added value.
|
|
854
|
+
* @memberof HashSet
|
|
105
855
|
* @since 2.0.0
|
|
856
|
+
* @example
|
|
857
|
+
*
|
|
858
|
+
* ```ts
|
|
859
|
+
* // Syntax
|
|
860
|
+
* import { HashSet, pipe } from "effect"
|
|
861
|
+
*
|
|
862
|
+
* // with data-last, a.k.a. pipeable API
|
|
863
|
+
* pipe(HashSet.empty(), HashSet.add(0), HashSet.add(0))
|
|
864
|
+
*
|
|
865
|
+
* // or piped with the pipe function
|
|
866
|
+
* HashSet.empty().pipe(HashSet.add(0))
|
|
867
|
+
*
|
|
868
|
+
* // or with data-first API
|
|
869
|
+
* HashSet.add(HashSet.empty(), 0)
|
|
870
|
+
* ```
|
|
871
|
+
*
|
|
872
|
+
* @see Other `HashSet` mutations are {@link module:HashSet.remove} {@link module:HashSet.toggle} {@link module:HashSet.beginMutation} {@link module:HashSet.endMutation} {@link module:HashSet.mutate}
|
|
106
873
|
*/
|
|
107
874
|
export const add = HS.add;
|
|
108
875
|
/**
|
|
109
876
|
* Removes a value from the `HashSet`.
|
|
110
877
|
*
|
|
878
|
+
* Time complexity: **`O(1)`** average
|
|
879
|
+
*
|
|
880
|
+
* @memberof HashSet
|
|
111
881
|
* @since 2.0.0
|
|
882
|
+
* @example
|
|
883
|
+
*
|
|
884
|
+
* ```ts
|
|
885
|
+
* // Syntax
|
|
886
|
+
* import { HashSet, pipe } from "effect"
|
|
887
|
+
*
|
|
888
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
889
|
+
* pipe(HashSet.make(0, 1, 2), HashSet.remove(0))
|
|
890
|
+
*
|
|
891
|
+
* // or piped with the pipe function
|
|
892
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.remove(0))
|
|
893
|
+
*
|
|
894
|
+
* // or with `data-first` API
|
|
895
|
+
* HashSet.remove(HashSet.make(0, 1, 2), 0)
|
|
896
|
+
* ```
|
|
897
|
+
*
|
|
898
|
+
* @see Other `HashSet` mutations are {@link module:HashSet.add} {@link module:HashSet.toggle} {@link module:HashSet.beginMutation} {@link module:HashSet.endMutation} {@link module:HashSet.mutate}
|
|
112
899
|
*/
|
|
113
900
|
export const remove = HS.remove;
|
|
114
901
|
/**
|
|
115
|
-
* Computes the set difference between this `HashSet` and the
|
|
116
|
-
* `Iterable<A>`.
|
|
902
|
+
* Computes the set difference `(A - B)` between this `HashSet` and the
|
|
903
|
+
* specified `Iterable<A>`.
|
|
904
|
+
*
|
|
905
|
+
* Time complexity: **`O(n)`** where n is the number of elements in the set
|
|
117
906
|
*
|
|
118
907
|
* **NOTE**: the hash and equal of the values in both the set and the iterable
|
|
119
|
-
* must be the same
|
|
908
|
+
* must be the same; meaning we cannot compute a difference between a `HashSet
|
|
909
|
+
* of bananas` and a `HashSet of elephants` as they are not the same type and
|
|
910
|
+
* won't implement the Equal trait in the same way.
|
|
120
911
|
*
|
|
912
|
+
* @memberof HashSet
|
|
121
913
|
* @since 2.0.0
|
|
914
|
+
* @example
|
|
915
|
+
*
|
|
916
|
+
* ```ts
|
|
917
|
+
* // Syntax
|
|
918
|
+
* import { HashSet, pipe } from "effect"
|
|
919
|
+
*
|
|
920
|
+
* // with data-last, a.k.a. pipeable API
|
|
921
|
+
* pipe(HashSet.make(1, 2, 3), HashSet.difference(HashSet.make(3, 4, 5)))
|
|
922
|
+
*
|
|
923
|
+
* // or piped with the pipe function
|
|
924
|
+
* HashSet.make(1, 2, 3).pipe(HashSet.difference(HashSet.make(3, 4, 5)))
|
|
925
|
+
*
|
|
926
|
+
* // or with data-first API
|
|
927
|
+
* HashSet.difference(HashSet.make(1, 2, 3), HashSet.make(3, 4, 5))
|
|
928
|
+
* ```
|
|
929
|
+
*
|
|
930
|
+
* @see Other `HashSet` operations are {@link module:HashSet.intersection} {@link module:HashSet.union}
|
|
122
931
|
*/
|
|
123
932
|
export const difference = HS.difference;
|
|
124
933
|
/**
|
|
125
934
|
* Returns a `HashSet` of values which are present in both this set and that
|
|
126
|
-
* `Iterable<A>`.
|
|
935
|
+
* `Iterable<A>`. Computes set intersection (A ∩ B)
|
|
936
|
+
*
|
|
937
|
+
* Time complexity: **`O(n)`** where n is the number of elements in the smaller
|
|
938
|
+
* set
|
|
127
939
|
*
|
|
128
940
|
* **NOTE**: the hash and equal of the values in both the set and the iterable
|
|
129
941
|
* must be the same.
|
|
130
942
|
*
|
|
943
|
+
* @memberof HashSet
|
|
131
944
|
* @since 2.0.0
|
|
945
|
+
* @example
|
|
946
|
+
*
|
|
947
|
+
* ```ts
|
|
948
|
+
* // Syntax
|
|
949
|
+
* import { HashSet, pipe } from "effect"
|
|
950
|
+
*
|
|
951
|
+
* // with data-last, a.k.a. pipeable API
|
|
952
|
+
* pipe(HashSet.make(1, 2, 3), HashSet.intersection(HashSet.make(2, 3, 4)))
|
|
953
|
+
*
|
|
954
|
+
* // or piped with the pipe function
|
|
955
|
+
* HashSet.make(1, 2, 3).pipe(HashSet.intersection(HashSet.make(2, 3, 4)))
|
|
956
|
+
*
|
|
957
|
+
* // or with data-first API
|
|
958
|
+
* HashSet.intersection(HashSet.make(1, 2, 3), HashSet.make(2, 3, 4))
|
|
959
|
+
* ```
|
|
960
|
+
*
|
|
961
|
+
* @see Other `HashSet` operations are {@link module:HashSet.difference} {@link module:HashSet.union}
|
|
132
962
|
*/
|
|
133
963
|
export const intersection = HS.intersection;
|
|
134
964
|
/**
|
|
135
|
-
* Computes the set union `(
|
|
965
|
+
* Computes the set union `( self ∪ that )` between this `HashSet` and the
|
|
136
966
|
* specified `Iterable<A>`.
|
|
137
967
|
*
|
|
968
|
+
* Time complexity: **`O(n)`** where n is the number of elements in the set
|
|
969
|
+
*
|
|
138
970
|
* **NOTE**: the hash and equal of the values in both the set and the iterable
|
|
139
971
|
* must be the same.
|
|
140
972
|
*
|
|
973
|
+
* @memberof HashSet
|
|
141
974
|
* @since 2.0.0
|
|
975
|
+
* @example
|
|
976
|
+
*
|
|
977
|
+
* ```ts
|
|
978
|
+
* // Syntax
|
|
979
|
+
* import { HashSet, pipe } from "effect"
|
|
980
|
+
*
|
|
981
|
+
* // with data-last, a.k.a. pipeable API
|
|
982
|
+
* pipe(HashSet.make(1, 2, 3), HashSet.union(HashSet.make(3, 4, 5)))
|
|
983
|
+
*
|
|
984
|
+
* // or piped with the pipe function
|
|
985
|
+
* HashSet.make(1, 2, 3).pipe(HashSet.union(HashSet.make(3, 4, 5)))
|
|
986
|
+
*
|
|
987
|
+
* // or with data-first API
|
|
988
|
+
* HashSet.union(HashSet.make(1, 2, 3), HashSet.make(3, 4, 5))
|
|
989
|
+
* ```
|
|
990
|
+
*
|
|
991
|
+
* @see Other `HashSet` operations are {@link module:HashSet.difference} {@link module:HashSet.intersection}
|
|
142
992
|
*/
|
|
143
993
|
export const union = HS.union;
|
|
144
994
|
/**
|
|
@@ -146,42 +996,196 @@ export const union = HS.union;
|
|
|
146
996
|
* will be removed from the `HashSet`, otherwise the value will be added to the
|
|
147
997
|
* `HashSet`.
|
|
148
998
|
*
|
|
999
|
+
* Time complexity: **`O(1)`** average
|
|
1000
|
+
*
|
|
1001
|
+
* @memberof HashSet
|
|
149
1002
|
* @since 2.0.0
|
|
1003
|
+
* @example
|
|
1004
|
+
*
|
|
1005
|
+
* ```ts
|
|
1006
|
+
* // Syntax
|
|
1007
|
+
* import { HashSet, pipe } from "effect"
|
|
1008
|
+
*
|
|
1009
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1010
|
+
* pipe(HashSet.make(0, 1, 2), HashSet.toggle(0))
|
|
1011
|
+
*
|
|
1012
|
+
* // or piped with the pipe function
|
|
1013
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.toggle(0))
|
|
1014
|
+
*
|
|
1015
|
+
* // or with `data-first` API
|
|
1016
|
+
* HashSet.toggle(HashSet.make(0, 1, 2), 0)
|
|
1017
|
+
* ```
|
|
1018
|
+
*
|
|
1019
|
+
* @returns A new `HashSet` where the toggled value is being either added or
|
|
1020
|
+
* removed based on the initial `HashSet` state.
|
|
1021
|
+
* @see Other `HashSet` mutations are {@link module:HashSet.add} {@link module:HashSet.remove} {@link module:HashSet.beginMutation} {@link module:HashSet.endMutation} {@link module:HashSet.mutate}
|
|
150
1022
|
*/
|
|
151
1023
|
export const toggle = HS.toggle;
|
|
152
1024
|
/**
|
|
153
1025
|
* Maps over the values of the `HashSet` using the specified function.
|
|
154
1026
|
*
|
|
1027
|
+
* The time complexity is of **`O(n)`**.
|
|
1028
|
+
*
|
|
1029
|
+
* @memberof HashSet
|
|
155
1030
|
* @since 2.0.0
|
|
156
1031
|
* @category mapping
|
|
1032
|
+
* @example
|
|
1033
|
+
*
|
|
1034
|
+
* ```ts
|
|
1035
|
+
* // Syntax
|
|
1036
|
+
* import { HashSet, pipe } from "effect"
|
|
1037
|
+
*
|
|
1038
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1039
|
+
* pipe(
|
|
1040
|
+
* HashSet.make(0, 1, 2), // HashSet.HashSet<number>
|
|
1041
|
+
* HashSet.map(String) // HashSet.HashSet<string>
|
|
1042
|
+
* )
|
|
1043
|
+
*
|
|
1044
|
+
* // or piped with the pipe method
|
|
1045
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.map(String))
|
|
1046
|
+
*
|
|
1047
|
+
* // or with `data-first` API
|
|
1048
|
+
* HashSet.map(HashSet.make(0, 1, 2), String)
|
|
1049
|
+
* ```
|
|
157
1050
|
*/
|
|
158
1051
|
export const map = HS.map;
|
|
159
1052
|
/**
|
|
160
1053
|
* Chains over the values of the `HashSet` using the specified function.
|
|
161
1054
|
*
|
|
1055
|
+
* The time complexity is of **`O(n)`**.
|
|
1056
|
+
*
|
|
1057
|
+
* @memberof HashSet
|
|
162
1058
|
* @since 2.0.0
|
|
163
1059
|
* @category sequencing
|
|
1060
|
+
* @example
|
|
1061
|
+
*
|
|
1062
|
+
* ```ts
|
|
1063
|
+
* // Syntax
|
|
1064
|
+
* import { HashSet, pipe } from "effect"
|
|
1065
|
+
*
|
|
1066
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1067
|
+
* pipe(
|
|
1068
|
+
* HashSet.make(0, 1, 2), // HashSet.HashSet<number>
|
|
1069
|
+
* HashSet.flatMap((n) => Array.of(String(n))) // HashSet.HashSet<string>
|
|
1070
|
+
* )
|
|
1071
|
+
*
|
|
1072
|
+
* // or piped with the pipe method
|
|
1073
|
+
* HashSet.make(0, 1, 2) // HashSet.HashSet<number>
|
|
1074
|
+
* .pipe(
|
|
1075
|
+
* HashSet.flatMap((n) => Array.of(String(n))) // HashSet.HashSet<string>
|
|
1076
|
+
* )
|
|
1077
|
+
*
|
|
1078
|
+
* // or with `data-first` API
|
|
1079
|
+
* HashSet.flatMap(HashSet.make(0, 1, 2), (n) => Array.of(String(n)))
|
|
1080
|
+
* ```
|
|
164
1081
|
*/
|
|
165
1082
|
export const flatMap = HS.flatMap;
|
|
166
1083
|
/**
|
|
167
1084
|
* Applies the specified function to the values of the `HashSet`.
|
|
168
1085
|
*
|
|
1086
|
+
* The time complexity is of **`O(n)`**.
|
|
1087
|
+
*
|
|
1088
|
+
* @memberof HashSet
|
|
169
1089
|
* @since 2.0.0
|
|
170
1090
|
* @category traversing
|
|
1091
|
+
* @example
|
|
1092
|
+
*
|
|
1093
|
+
* ```ts
|
|
1094
|
+
* // Syntax
|
|
1095
|
+
* import { HashSet, pipe } from "effect"
|
|
1096
|
+
*
|
|
1097
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1098
|
+
* pipe(HashSet.make(0, 1, 2), HashSet.forEach(console.log)) // logs: 0 1 2
|
|
1099
|
+
*
|
|
1100
|
+
* // or piped with the pipe method
|
|
1101
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.forEach(console.log)) // logs: 0 1 2
|
|
1102
|
+
*
|
|
1103
|
+
* // or with `data-first` API
|
|
1104
|
+
* HashSet.forEach(HashSet.make(0, 1, 2), console.log) // logs: 0 1 2
|
|
1105
|
+
* ```
|
|
171
1106
|
*/
|
|
172
1107
|
export const forEach = HS.forEach;
|
|
173
1108
|
/**
|
|
174
1109
|
* Reduces the specified state over the values of the `HashSet`.
|
|
175
1110
|
*
|
|
1111
|
+
* The time complexity is of **`O(n)`**.
|
|
1112
|
+
*
|
|
1113
|
+
* @memberof HashSet
|
|
176
1114
|
* @since 2.0.0
|
|
177
1115
|
* @category folding
|
|
1116
|
+
* @example
|
|
1117
|
+
*
|
|
1118
|
+
* ```ts
|
|
1119
|
+
* // Syntax
|
|
1120
|
+
* import { HashSet, pipe } from "effect"
|
|
1121
|
+
*
|
|
1122
|
+
* const sum = (a: number, b: number): number => a + b
|
|
1123
|
+
*
|
|
1124
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1125
|
+
* pipe(HashSet.make(0, 1, 2), HashSet.reduce(0, sum))
|
|
1126
|
+
*
|
|
1127
|
+
* // or with the pipe method
|
|
1128
|
+
* HashSet.make(0, 1, 2).pipe(HashSet.reduce(0, sum))
|
|
1129
|
+
*
|
|
1130
|
+
* // or with `data-first` API
|
|
1131
|
+
* HashSet.reduce(HashSet.make(0, 1, 2), 0, sum)
|
|
1132
|
+
* ```
|
|
178
1133
|
*/
|
|
179
1134
|
export const reduce = HS.reduce;
|
|
180
1135
|
/**
|
|
181
1136
|
* Filters values out of a `HashSet` using the specified predicate.
|
|
182
1137
|
*
|
|
1138
|
+
* The time complexity is of **`O(n)`**.
|
|
1139
|
+
*
|
|
1140
|
+
* @memberof HashSet
|
|
183
1141
|
* @since 2.0.0
|
|
184
1142
|
* @category filtering
|
|
1143
|
+
* @example
|
|
1144
|
+
*
|
|
1145
|
+
* ```ts
|
|
1146
|
+
* // Syntax with Predicate
|
|
1147
|
+
* import { HashSet, type Predicate, pipe } from "effect"
|
|
1148
|
+
*
|
|
1149
|
+
* const filterPositiveNumbers: Predicate.Predicate<number> = (n) => n > 0
|
|
1150
|
+
*
|
|
1151
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1152
|
+
* pipe(
|
|
1153
|
+
* HashSet.make(-2, -1, 0, 1, 2),
|
|
1154
|
+
* HashSet.filter(filterPositiveNumbers)
|
|
1155
|
+
* )
|
|
1156
|
+
*
|
|
1157
|
+
* // or with the pipe method
|
|
1158
|
+
* HashSet.make(-2, -1, 0, 1, 2).pipe(HashSet.filter(filterPositiveNumbers))
|
|
1159
|
+
*
|
|
1160
|
+
* // or with `data-first` API
|
|
1161
|
+
* HashSet.filter(HashSet.make(-2, -1, 0, 1, 2), filterPositiveNumbers)
|
|
1162
|
+
* ```
|
|
1163
|
+
*
|
|
1164
|
+
* @example
|
|
1165
|
+
*
|
|
1166
|
+
* ```ts
|
|
1167
|
+
* /// Syntax with Refinement
|
|
1168
|
+
* import { HashSet, pipe } from "effect"
|
|
1169
|
+
*
|
|
1170
|
+
* const stringRefinement = (value: unknown): value is string =>
|
|
1171
|
+
* typeof value === "string"
|
|
1172
|
+
*
|
|
1173
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1174
|
+
* pipe(
|
|
1175
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier"), // // HashSet.HashSet<number | string>
|
|
1176
|
+
* HashSet.filter(stringRefinement)
|
|
1177
|
+
* ) // HashSet.HashSet<string>
|
|
1178
|
+
*
|
|
1179
|
+
* // or with the pipe method
|
|
1180
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier") // HashSet.HashSet<number | string>
|
|
1181
|
+
* .pipe(HashSet.filter(stringRefinement)) // HashSet.HashSet<string>
|
|
1182
|
+
*
|
|
1183
|
+
* // or with `data-first` API
|
|
1184
|
+
* HashSet.filter(
|
|
1185
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier"), // HashSet.HashSet<number | string>
|
|
1186
|
+
* stringRefinement
|
|
1187
|
+
* ) // HashSet.HashSet<string>
|
|
1188
|
+
* ```
|
|
185
1189
|
*/
|
|
186
1190
|
export const filter = HS.filter;
|
|
187
1191
|
/**
|
|
@@ -191,8 +1195,59 @@ export const filter = HS.filter;
|
|
|
191
1195
|
* right side of the resulting `Tuple`, otherwise the value will be placed into
|
|
192
1196
|
* the left side.
|
|
193
1197
|
*
|
|
1198
|
+
* Time complexity is of **`O(n)`**.
|
|
1199
|
+
*
|
|
1200
|
+
* @memberof HashSet
|
|
194
1201
|
* @since 2.0.0
|
|
195
1202
|
* @category partitioning
|
|
1203
|
+
* @example
|
|
1204
|
+
*
|
|
1205
|
+
* ```ts
|
|
1206
|
+
* // Syntax with Predicate
|
|
1207
|
+
* import { HashSet, pipe, Predicate } from "effect"
|
|
1208
|
+
*
|
|
1209
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1210
|
+
* pipe(
|
|
1211
|
+
* HashSet.make(0, 1, 2, 3, 4, 5),
|
|
1212
|
+
* HashSet.partition((n) => n % 2 === 0)
|
|
1213
|
+
* )
|
|
1214
|
+
*
|
|
1215
|
+
* // or with the pipe method
|
|
1216
|
+
* HashSet.make(0, 1, 2, 3, 4, 5).pipe(
|
|
1217
|
+
* HashSet.partition((n) => n % 2 === 0)
|
|
1218
|
+
* )
|
|
1219
|
+
*
|
|
1220
|
+
* // or with `data-first` API
|
|
1221
|
+
* HashSet.partition(HashSet.make(0, 1, 2, 3, 4, 5), (n) => n % 2 === 0)
|
|
1222
|
+
* ```
|
|
1223
|
+
*
|
|
1224
|
+
* @example
|
|
1225
|
+
*
|
|
1226
|
+
* ```ts
|
|
1227
|
+
* // Syntax with Refinement
|
|
1228
|
+
* import { HashSet, pipe, Predicate } from "effect"
|
|
1229
|
+
*
|
|
1230
|
+
* const stringRefinement: Predicate.Refinement<string | number, string> = (
|
|
1231
|
+
* value
|
|
1232
|
+
* ) => typeof value === "string"
|
|
1233
|
+
*
|
|
1234
|
+
* // with `data-last`, a.k.a. `pipeable` API
|
|
1235
|
+
* pipe(
|
|
1236
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier"),
|
|
1237
|
+
* HashSet.partition(stringRefinement)
|
|
1238
|
+
* )
|
|
1239
|
+
*
|
|
1240
|
+
* // or with the pipe method
|
|
1241
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier").pipe(
|
|
1242
|
+
* HashSet.partition(stringRefinement)
|
|
1243
|
+
* )
|
|
1244
|
+
*
|
|
1245
|
+
* // or with `data-first` API
|
|
1246
|
+
* HashSet.partition(
|
|
1247
|
+
* HashSet.make(1, "unos", 2, "two", 3, "trois", 4, "vier"),
|
|
1248
|
+
* stringRefinement
|
|
1249
|
+
* )
|
|
1250
|
+
* ```
|
|
196
1251
|
*/
|
|
197
1252
|
export const partition = HS.partition;
|
|
198
1253
|
//# sourceMappingURL=HashSet.js.map
|