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