# HashSet
import { Aside } from "@astrojs/starlight/components"
A HashSet represents an **unordered** collection of **unique** values with efficient lookup, insertion and removal operations.
The Effect library provides two versions of this structure:
- [HashSet](/docs/data-types/hash-set/#hashset) - Immutable
- [MutableHashSet](/docs/data-types/hash-set/#mutablehashset) - Mutable
Both versions provide constant-time operations on average. The main difference is how they handle changes: one returns new sets, the other modifies the original.
### Why use HashSet?
HashSet solves the problem of maintaining an **unsorted collection where each value appears exactly once**, with fast operations for checking membership and adding/removing values.
Some common use cases include:
- Tracking unique items (e.g., users who have completed an action)
- Efficiently testing for membership in a collection
- Performing set operations like union, intersection, and difference
- Eliminating duplicates from a collection
### When to use HashSet Instead of other collections
Choose HashSet (either variant) over other collections when:
- You need to ensure elements are unique
- You frequently need to check if an element exists in the collection
- You need to perform set operations like union, intersection, and difference
- The order of elements doesn't matter to your use case
Choose other collections when:
- You need to maintain insertion order (use `List` or `Array`)
- You need key-value associations (use `HashMap` or `MutableHashMap`)
- You need to frequently access elements by index (use `Array`)
### Choosing between immutable and mutable variants
Effect offers both immutable and mutable versions to support different coding styles and performance needs.
**HashSet**
This version never modifies the original set. Instead, it returns a new set for every change.
Characteristics:
- Operations return new instances instead of modifying the original
- Previous states are preserved
- Thread-safe by design
- Ideal for functional programming patterns
- Suitable for sharing across different parts of your application
**MutableHashSet**
This version allows direct updates: adding and removing values changes the set in place.
Characteristics:
- Operations modify the original set directly
- More efficient when building sets incrementally
- Requires careful handling to avoid unexpected side effects
- Better performance in scenarios with many modifications
- Ideal for localized use where mutations won't cause issues elsewhere
### When to use each variant
Use **HashSet** when:
- You need predictable behavior with no side effects
- You want to preserve previous states of your data
- You're sharing sets across different parts of your application
- You prefer functional programming patterns
- You need fiber safety in concurrent environments
Use **MutableHashSet** when:
- Performance is critical, and you need to avoid creating new instances
- You're building a collection incrementally with many additions/removals
- You're working in a controlled scope where mutation is safe
- You need to optimize memory usage in performance-critical code
### Hybrid approach
You can apply multiple updates to a `HashSet` in a temporary mutable context using `HashSet.mutate`. This allows you to perform several changes at once without modifying the original set.
**Example** (Batching changes without mutating the original)
```ts twoslash
import { HashSet } from "effect"
// Create an immutable HashSet
const original = HashSet.make(1, 2, 3)
// Apply several updates inside a temporary mutable draft
const modified = HashSet.mutate(original, (draft) => {
HashSet.add(draft, 4)
HashSet.add(draft, 5)
HashSet.remove(draft, 1)
})
console.log(HashSet.toValues(original))
// Output: [1, 2, 3] - original remains unchanged
console.log(HashSet.toValues(modified))
// Output: [2, 3, 4, 5] - changes applied to a new version
```
## Performance characteristics
Both `HashSet` and `MutableHashSet` offer similar average-time performance for core operations:
| Operation | HashSet | MutableHashSet | Description |
| -------------- | ------------ | -------------- | ------------------------------- |
| Lookup | O(1) average | O(1) average | Check if a value exists |
| Insertion | O(1) average | O(1) average | Add a value |
| Removal | O(1) average | O(1) average | Remove a value |
| Iteration | O(n) | O(n) | Iterate over all values |
| Set operations | O(n) | O(n) | Union, intersection, difference |
The main difference is how updates are handled:
- **HashSet** returns a new set for each change. This can be slower if many changes are made in a row.
- **MutableHashSet** updates the same set in place. This is usually faster when performing many changes.
## Equality and uniqueness
Both `HashSet` and `MutableHashSet` use Effect's [`Equal`](/docs/trait/equal/) trait to determine if two elements are the same. This ensures that each value appears only once in the set.
- **Primitive values** (like numbers or strings) are compared by value, similar to the `===` operator.
- **Objects and custom types** must implement the `Equal` interface to define what it means for two instances to be equal. If no implementation is provided, equality falls back to reference comparison.
**Example** (Using custom equality and hashing)
```ts twoslash
import { Equal, Hash, HashSet } from "effect"
// Define a custom class that implements the Equal interface
class Person implements Equal.Equal {
constructor(
readonly id: number,
readonly name: string,
readonly age: number
) {}
// Two Person instances are equal if their id, name, and age match
[Equal.symbol](that: Equal.Equal): boolean {
if (that instanceof Person) {
return (
Equal.equals(this.id, that.id) &&
Equal.equals(this.name, that.name) &&
Equal.equals(this.age, that.age)
)
}
return false
}
// Hash code is based on the id (must match the equality logic)
[Hash.symbol](): number {
return Hash.hash(this.id)
}
}
// Add two different instances with the same content
const set = HashSet.empty().pipe(
HashSet.add(new Person(1, "Alice", 30)),
HashSet.add(new Person(1, "Alice", 30))
)
// Only one instance is kept
console.log(HashSet.size(set))
// Output: 1
```
### Simplifying Equality with Data and Schema
Effect's [`Data`](/docs/data-types/data/) and [`Schema.Data`](/docs/schema/effect-data-types/#interop-with-data) modules implement `Equal` for you automatically, based on structural equality.
**Example** (Using `Data.struct`)
```ts twoslash
import { Data, Equal, HashSet, pipe } from "effect"
// Define two records with the same content
const person1 = Data.struct({ id: 1, name: "Alice", age: 30 })
const person2 = Data.struct({ id: 1, name: "Alice", age: 30 })
// They are different object references
console.log(Object.is(person1, person2))
// Output: false
// But they are equal in value (based on content)
console.log(Equal.equals(person1, person2))
// Output: true
// Add both to a HashSet — only one will be stored
const set = pipe(
HashSet.empty(),
HashSet.add(person1),
HashSet.add(person2)
)
console.log(HashSet.size(set))
// Output: 1
```
**Example** (Using `Schema.Data`)
```ts twoslash
import { Equal, MutableHashSet, Schema } from "effect"
// Define a schema that describes the structure of a Person
const PersonSchema = Schema.Data(
Schema.Struct({
id: Schema.Number,
name: Schema.String,
age: Schema.Number
})
)
// Decode values from plain objects
const Person = Schema.decodeSync(PersonSchema)
const person1 = Person({ id: 1, name: "Alice", age: 30 })
const person2 = Person({ id: 1, name: "Alice", age: 30 })
// person1 and person2 are different instances but equal in value
console.log(Equal.equals(person1, person2))
// Output: true
// Add both to a MutableHashSet — only one will be stored
const set = MutableHashSet.empty().pipe(
MutableHashSet.add(person1),
MutableHashSet.add(person2)
)
console.log(MutableHashSet.size(set))
// Output: 1
```
## HashSet
A `HashSet<A>` is an **immutable**, **unordered** collection of **unique** values.
It guarantees that each value appears only once and supports fast operations like lookup, insertion, and removal.
Any operation that would modify the set (like adding or removing a value) returns a new `HashSet`, leaving the original unchanged.
### Operations
| Category | Operation | Description | Time Complexity |
| ------------ | -------------------------------------------------------------------------------------- | ------------------------------------------- | --------------- |
| constructors | [empty](https://effect-ts.github.io/effect/effect/HashSet.ts.html#empty) | Creates an empty HashSet | O(1) |
| constructors | [fromIterable](https://effect-ts.github.io/effect/effect/HashSet.ts.html#fromiterable) | Creates a HashSet from an iterable | O(n) |
| constructors | [make](https://effect-ts.github.io/effect/effect/HashSet.ts.html#make) | Creates a HashSet from multiple values | O(n) |
| elements | [has](https://effect-ts.github.io/effect/effect/HashSet.ts.html#has) | Checks if a value exists in the set | O(1) avg |
| elements | [some](https://effect-ts.github.io/effect/effect/HashSet.ts.html#some) | Checks if any element satisfies a predicate | O(n) |
| elements | [every](https://effect-ts.github.io/effect/effect/HashSet.ts.html#every) | Checks if all elements satisfy a predicate | O(n) |
| elements | [isSubset](https://effect-ts.github.io/effect/effect/HashSet.ts.html#issubset) | Checks if a set is a subset of another | O(n) |
| getters | [values](https://effect-ts.github.io/effect/effect/HashSet.ts.html#values) | Gets an `Iterator` of all values | O(1) |
| getters | [toValues](https://effect-ts.github.io/effect/effect/HashSet.ts.html#tovalues) | Gets an `Array` of all values | O(n) |
| getters | [size](https://effect-ts.github.io/effect/effect/HashSet.ts.html#size) | Gets the number of elements | O(1) |
| mutations | [add](https://effect-ts.github.io/effect/effect/HashSet.ts.html#add) | Adds a value to the set | O(1) avg |
| mutations | [remove](https://effect-ts.github.io/effect/effect/HashSet.ts.html#remove) | Removes a value from the set | O(1) avg |
| mutations | [toggle](https://effect-ts.github.io/effect/effect/HashSet.ts.html#toggle) | Toggles a value's presence | O(1) avg |
| operations | [difference](https://effect-ts.github.io/effect/effect/HashSet.ts.html#difference) | Computes set difference (A - B) | O(n) |
| operations | [intersection](https://effect-ts.github.io/effect/effect/HashSet.ts.html#intersection) | Computes set intersection (A ∩ B) | O(n) |
| operations | [union](https://effect-ts.github.io/effect/effect/HashSet.ts.html#union) | Computes set union (A ∪ B) | O(n) |
| mapping | [map](https://effect-ts.github.io/effect/effect/HashSet.ts.html#map) | Transforms each element | O(n) |
| sequencing | [flatMap](https://effect-ts.github.io/effect/effect/HashSet.ts.html#flatmap) | Transforms and flattens elements | O(n) |
| traversing | [forEach](https://effect-ts.github.io/effect/effect/HashSet.ts.html#foreach) | Applies a function to each element | O(n) |
| folding | [reduce](https://effect-ts.github.io/effect/effect/HashSet.ts.html#reduce) | Reduces the set to a single value | O(n) |
| filtering | [filter](https://effect-ts.github.io/effect/effect/HashSet.ts.html#filter) | Keeps elements that satisfy a predicate | O(n) |
| partitioning | [partition](https://effect-ts.github.io/effect/effect/HashSet.ts.html#partition) | Splits into two sets by a predicate | O(n) |
**Example** (Basic creation and operations)
```ts twoslash
import { HashSet } from "effect"
// Create an initial set with 3 values
const set1 = HashSet.make(1, 2, 3)
// Add a value (returns a new set)
const set2 = HashSet.add(set1, 4)
// The original set is unchanged
console.log(HashSet.toValues(set1))
// Output: [1, 2, 3]
console.log(HashSet.toValues(set2))
// Output: [1, 2, 3, 4]
// Perform set operations with another set
const set3 = HashSet.make(3, 4, 5)
// Combine both sets
const union = HashSet.union(set2, set3)
console.log(HashSet.toValues(union))
// Output: [1, 2, 3, 4, 5]
// Shared values
const intersection = HashSet.intersection(set2, set3)
console.log(HashSet.toValues(intersection))
// Output: [3, 4]
// Values only in set2
const difference = HashSet.difference(set2, set3)
console.log(HashSet.toValues(difference))
// Output: [1, 2]
```
**Example** (Chaining with `pipe`)
```ts twoslash
import { HashSet, pipe } from "effect"
const result = pipe(
// Duplicates are ignored
HashSet.make(1, 2, 2, 3, 4, 5, 5),
// Keep even numbers
HashSet.filter((n) => n % 2 === 0),
// Double each value
HashSet.map((n) => n * 2),
// Convert to array
HashSet.toValues
)
console.log(result)
// Output: [4, 8]
```
## MutableHashSet
A `MutableHashSet<A>` is a **mutable**, **unordered** collection of **unique** values.
Unlike `HashSet`, it allows direct modifications, operations like `add`, `remove`, and `clear` update the original set instead of returning a new one.
This mutability can improve performance when you need to build or update a set repeatedly, especially within a local or isolated scope.
### Operations
| Category | Operation | Description | Complexity |
| ------------ | --------------------------------------------------------------------------------------------- | ----------------------------------- | ---------- |
| constructors | [empty](https://effect-ts.github.io/effect/effect/MutableHashSet.ts.html#empty) | Creates an empty MutableHashSet | O(1) |
| constructors | [fromIterable](https://effect-ts.github.io/effect/effect/MutableHashSet.ts.html#fromiterable) | Creates a set from an iterable | O(n) |
| constructors | [make](https://effect-ts.github.io/effect/effect/MutableHashSet.ts.html#make) | Creates a set from multiple values | O(n) |
| elements | [has](https://effect-ts.github.io/effect/effect/MutableHashSet.ts.html#has) | Checks if a value exists in the set | O(1) avg |
| elements | [add](https://effect-ts.github.io/effect/effect/MutableHashSet.ts.html#add) | Adds a value to the set | O(1) avg |
| elements | [remove](https://effect-ts.github.io/effect/effect/MutableHashSet.ts.html#remove) | Removes a value from the set | O(1) avg |
| getters | [size](https://effect-ts.github.io/effect/effect/MutableHashSet.ts.html#size) | Gets the number of elements | O(1) |
| mutations | [clear](https://effect-ts.github.io/effect/effect/MutableHashSet.ts.html#clear) | Removes all values from the set | O(1) |
**Example** (Working with a mutable set)
```ts twoslash
import { MutableHashSet } from "effect"
// Create a mutable set with initial values
const set = MutableHashSet.make(1, 2, 3)
// Add a new element (updates the set in place)
MutableHashSet.add(set, 4)
// Check current contents
console.log([...set])
// Output: [1, 2, 3, 4]
// Remove an element (modifies in place)
MutableHashSet.remove(set, 1)
console.log([...set])
// Output: [2, 3, 4]
// Clear the set entirely
MutableHashSet.clear(set)
console.log(MutableHashSet.size(set))
// Output: 0
```
## Interoperability with JavaScript
Both `HashSet` and `MutableHashSet` implement the `Iterable` interface, so you can use them with JavaScript features like:
- the spread operator (`...`)
- `for...of` loops
- `Array.from`
You can also extract values as an array using `.toValues`.
**Example** (Using HashSet values in JS-native ways)
```ts twoslash
import { HashSet, MutableHashSet } from "effect"
// Immutable HashSet
const hashSet = HashSet.make(1, 2, 3)
// Mutable variant
const mutableSet = MutableHashSet.make(4, 5, 6)
// Convert HashSet to an iterator
//
// ┌─── IterableIterator<number>
// ▼
const iterable = HashSet.values(hashSet)
// Spread into console.log
console.log(...iterable)
// Output: 1 2 3
// Use in a for...of loop
for (const value of mutableSet) {
console.log(value)
}
// Output: 4 5 6
// Convert to array with Array.from
console.log(Array.from(mutableSet))
// Output: [ 4, 5, 6 ]
// Convert immutable HashSet to array using toValues
//
// ┌─── Array<number>
// ▼
const array = HashSet.toValues(hashSet)
console.log(array)
// Output: [ 1, 2, 3 ]
```
<Aside type="caution" title="Performance considerations">
Avoid repeatedly converting between `HashSet` and JavaScript arrays in
hot paths or large collections. These operations involve copying data
and can impact memory and speed.
</Aside>
A HashSet represents an unordered collection of unique values with efficient lookup, insertion and removal operations.
The Effect library provides two versions of this structure:
Both versions provide constant-time operations on average. The main difference is how they handle changes: one returns new sets, the other modifies the original.
Why use HashSet?
HashSet solves the problem of maintaining an unsorted collection where each value appears exactly once, with fast operations for checking membership and adding/removing values.
Some common use cases include:
Tracking unique items (e.g., users who have completed an action)
Efficiently testing for membership in a collection
Performing set operations like union, intersection, and difference
Eliminating duplicates from a collection
When to use HashSet Instead of other collections
Choose HashSet (either variant) over other collections when:
You need to ensure elements are unique
You frequently need to check if an element exists in the collection
You need to perform set operations like union, intersection, and difference
The order of elements doesn’t matter to your use case
Choose other collections when:
You need to maintain insertion order (use List or Array)
You need key-value associations (use HashMap or MutableHashMap)
You need to frequently access elements by index (use Array)
Choosing between immutable and mutable variants
Effect offers both immutable and mutable versions to support different coding styles and performance needs.
HashSet
This version never modifies the original set. Instead, it returns a new set for every change.
Characteristics:
Operations return new instances instead of modifying the original
Previous states are preserved
Thread-safe by design
Ideal for functional programming patterns
Suitable for sharing across different parts of your application
MutableHashSet
This version allows direct updates: adding and removing values changes the set in place.
Characteristics:
Operations modify the original set directly
More efficient when building sets incrementally
Requires careful handling to avoid unexpected side effects
Better performance in scenarios with many modifications
Ideal for localized use where mutations won’t cause issues elsewhere
When to use each variant
Use HashSet when:
You need predictable behavior with no side effects
You want to preserve previous states of your data
You’re sharing sets across different parts of your application
You prefer functional programming patterns
You need fiber safety in concurrent environments
Use MutableHashSet when:
Performance is critical, and you need to avoid creating new instances
You’re building a collection incrementally with many additions/removals
You’re working in a controlled scope where mutation is safe
You need to optimize memory usage in performance-critical code
Hybrid approach
You can apply multiple updates to a HashSet in a temporary mutable context using HashSet.mutate. This allows you to perform several changes at once without modifying the original set.
Example (Batching changes without mutating the original)
assert.equal(HashSet.has(result, 0), false) // it has correctly removed 0
assert.equal(HashSet.has(set, 0), true) // it does not mutate the original set
assert.equal(HashSet.has(result, 1), true)
assert.equal(HashSet.has(result, 2), true)
remove(
draft: HashSet.HashSet<number>
draft, 1)
11
})
12
13
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)
@see ― Other HashSet getters are module:HashSet.values module:HashSet.size
toValues(
constoriginal:HashSet.HashSet<number>
original))
14
// Output: [1, 2, 3] - original remains unchanged
15
16
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)
@see ― Other HashSet getters are module:HashSet.values module:HashSet.size
toValues(
constmodified:HashSet.HashSet<number>
modified))
17
// Output: [2, 3, 4, 5] - changes applied to a new version
Performance characteristics
Both HashSet and MutableHashSet offer similar average-time performance for core operations:
Operation
HashSet
MutableHashSet
Description
Lookup
O(1) average
O(1) average
Check if a value exists
Insertion
O(1) average
O(1) average
Add a value
Removal
O(1) average
O(1) average
Remove a value
Iteration
O(n)
O(n)
Iterate over all values
Set operations
O(n)
O(n)
Union, intersection, difference
The main difference is how updates are handled:
HashSet returns a new set for each change. This can be slower if many changes are made in a row.
MutableHashSet updates the same set in place. This is usually faster when performing many changes.
Equality and uniqueness
Both HashSet and MutableHashSet use Effect’s Equal trait to determine if two elements are the same. This ensures that each value appears only once in the set.
Primitive values (like numbers or strings) are compared by value, similar to the === operator.
Objects and custom types must implement the Equal interface to define what it means for two instances to be equal. If no implementation is provided, equality falls back to reference comparison.
Example (Using custom equality and hashing)
1
import {
import Equal
Equal,
import Hash
Hash,
import HashSet
HashSet } from"effect"
2
3
// Define a custom class that implements the Equal interface
4
class
classPerson
Personimplements
import Equal
Equal.
interfaceEqual
@since ― 2.0.0
Equal {
5
constructor(
6
readonly
Person.id: number
id:number,
7
readonly
Person.name: string
name:string,
8
readonly
Person.age: number
age:number
9
) {}
10
11
// Two Person instances are equal if their id, name, and age match
constructor Person(id: number, name: string, age: number): Person
Person(1, "Alice", 30))
33
)
34
35
// Only one instance is kept
36
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
@see ― Other HashSet getters are module:HashSet.values module:HashSet.toValues
size(
constset:HashSet.HashSet<Person>
set))
37
// Output: 1
Simplifying Equality with Data and Schema
Effect’s Data and Schema.Data modules implement Equal for you automatically, based on structural equality.
Example (Using Data.struct)
1
import {
import Data
Data,
import Equal
Equal,
import HashSet
HashSet,
functionpipe<A>(a:A):A (+19overloads)
Pipes the value of an expression into a pipeline of functions.
Details
The pipe function is a utility that allows us to compose functions in a
readable and sequential manner. It takes the output of one function and
passes it as the input to the next function in the pipeline. This enables us
to build complex transformations by chaining multiple functions together.
import { pipe } from"effect"
constresult=pipe(input, func1, func2, ..., funcN)
In this syntax, input is the initial value, and func1, func2, ...,
funcN are the functions to be applied in sequence. The result of each
function becomes the input for the next function, and the final result is
returned.
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Returns true if the values are the same value, false otherwise.
@param ― value1 The first value.
@param ― value2 The second value.
is(
constperson1: {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}
person1,
constperson2: {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}
person2))
9
// Output: false
10
11
// But they are equal in value (based on content)
12
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Pipes the value of an expression into a pipeline of functions.
Details
The pipe function is a utility that allows us to compose functions in a
readable and sequential manner. It takes the output of one function and
passes it as the input to the next function in the pipeline. This enables us
to build complex transformations by chaining multiple functions together.
import { pipe } from"effect"
constresult=pipe(input, func1, func2, ..., funcN)
In this syntax, input is the initial value, and func1, func2, ...,
funcN are the functions to be applied in sequence. The result of each
function becomes the input for the next function, and the final result is
returned.
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
@see ― Other HashSet getters are module:HashSet.values module:HashSet.toValues
size(
constset:HashSet.HashSet<{
readonlyid:number;
readonlyname:string;
readonlyage:number;
}>
set))
23
// Output: 1
Example (Using Schema.Data)
1
import {
import Equal
Equal,
import MutableHashSet
MutableHashSet,
import Schema
Schema } from"effect"
2
3
// Define a schema that describes the structure of a Person
4
const
constPersonSchema:Schema.Data<Schema.Struct<{
id:typeof Schema.Number;
name:typeof Schema.String;
age:typeof Schema.Number;
}>>
PersonSchema=
import Schema
Schema.
constData: <Schema.Struct<{
id:typeof Schema.Number;
name:typeof Schema.String;
age:typeof Schema.Number;
}>, {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}, {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}>(value:Schema.Struct<{
id:typeof Schema.Number;
name:typeof Schema.String;
age:typeof Schema.Number;
}> &Schema.Schema<{
readonlyid:number;
readonlyname:string;
readonlyage:number;
}, {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}, never>) =>Schema.Data<Schema.Struct<{
id:typeof Schema.Number;
name:typeof Schema.String;
age:typeof Schema.Number;
}>>
Type and Encoded must extend Readonly<Record<string, any>> | ReadonlyArray<any> to be compatible with this API.
@since ― 3.10.0
Data(
5
import Schema
Schema.
functionStruct<{
id:typeof Schema.Number;
name:typeof Schema.String;
age:typeof Schema.Number;
}>(fields: {
id:typeof Schema.Number;
name:typeof Schema.String;
age:typeof Schema.Number;
}):Schema.Struct<{
id:typeof Schema.Number;
name:typeof Schema.String;
age:typeof Schema.Number;
}> (+1overload)
@since ― 3.10.0
Struct({
6
id: typeof Schema.Number
id:
import Schema
Schema.
classNumber
exportNumber
@since ― 3.10.0
Number,
7
name: typeof Schema.String
name:
import Schema
Schema.
classString
exportString
@since ― 3.10.0
String,
8
age: typeof Schema.Number
age:
import Schema
Schema.
classNumber
exportNumber
@since ― 3.10.0
Number
9
})
10
)
11
12
// Decode values from plain objects
13
const
constPerson: (i: {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}, overrideOptions?:ParseOptions) => {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}
Person=
import Schema
Schema.
decodeSync<{
readonlyid:number;
readonlyname:string;
readonlyage:number;
}, {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}>(schema:Schema.Schema<{
readonlyid:number;
readonlyname:string;
readonlyage:number;
}, {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}, never>, options?:ParseOptions): (i: {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}, overrideOptions?:ParseOptions) => {
readonly id: number;
readonly name: string;
readonly age: number;
}
export decodeSync
@since ― 3.10.0
decodeSync(
constPersonSchema:Schema.Data<Schema.Struct<{
id:typeof Schema.Number;
name:typeof Schema.String;
age:typeof Schema.Number;
}>>
PersonSchema)
14
15
const
constperson1: {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}
person1=
constPerson: (i: {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}, overrideOptions?:ParseOptions) => {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}
Person({
id: number
id: 1,
name: string
name: "Alice",
age: number
age: 30 })
16
const
constperson2: {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}
person2=
constPerson: (i: {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}, overrideOptions?:ParseOptions) => {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}
Person({
id: number
id: 1,
name: string
name: "Alice",
age: number
age: 30 })
17
18
// person1 and person2 are different instances but equal in value
19
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Array.from(mutableHashSet), // remember that MutableHashSet is also an Iterable
Array.of(0, 1, 2)
)
@param ― key - The key to be added to the MutableHashSet if not already
present.
@returns ― A function that accepts a MutableHashSet and returns the
reference of the updated MutableHashSet including the key.
add(
constperson2: {
readonlyid:number;
readonlyname:string;
readonlyage:number;
}
person2)
26
)
27
28
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
@param ― self - The MutableHashSet instance for which the size is to be
determined.
@returns ― The total number of elements within the MutableHashSet.
@see ― Other MutableHashSet elements are module:MutableHashSet.add module:MutableHashSet.has module:MutableHashSet.remove module:MutableHashSet.clear
size(
constset:MutableHashSet.MutableHashSet<{
readonlyid:number;
readonlyname:string;
readonlyage:number;
}>
set))
29
// Output: 1
HashSet
A HashSet<A> is an immutable, unordered collection of unique values.
It guarantees that each value appears only once and supports fast operations like lookup, insertion, and removal.
Any operation that would modify the set (like adding or removing a value) returns a new HashSet, leaving the original unchanged.
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)
@see ― Other HashSet getters are module:HashSet.values module:HashSet.size
toValues(
constset1:HashSet.HashSet<number>
set1))
11
// Output: [1, 2, 3]
12
13
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)
@see ― Other HashSet getters are module:HashSet.values module:HashSet.size
toValues(
constdifference:HashSet.HashSet<number>
difference))
35
// Output: [1, 2]
Example (Chaining with pipe)
1
import {
import HashSet
HashSet,
functionpipe<A>(a:A):A (+19overloads)
Pipes the value of an expression into a pipeline of functions.
Details
The pipe function is a utility that allows us to compose functions in a
readable and sequential manner. It takes the output of one function and
passes it as the input to the next function in the pipeline. This enables us
to build complex transformations by chaining multiple functions together.
import { pipe } from"effect"
constresult=pipe(input, func1, func2, ..., funcN)
In this syntax, input is the initial value, and func1, func2, ...,
funcN are the functions to be applied in sequence. The result of each
function becomes the input for the next function, and the final result is
returned.
Pipes the value of an expression into a pipeline of functions.
Details
The pipe function is a utility that allows us to compose functions in a
readable and sequential manner. It takes the output of one function and
passes it as the input to the next function in the pipeline. This enables us
to build complex transformations by chaining multiple functions together.
import { pipe } from"effect"
constresult=pipe(input, func1, func2, ..., funcN)
In this syntax, input is the initial value, and func1, func2, ...,
funcN are the functions to be applied in sequence. The result of each
function becomes the input for the next function, and the final result is
returned.
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)
@see ― Other HashSet getters are module:HashSet.values module:HashSet.size
toValues
12
)
13
14
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
A MutableHashSet<A> is a mutable, unordered collection of unique values.
Unlike HashSet, it allows direct modifications, operations like add, remove, and clear update the original set instead of returning a new one.
This mutability can improve performance when you need to build or update a set repeatedly, especially within a local or isolated scope.
assert(Object.is(withTwoTwo, empty)) // proof that it does mutate the original set
assert.deepStrictEqual(
Array.from(withTwoTwo), // remember that MutableHashSet is also an Iterable
Array.of(0, 1, 2)
)
@param ― self - The MutableHashSet instance from which the key should be
added to.
@param ― key - The key to be added to the MutableHashSet if not already
present.
@returns ― The reference of the updated MutableHashSet including the key.
add(
constset:MutableHashSet.MutableHashSet<number>
set, 4)
8
9
// Check current contents
10
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
assert(Object.is(set, result)) // set and result have the same identity
assert.equal(MutableHashSet.has(result, 0), false) // it has correctly removed 0
assert.equal(MutableHashSet.has(set, 0), false) // it mutates the original MutableHashSet
assert.equal(MutableHashSet.has(result, 1), true)
assert.equal(MutableHashSet.has(result, 2), true)
@param ― self - The MutableHashSet to which the key will be removed from.
@param ― key - The value to be removed from the MutableHashSet if present.
@returns ― The reference to the updated MutableHashSet.
remove(
constset:MutableHashSet.MutableHashSet<number>
set, 1)
15
16
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
This function operates by delegating the clearing action to the underlying
key map associated with the given MutableHashSet. It ensures that the hash
set becomes empty while maintaining its existence and structure.
@memberof ― MutableHashSet
@since ― 2.0.0
@example
import { MutableHashSet, pipe } from"effect"
import assert from"node:assert/strict"
assert.deepStrictEqual(
pipe(
MutableHashSet.make(1, 2, 3, 4),
MutableHashSet.clear,
MutableHashSet.size
),
0
)
@param ― self - The MutableHashSet to clear.
@returns ― The same MutableHashSet after all elements have been removed.
@see ― Other MutableHashSet elements are module:MutableHashSet.add module:MutableHashSet.has module:MutableHashSet.remove module:MutableHashSet.size
clear(
constset:MutableHashSet.MutableHashSet<number>
set)
21
22
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
HashSet.values // takes an HashSet<A> and returns an IterableIterator<A>
)
for (constnumberof numberIterable) {
console.log(number) // it will logs: 0, 1, 2
}
@see ― Other HashSet getters are module:HashSet.toValues module:HashSet.size
values(
consthashSet:HashSet.HashSet<number>
hashSet)
14
15
// Spread into console.log
16
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)
@see ― Other HashSet getters are module:HashSet.values module:HashSet.size
toValues(
consthashSet:HashSet.HashSet<number>
hashSet)
34
35
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).