Skip to content

HashSet

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.

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

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)

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

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

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)

import {
import HashSet
HashSet
} from "effect"
// Create an immutable HashSet
const
const original: HashSet.HashSet<number>
original
=
import HashSet
HashSet
.
const make: <[number, number, number]>(elements_0: number, elements_1: number, elements_2: number) => HashSet.HashSet<number>

Construct a new HashSet from a variable number of values.

Time complexity: O(n) where n is the number of elements

@memberofHashSet

@since2.0.0

@example

import { Equal, Hash, HashSet, pipe } from "effect"
import assert from "node:assert/strict"
class Character implements Equal.Equal {
readonly name: string
readonly trait: string
constructor(name: string, trait: string) {
this.name = name
this.trait = trait
}
// Define equality based on name, and trait
[Equal.symbol](that: Equal.Equal): boolean {
if (that instanceof Character) {
return (
Equal.equals(this.name, that.name) &&
Equal.equals(this.trait, that.trait)
)
}
return false
}
// Generate a hash code based on the sum of the character's name and trait
[Hash.symbol](): number {
return Hash.hash(this.name + this.trait)
}
static readonly of = (name: string, trait: string): Character => {
return new Character(name, trait)
}
}
assert.strictEqual(
Equal.equals(
HashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
),
// Is the same as adding each character to an empty set
pipe(
HashSet.empty(),
HashSet.add(Character.of("Alice", "Curious")),
HashSet.add(Character.of("Alice", "Curious")), // Alice tried to attend twice!
HashSet.add(Character.of("White Rabbit", "Always late")),
HashSet.add(Character.of("Mad Hatter", "Tea enthusiast"))
)
),
true,
"`HashSet.make` and `HashSet.empty() + HashSet.add()` should be equal"
)
assert.strictEqual(
Equal.equals(
HashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
),
HashSet.fromIterable([
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
])
),
true,
"`HashSet.make` and `HashSet.fromIterable` should be equal"
)

@seeOther HashSet constructors are {@linkcode fromIterable} {@linkcode empty}

make
(1, 2, 3)
// Apply several updates inside a temporary mutable draft
const
const modified: HashSet.HashSet<number>
modified
=
import HashSet
HashSet
.
const mutate: <number>(self: HashSet.HashSet<number>, f: (set: HashSet.HashSet<number>) => void) => HashSet.HashSet<number> (+1 overload)

@example

{@linkcode HashSet.mutate } data-first API

import { HashSet } from "effect"
import assert from "node:assert/strict"
// Create a set with initial values
const immutableSet = HashSet.make(1, 2, 3)
// Use mutate with data-first API
const result = HashSet.mutate(immutableSet, (set) => {
// The set is temporarily mutable inside this function
HashSet.add(set, 4)
HashSet.remove(set, 1)
})
// The original set is unchanged
assert.equal(Object.is(immutableSet, result), false)
assert.deepStrictEqual(
HashSet.toValues(immutableSet).sort(),
[1, 2, 3]
)
// The result contains the mutations
assert.deepStrictEqual(HashSet.toValues(result).sort(), [2, 3, 4])

mutate
(
const original: HashSet.HashSet<number>
original
, (
draft: HashSet.HashSet<number>
draft
) => {
import HashSet
HashSet
.
const add: <number>(self: HashSet.HashSet<number>, value: number) => HashSet.HashSet<number> (+1 overload)

@example

add data-first API

import { HashSet, pipe } from "effect"
import assert from "node:assert/strict"
const empty = HashSet.empty<number>()
const withZero = HashSet.add(empty, 0)
const withOne = HashSet.add(withZero, 1)
const withTwo = HashSet.add(withOne, 2)
const withTwoTwo = HashSet.add(withTwo, 2)
assert.deepStrictEqual(HashSet.toValues(withTwoTwo), Array.of(0, 1, 2))

add
(
draft: HashSet.HashSet<number>
draft
, 4)
import HashSet
HashSet
.
const add: <number>(self: HashSet.HashSet<number>, value: number) => HashSet.HashSet<number> (+1 overload)

@example

add data-first API

import { HashSet, pipe } from "effect"
import assert from "node:assert/strict"
const empty = HashSet.empty<number>()
const withZero = HashSet.add(empty, 0)
const withOne = HashSet.add(withZero, 1)
const withTwo = HashSet.add(withOne, 2)
const withTwoTwo = HashSet.add(withTwo, 2)
assert.deepStrictEqual(HashSet.toValues(withTwoTwo), Array.of(0, 1, 2))

add
(
draft: HashSet.HashSet<number>
draft
, 5)
import HashSet
HashSet
.
const remove: <number>(self: HashSet.HashSet<number>, value: number) => HashSet.HashSet<number> (+1 overload)

@example

remove data-first API

import { HashSet, pipe } from "effect"
import * as assert from "node:assert/strict"
const set = HashSet.make(0, 1, 2)
const result = HashSet.remove(set, 0)
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)
})
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import HashSet
HashSet
.
const toValues: <number>(self: HashSet.HashSet<number>) => number[]

Returns an Array of the values within the HashSet.

Time complexity: O(n) where n is the number of elements in the set

@memberofHashSet

@since3.13.0

@example

import { HashSet, pipe } from "effect"
import { deepStrictEqual } from "node:assert/strict"
deepStrictEqual(
pipe(
HashSet.make(0, 1, 1, 2), // HashSet<number>
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)

@seeOther HashSet getters are values size

toValues
(
const original: HashSet.HashSet<number>
original
))
// Output: [1, 2, 3] - original remains unchanged
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import HashSet
HashSet
.
const toValues: <number>(self: HashSet.HashSet<number>) => number[]

Returns an Array of the values within the HashSet.

Time complexity: O(n) where n is the number of elements in the set

@memberofHashSet

@since3.13.0

@example

import { HashSet, pipe } from "effect"
import { deepStrictEqual } from "node:assert/strict"
deepStrictEqual(
pipe(
HashSet.make(0, 1, 1, 2), // HashSet<number>
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)

@seeOther HashSet getters are values size

toValues
(
const modified: HashSet.HashSet<number>
modified
))
// Output: [2, 3, 4, 5] - changes applied to a new version

Both HashSet and MutableHashSet offer similar average-time performance for core operations:

OperationHashSetMutableHashSetDescription
LookupO(1) averageO(1) averageCheck if a value exists
InsertionO(1) averageO(1) averageAdd a value
RemovalO(1) averageO(1) averageRemove a value
IterationO(n)O(n)Iterate over all values
Set operationsO(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.

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)

import {
import Equal
Equal
,
import Hash
Hash
,
import HashSet
HashSet
} from "effect"
// Define a custom class that implements the Equal interface
class
class Person
Person
implements
import Equal
Equal
.
interface Equal

@since2.0.0

Equal
{
constructor(
readonly
Person.id: number
id
: number,
readonly
Person.name: string
name
: string,
readonly
Person.age: number
age
: number
) {}
// Two Person instances are equal if their id, name, and age match
[
import Equal
Equal
.
const symbol: typeof Equal.symbol

@since2.0.0

symbol
](
that: Equal.Equal
that
:
import Equal
Equal
.
interface Equal

@since2.0.0

Equal
): boolean {
if (
that: Equal.Equal
that
instanceof
class Person
Person
) {
return (
import Equal
Equal
.
function equals<number, number>(self: number, that: number): boolean (+1 overload)

@since2.0.0

equals
(this.
Person.id: number
id
,
that: Person
that
.
Person.id: number
id
) &&
import Equal
Equal
.
function equals<string, string>(self: string, that: string): boolean (+1 overload)

@since2.0.0

equals
(this.
Person.name: string
name
,
that: Person
that
.
Person.name: string
name
) &&
import Equal
Equal
.
function equals<number, number>(self: number, that: number): boolean (+1 overload)

@since2.0.0

equals
(this.
Person.age: number
age
,
that: Person
that
.
Person.age: number
age
)
)
}
return false
}
// Hash code is based on the id (must match the equality logic)
[
import Hash
Hash
.
const symbol: typeof Hash.symbol

@since2.0.0

symbol
](): number {
return
import Hash
Hash
.
const hash: <number>(self: number) => number

@since2.0.0

hash
(this.
Person.id: number
id
)
}
}
// Add two different instances with the same content
const
const set: HashSet.HashSet<Person>
set
=
import HashSet
HashSet
.
const empty: <never>() => HashSet.HashSet<never>

Creates an empty HashSet.

Time complexity: O(1)

@memberofHashSet

@since2.0.0

@example

import { HashSet, pipe } from "effect"
console.log(
pipe(
// Provide a type argument to create a HashSet of a specific type
HashSet.empty<number>(),
HashSet.add(1),
HashSet.add(1), // Notice the duplicate
HashSet.add(2),
HashSet.toValues
)
) // Output: [1, 2]

@seeOther HashSet constructors are make fromIterable

empty
().
Pipeable.pipe<HashSet.HashSet<never>, HashSet.HashSet<Person>, HashSet.HashSet<Person>>(this: HashSet.HashSet<...>, ab: (_: HashSet.HashSet<never>) => HashSet.HashSet<Person>, bc: (_: HashSet.HashSet<...>) => HashSet.HashSet<...>): HashSet.HashSet<...> (+21 overloads)
pipe
(
import HashSet
HashSet
.
const add: <Person>(value: Person) => (self: HashSet.HashSet<Person>) => HashSet.HashSet<Person> (+1 overload)

@example

add data-last a.k.a. pipeable API

import { HashSet, pipe } from "effect"
import assert from "node:assert/strict"
assert.deepStrictEqual(
pipe(
HashSet.empty<number>(), // HashSet.HashSet<number>
HashSet.add(0),
HashSet.add(1),
HashSet.add(1),
HashSet.add(2),
HashSet.toValues
),
Array.of(0, 1, 2)
)

add
(new
constructor Person(id: number, name: string, age: number): Person
Person
(1, "Alice", 30)),
import HashSet
HashSet
.
const add: <Person>(value: Person) => (self: HashSet.HashSet<Person>) => HashSet.HashSet<Person> (+1 overload)

@example

add data-last a.k.a. pipeable API

import { HashSet, pipe } from "effect"
import assert from "node:assert/strict"
assert.deepStrictEqual(
pipe(
HashSet.empty<number>(), // HashSet.HashSet<number>
HashSet.add(0),
HashSet.add(1),
HashSet.add(1),
HashSet.add(2),
HashSet.toValues
),
Array.of(0, 1, 2)
)

add
(new
constructor Person(id: number, name: string, age: number): Person
Person
(1, "Alice", 30))
)
// Only one instance is kept
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import HashSet
HashSet
.
const size: <Person>(self: HashSet.HashSet<Person>) => number

Calculates the number of values in the HashSet.

Time complexity: O(1)

@memberofHashSet

@since2.0.0

@example

import { HashSet, pipe } from "effect"
import assert from "node:assert/strict"
assert.deepStrictEqual(pipe(HashSet.empty(), HashSet.size), 0)
assert.deepStrictEqual(
pipe(HashSet.make(1, 2, 2, 3, 4, 3), HashSet.size),
4
)

@seeOther HashSet getters are values toValues

size
(
const set: HashSet.HashSet<Person>
set
))
// Output: 1

Effect’s Data and Schema.Data modules implement Equal for you automatically, based on structural equality.

Example (Using Data.struct)

import {
import Data
Data
,
import Equal
Equal
,
import HashSet
HashSet
,
function pipe<A>(a: A): A (+19 overloads)

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"
const result = 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.

Here's an illustration of how pipe works:

┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌────────┐
│ input │───►│ func1 │───►│ func2 │───►│ ... │───►│ funcN │───►│ result │
└───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └────────┘

It's important to note that functions passed to pipe must have a single argument because they are only called with a single argument.

When to Use

This is useful in combination with data-last functions as a simulation of methods:

as.map(f).filter(g)

becomes:

import { pipe, Array } from "effect"
pipe(as, Array.map(f), Array.filter(g))

Example (Chaining Arithmetic Operations)

import { pipe } from "effect"
// Define simple arithmetic operations
const increment = (x: number) => x + 1
const double = (x: number) => x * 2
const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`
const result = pipe(5, increment, double, subtractTen)
console.log(result)
// Output: 2

@since2.0.0

pipe
} from "effect"
// Define two records with the same content
const
const person1: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person1
=
import Data
Data
.
const struct: <{
id: number;
name: string;
age: number;
}>(a: {
id: number;
name: string;
age: number;
}) => {
readonly id: number;
readonly name: string;
readonly age: number;
}

@example

import * as assert from "node:assert"
import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
id: number
id
: 1,
name: string
name
: "Alice",
age: number
age
: 30 })
const
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
=
import Data
Data
.
const struct: <{
id: number;
name: string;
age: number;
}>(a: {
id: number;
name: string;
age: number;
}) => {
readonly id: number;
readonly name: string;
readonly age: number;
}

@example

import * as assert from "node:assert"
import { Data, Equal } from "effect"
const alice = Data.struct({ name: "Alice", age: 30 })
const bob = Data.struct({ name: "Bob", age: 40 })
assert.deepStrictEqual(Equal.equals(alice, alice), true)
assert.deepStrictEqual(Equal.equals(alice, Data.struct({ name: "Alice", age: 30 })), true)
assert.deepStrictEqual(Equal.equals(alice, { name: "Alice", age: 30 }), false)
assert.deepStrictEqual(Equal.equals(alice, bob), false)

@since2.0.0

struct
({
id: number
id
: 1,
name: string
name
: "Alice",
age: number
age
: 30 })
// They are different object references
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

Object
.
ObjectConstructor.is(value1: any, value2: any): boolean

Returns true if the values are the same value, false otherwise.

@paramvalue1 The first value.

@paramvalue2 The second value.

is
(
const person1: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person1
,
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
))
// Output: false
// But they are equal in value (based on content)
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<{
readonly id: number;
readonly name: string;
readonly age: number;
}, {
readonly id: number;
readonly name: string;
readonly age: number;
}>(self: {
readonly id: number;
readonly name: string;
readonly age: number;
}, that: {
readonly id: number;
readonly name: string;
readonly age: number;
}): boolean (+1 overload)

@since2.0.0

equals
(
const person1: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person1
,
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
))
// Output: true
// Add both to a HashSet — only one will be stored
const
const set: HashSet.HashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>
set
=
pipe<HashSet.HashSet<never>, HashSet.HashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>, HashSet.HashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>>(a: HashSet.HashSet<...>, ab: (a: HashSet.HashSet<...>) => HashSet.HashSet<...>, bc: (b: HashSet.HashSet<...>) => HashSet.HashSet<...>): HashSet.HashSet<...> (+19 overloads)

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"
const result = 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.

Here's an illustration of how pipe works:

┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌────────┐
│ input │───►│ func1 │───►│ func2 │───►│ ... │───►│ funcN │───►│ result │
└───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └────────┘

It's important to note that functions passed to pipe must have a single argument because they are only called with a single argument.

When to Use

This is useful in combination with data-last functions as a simulation of methods:

as.map(f).filter(g)

becomes:

import { pipe, Array } from "effect"
pipe(as, Array.map(f), Array.filter(g))

Example (Chaining Arithmetic Operations)

import { pipe } from "effect"
// Define simple arithmetic operations
const increment = (x: number) => x + 1
const double = (x: number) => x * 2
const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`
const result = pipe(5, increment, double, subtractTen)
console.log(result)
// Output: 2

@since2.0.0

pipe
(
import HashSet
HashSet
.
const empty: <never>() => HashSet.HashSet<never>

Creates an empty HashSet.

Time complexity: O(1)

@memberofHashSet

@since2.0.0

@example

import { HashSet, pipe } from "effect"
console.log(
pipe(
// Provide a type argument to create a HashSet of a specific type
HashSet.empty<number>(),
HashSet.add(1),
HashSet.add(1), // Notice the duplicate
HashSet.add(2),
HashSet.toValues
)
) // Output: [1, 2]

@seeOther HashSet constructors are make fromIterable

empty
(),
import HashSet
HashSet
.
const add: <{
readonly id: number;
readonly name: string;
readonly age: number;
}>(value: {
readonly id: number;
readonly name: string;
readonly age: number;
}) => (self: HashSet.HashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>) => HashSet.HashSet<...> (+1 overload)

@example

add data-last a.k.a. pipeable API

import { HashSet, pipe } from "effect"
import assert from "node:assert/strict"
assert.deepStrictEqual(
pipe(
HashSet.empty<number>(), // HashSet.HashSet<number>
HashSet.add(0),
HashSet.add(1),
HashSet.add(1),
HashSet.add(2),
HashSet.toValues
),
Array.of(0, 1, 2)
)

add
(
const person1: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person1
),
import HashSet
HashSet
.
const add: <{
readonly id: number;
readonly name: string;
readonly age: number;
}>(value: {
readonly id: number;
readonly name: string;
readonly age: number;
}) => (self: HashSet.HashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>) => HashSet.HashSet<...> (+1 overload)

@example

add data-last a.k.a. pipeable API

import { HashSet, pipe } from "effect"
import assert from "node:assert/strict"
assert.deepStrictEqual(
pipe(
HashSet.empty<number>(), // HashSet.HashSet<number>
HashSet.add(0),
HashSet.add(1),
HashSet.add(1),
HashSet.add(2),
HashSet.toValues
),
Array.of(0, 1, 2)
)

add
(
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
)
)
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import HashSet
HashSet
.
const size: <{
readonly id: number;
readonly name: string;
readonly age: number;
}>(self: HashSet.HashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>) => number

Calculates the number of values in the HashSet.

Time complexity: O(1)

@memberofHashSet

@since2.0.0

@example

import { HashSet, pipe } from "effect"
import assert from "node:assert/strict"
assert.deepStrictEqual(pipe(HashSet.empty(), HashSet.size), 0)
assert.deepStrictEqual(
pipe(HashSet.make(1, 2, 2, 3, 4, 3), HashSet.size),
4
)

@seeOther HashSet getters are values toValues

size
(
const set: HashSet.HashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>
set
))
// Output: 1

Example (Using Schema.Data)

import {
import Equal
Equal
,
import MutableHashSet
MutableHashSet
,
import Schema
Schema
} from "effect"
// Define a schema that describes the structure of a Person
const
const PersonSchema: Schema.Data<Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>>
PersonSchema
=
import Schema
Schema
.
const Data: <Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>, {
readonly id: number;
readonly name: string;
readonly age: number;
}, {
...;
}>(value: Schema.Struct<...> & Schema.Schema<...>) => Schema.Data<...>

Type and Encoded must extend Readonly<Record<string, any>> | ReadonlyArray<any> to be compatible with this API.

@since3.10.0

Data
(
import Schema
Schema
.
function Struct<{
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<...> (+1 overload)

@since3.10.0

Struct
({
id: typeof Schema.Number
id
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
,
name: typeof Schema.String
name
:
import Schema
Schema
.
class String
export String

@since3.10.0

String
,
age: typeof Schema.Number
age
:
import Schema
Schema
.
class Number
export Number

@since3.10.0

Number
})
)
// Decode values from plain objects
const
const Person: (i: {
readonly id: number;
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly age: number;
}
Person
=
import Schema
Schema
.
decodeSync<{
readonly id: number;
readonly name: string;
readonly age: number;
}, {
readonly id: number;
readonly name: string;
readonly age: number;
}>(schema: Schema.Schema<{
readonly id: number;
readonly name: string;
readonly age: number;
}, {
readonly id: number;
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions): (i: {
readonly id: number;
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly age: number;
}
export decodeSync

@since3.10.0

decodeSync
(
const PersonSchema: Schema.Data<Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>>
PersonSchema
)
const
const person1: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person1
=
const Person: (i: {
readonly id: number;
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly age: number;
}
Person
({
id: number
id
: 1,
name: string
name
: "Alice",
age: number
age
: 30 })
const
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
=
const Person: (i: {
readonly id: number;
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly age: number;
}
Person
({
id: number
id
: 1,
name: string
name
: "Alice",
age: number
age
: 30 })
// person1 and person2 are different instances but equal in value
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import Equal
Equal
.
function equals<{
readonly id: number;
readonly name: string;
readonly age: number;
}, {
readonly id: number;
readonly name: string;
readonly age: number;
}>(self: {
readonly id: number;
readonly name: string;
readonly age: number;
}, that: {
readonly id: number;
readonly name: string;
readonly age: number;
}): boolean (+1 overload)

@since2.0.0

equals
(
const person1: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person1
,
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
))
// Output: true
// Add both to a MutableHashSet — only one will be stored
const
const set: MutableHashSet.MutableHashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>
set
=
import MutableHashSet
MutableHashSet
.
const empty: <never>() => MutableHashSet.MutableHashSet<never>

@since2.0.0

empty
().
Pipeable.pipe<MutableHashSet.MutableHashSet<never>, MutableHashSet.MutableHashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>, MutableHashSet.MutableHashSet<...>>(this: MutableHashSet.MutableHashSet<...>, ab: (_: MutableHashSet.MutableHashSet<...>) => MutableHashSet.MutableHashSet<...>, bc: (_: MutableHashSet.MutableHashSet<...>) => MutableHashSet.MutableHashSet<...>): MutableHashSet.MutableHashSet<...> (+21 overloads)
pipe
(
import MutableHashSet
MutableHashSet
.
const add: <{
readonly id: number;
readonly name: string;
readonly age: number;
}>(key: {
readonly id: number;
readonly name: string;
readonly age: number;
}) => (self: MutableHashSet.MutableHashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>) => MutableHashSet.MutableHashSet<...> (+1 overload)

@since2.0.0

add
(
const person1: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person1
),
import MutableHashSet
MutableHashSet
.
const add: <{
readonly id: number;
readonly name: string;
readonly age: number;
}>(key: {
readonly id: number;
readonly name: string;
readonly age: number;
}) => (self: MutableHashSet.MutableHashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>) => MutableHashSet.MutableHashSet<...> (+1 overload)

@since2.0.0

add
(
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
)
)
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import MutableHashSet
MutableHashSet
.
const size: <{
readonly id: number;
readonly name: string;
readonly age: number;
}>(self: MutableHashSet.MutableHashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>) => number

@since2.0.0

size
(
const set: MutableHashSet.MutableHashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>
set
))
// Output: 1

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.

CategoryOperationDescriptionTime Complexity
constructorsemptyCreates an empty HashSetO(1)
constructorsfromIterableCreates a HashSet from an iterableO(n)
constructorsmakeCreates a HashSet from multiple valuesO(n)
elementshasChecks if a value exists in the setO(1) avg
elementssomeChecks if any element satisfies a predicateO(n)
elementseveryChecks if all elements satisfy a predicateO(n)
elementsisSubsetChecks if a set is a subset of anotherO(n)
gettersvaluesGets an Iterator of all valuesO(1)
getterstoValuesGets an Array of all valuesO(n)
getterssizeGets the number of elementsO(1)
mutationsaddAdds a value to the setO(1) avg
mutationsremoveRemoves a value from the setO(1) avg
mutationstoggleToggles a value’s presenceO(1) avg
operationsdifferenceComputes set difference (A - B)O(n)
operationsintersectionComputes set intersection (A ∩ B)O(n)
operationsunionComputes set union (A ∪ B)O(n)
mappingmapTransforms each elementO(n)
sequencingflatMapTransforms and flattens elementsO(n)
traversingforEachApplies a function to each elementO(n)
foldingreduceReduces the set to a single valueO(n)
filteringfilterKeeps elements that satisfy a predicateO(n)
partitioningpartitionSplits into two sets by a predicateO(n)

Example (Basic creation and operations)

import {
import HashSet
HashSet
} from "effect"
// Create an initial set with 3 values
const
const set1: HashSet.HashSet<number>
set1
=
import HashSet
HashSet
.
const make: <[number, number, number]>(elements_0: number, elements_1: number, elements_2: number) => HashSet.HashSet<number>

Construct a new HashSet from a variable number of values.

Time complexity: O(n) where n is the number of elements

@memberofHashSet

@since2.0.0

@example

import { Equal, Hash, HashSet, pipe } from "effect"
import assert from "node:assert/strict"
class Character implements Equal.Equal {
readonly name: string
readonly trait: string
constructor(name: string, trait: string) {
this.name = name
this.trait = trait
}
// Define equality based on name, and trait
[Equal.symbol](that: Equal.Equal): boolean {
if (that instanceof Character) {
return (
Equal.equals(this.name, that.name) &&
Equal.equals(this.trait, that.trait)
)
}
return false
}
// Generate a hash code based on the sum of the character's name and trait
[Hash.symbol](): number {
return Hash.hash(this.name + this.trait)
}
static readonly of = (name: string, trait: string): Character => {
return new Character(name, trait)
}
}
assert.strictEqual(
Equal.equals(
HashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
),
// Is the same as adding each character to an empty set
pipe(
HashSet.empty(),
HashSet.add(Character.of("Alice", "Curious")),
HashSet.add(Character.of("Alice", "Curious")), // Alice tried to attend twice!
HashSet.add(Character.of("White Rabbit", "Always late")),
HashSet.add(Character.of("Mad Hatter", "Tea enthusiast"))
)
),
true,
"`HashSet.make` and `HashSet.empty() + HashSet.add()` should be equal"
)
assert.strictEqual(
Equal.equals(
HashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
),
HashSet.fromIterable([
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
])
),
true,
"`HashSet.make` and `HashSet.fromIterable` should be equal"
)

@seeOther HashSet constructors are {@linkcode fromIterable} {@linkcode empty}

make
(1, 2, 3)
// Add a value (returns a new set)
const
const set2: HashSet.HashSet<number>
set2
=
import HashSet
HashSet
.
const add: <number>(self: HashSet.HashSet<number>, value: number) => HashSet.HashSet<number> (+1 overload)

@example

add data-first API

import { HashSet, pipe } from "effect"
import assert from "node:assert/strict"
const empty = HashSet.empty<number>()
const withZero = HashSet.add(empty, 0)
const withOne = HashSet.add(withZero, 1)
const withTwo = HashSet.add(withOne, 2)
const withTwoTwo = HashSet.add(withTwo, 2)
assert.deepStrictEqual(HashSet.toValues(withTwoTwo), Array.of(0, 1, 2))

add
(
const set1: HashSet.HashSet<number>
set1
, 4)
// The original set is unchanged
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import HashSet
HashSet
.
const toValues: <number>(self: HashSet.HashSet<number>) => number[]

Returns an Array of the values within the HashSet.

Time complexity: O(n) where n is the number of elements in the set

@memberofHashSet

@since3.13.0

@example

import { HashSet, pipe } from "effect"
import { deepStrictEqual } from "node:assert/strict"
deepStrictEqual(
pipe(
HashSet.make(0, 1, 1, 2), // HashSet<number>
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)

@seeOther HashSet getters are values size

toValues
(
const set1: HashSet.HashSet<number>
set1
))
// Output: [1, 2, 3]
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import HashSet
HashSet
.
const toValues: <number>(self: HashSet.HashSet<number>) => number[]

Returns an Array of the values within the HashSet.

Time complexity: O(n) where n is the number of elements in the set

@memberofHashSet

@since3.13.0

@example

import { HashSet, pipe } from "effect"
import { deepStrictEqual } from "node:assert/strict"
deepStrictEqual(
pipe(
HashSet.make(0, 1, 1, 2), // HashSet<number>
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)

@seeOther HashSet getters are values size

toValues
(
const set2: HashSet.HashSet<number>
set2
))
// Output: [1, 2, 3, 4]
// Perform set operations with another set
const
const set3: HashSet.HashSet<number>
set3
=
import HashSet
HashSet
.
const make: <[number, number, number]>(elements_0: number, elements_1: number, elements_2: number) => HashSet.HashSet<number>

Construct a new HashSet from a variable number of values.

Time complexity: O(n) where n is the number of elements

@memberofHashSet

@since2.0.0

@example

import { Equal, Hash, HashSet, pipe } from "effect"
import assert from "node:assert/strict"
class Character implements Equal.Equal {
readonly name: string
readonly trait: string
constructor(name: string, trait: string) {
this.name = name
this.trait = trait
}
// Define equality based on name, and trait
[Equal.symbol](that: Equal.Equal): boolean {
if (that instanceof Character) {
return (
Equal.equals(this.name, that.name) &&
Equal.equals(this.trait, that.trait)
)
}
return false
}
// Generate a hash code based on the sum of the character's name and trait
[Hash.symbol](): number {
return Hash.hash(this.name + this.trait)
}
static readonly of = (name: string, trait: string): Character => {
return new Character(name, trait)
}
}
assert.strictEqual(
Equal.equals(
HashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
),
// Is the same as adding each character to an empty set
pipe(
HashSet.empty(),
HashSet.add(Character.of("Alice", "Curious")),
HashSet.add(Character.of("Alice", "Curious")), // Alice tried to attend twice!
HashSet.add(Character.of("White Rabbit", "Always late")),
HashSet.add(Character.of("Mad Hatter", "Tea enthusiast"))
)
),
true,
"`HashSet.make` and `HashSet.empty() + HashSet.add()` should be equal"
)
assert.strictEqual(
Equal.equals(
HashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
),
HashSet.fromIterable([
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
])
),
true,
"`HashSet.make` and `HashSet.fromIterable` should be equal"
)

@seeOther HashSet constructors are {@linkcode fromIterable} {@linkcode empty}

make
(3, 4, 5)
// Combine both sets
const
const union: HashSet.HashSet<number>
union
=
import HashSet
HashSet
.
const union: <number>(self: HashSet.HashSet<number>, that: Iterable<number>) => HashSet.HashSet<number> (+1 overload)

@example

union data-first API

import { HashSet } from "effect"
import * as assert from "node:assert/strict"
// Create two sets with some overlapping elements
const selfSet = HashSet.make(1, 2, 3)
const thatIterable = HashSet.make(3, 4, 5)
// Compute the union using data-first API
const result = HashSet.union(selfSet, thatIterable)
// The result contains all elements from both sets (without duplicates)
assert.deepStrictEqual(
HashSet.toValues(result).sort(),
[1, 2, 3, 4, 5]
)
// The original sets are unchanged
assert.deepStrictEqual(HashSet.toValues(selfSet).sort(), [1, 2, 3])
assert.deepStrictEqual(
HashSet.toValues(thatIterable).sort(),
[3, 4, 5]
)
// You can also use arrays or other iterables
const unionWithArray = HashSet.union(selfSet, [4, 5, 6])
assert.deepStrictEqual(
HashSet.toValues(unionWithArray).sort(),
[1, 2, 3, 4, 5, 6]
)

union
(
const set2: HashSet.HashSet<number>
set2
,
const set3: HashSet.HashSet<number>
set3
)
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import HashSet
HashSet
.
const toValues: <number>(self: HashSet.HashSet<number>) => number[]

Returns an Array of the values within the HashSet.

Time complexity: O(n) where n is the number of elements in the set

@memberofHashSet

@since3.13.0

@example

import { HashSet, pipe } from "effect"
import { deepStrictEqual } from "node:assert/strict"
deepStrictEqual(
pipe(
HashSet.make(0, 1, 1, 2), // HashSet<number>
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)

@seeOther HashSet getters are values size

toValues
(
const union: HashSet.HashSet<number>
union
))
// Output: [1, 2, 3, 4, 5]
// Shared values
const
const intersection: HashSet.HashSet<number>
intersection
=
import HashSet
HashSet
.
const intersection: <number>(self: HashSet.HashSet<number>, that: Iterable<number>) => HashSet.HashSet<number> (+1 overload)

@example

intersection data-first API

import { HashSet } from "effect"
import * as assert from "node:assert/strict"
// Create two sets with some overlapping elements
const set1 = HashSet.make(1, 2, 3)
const set2 = HashSet.make(2, 3, 4)
// Compute the intersection using data-first API
const result = HashSet.intersection(set1, set2)
// The result contains only elements that are in both sets
assert.deepStrictEqual(HashSet.toValues(result).sort(), [2, 3])
// The original sets are unchanged
assert.deepStrictEqual(HashSet.toValues(set1).sort(), [1, 2, 3])
assert.deepStrictEqual(HashSet.toValues(set2).sort(), [2, 3, 4])
// You can also use arrays or other iterables
const intersectWithArray = HashSet.intersection(set1, [2, 3, 5])
assert.deepStrictEqual(
HashSet.toValues(intersectWithArray).sort(),
[2, 3]
)

intersection
(
const set2: HashSet.HashSet<number>
set2
,
const set3: HashSet.HashSet<number>
set3
)
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import HashSet
HashSet
.
const toValues: <number>(self: HashSet.HashSet<number>) => number[]

Returns an Array of the values within the HashSet.

Time complexity: O(n) where n is the number of elements in the set

@memberofHashSet

@since3.13.0

@example

import { HashSet, pipe } from "effect"
import { deepStrictEqual } from "node:assert/strict"
deepStrictEqual(
pipe(
HashSet.make(0, 1, 1, 2), // HashSet<number>
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)

@seeOther HashSet getters are values size

toValues
(
const intersection: HashSet.HashSet<number>
intersection
))
// Output: [3, 4]
// Values only in set2
const
const difference: HashSet.HashSet<number>
difference
=
import HashSet
HashSet
.
const difference: <number>(self: HashSet.HashSet<number>, that: Iterable<number>) => HashSet.HashSet<number> (+1 overload)

@example

difference data-first API

import { HashSet } from "effect"
import * as assert from "node:assert/strict"
// Create two sets with some overlapping elements
const thisSet = HashSet.make(1, 2, 3)
const thatIterable = HashSet.make(3, 4, 5)
// Compute the difference using data-first API
const result = HashSet.difference(thisSet, thatIterable)
// The result contains only elements from thisSet that are not in thatIterable
assert.deepStrictEqual(HashSet.toValues(result).sort(), [1, 2])
// The original sets are unchanged
assert.deepStrictEqual(HashSet.toValues(thisSet).sort(), [1, 2, 3])
assert.deepStrictEqual(
HashSet.toValues(thatIterable).sort(),
[3, 4, 5]
)
// You can also compute the difference in the other direction
const reverseResult = HashSet.difference(thatIterable, thisSet)
assert.deepStrictEqual(HashSet.toValues(reverseResult).sort(), [4, 5])

difference
(
const set2: HashSet.HashSet<number>
set2
,
const set3: HashSet.HashSet<number>
set3
)
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import HashSet
HashSet
.
const toValues: <number>(self: HashSet.HashSet<number>) => number[]

Returns an Array of the values within the HashSet.

Time complexity: O(n) where n is the number of elements in the set

@memberofHashSet

@since3.13.0

@example

import { HashSet, pipe } from "effect"
import { deepStrictEqual } from "node:assert/strict"
deepStrictEqual(
pipe(
HashSet.make(0, 1, 1, 2), // HashSet<number>
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)

@seeOther HashSet getters are values size

toValues
(
const difference: HashSet.HashSet<number>
difference
))
// Output: [1, 2]

Example (Chaining with pipe)

import {
import HashSet
HashSet
,
function pipe<A>(a: A): A (+19 overloads)

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"
const result = 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.

Here's an illustration of how pipe works:

┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌────────┐
│ input │───►│ func1 │───►│ func2 │───►│ ... │───►│ funcN │───►│ result │
└───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └────────┘

It's important to note that functions passed to pipe must have a single argument because they are only called with a single argument.

When to Use

This is useful in combination with data-last functions as a simulation of methods:

as.map(f).filter(g)

becomes:

import { pipe, Array } from "effect"
pipe(as, Array.map(f), Array.filter(g))

Example (Chaining Arithmetic Operations)

import { pipe } from "effect"
// Define simple arithmetic operations
const increment = (x: number) => x + 1
const double = (x: number) => x * 2
const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`
const result = pipe(5, increment, double, subtractTen)
console.log(result)
// Output: 2

@since2.0.0

pipe
} from "effect"
const
const result: number[]
result
=
pipe<HashSet.HashSet<number>, HashSet.HashSet<number>, HashSet.HashSet<number>, number[]>(a: HashSet.HashSet<number>, ab: (a: HashSet.HashSet<number>) => HashSet.HashSet<...>, bc: (b: HashSet.HashSet<...>) => HashSet.HashSet<...>, cd: (c: HashSet.HashSet<...>) => number[]): number[] (+19 overloads)

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"
const result = 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.

Here's an illustration of how pipe works:

┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌────────┐
│ input │───►│ func1 │───►│ func2 │───►│ ... │───►│ funcN │───►│ result │
└───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └────────┘

It's important to note that functions passed to pipe must have a single argument because they are only called with a single argument.

When to Use

This is useful in combination with data-last functions as a simulation of methods:

as.map(f).filter(g)

becomes:

import { pipe, Array } from "effect"
pipe(as, Array.map(f), Array.filter(g))

Example (Chaining Arithmetic Operations)

import { pipe } from "effect"
// Define simple arithmetic operations
const increment = (x: number) => x + 1
const double = (x: number) => x * 2
const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`
const result = pipe(5, increment, double, subtractTen)
console.log(result)
// Output: 2

@since2.0.0

pipe
(
// Duplicates are ignored
import HashSet
HashSet
.
const make: <[number, number, number, number, number, number, number]>(elements_0: number, elements_1: number, elements_2: number, elements_3: number, elements_4: number, elements_5: number, elements_6: number) => HashSet.HashSet<...>

Construct a new HashSet from a variable number of values.

Time complexity: O(n) where n is the number of elements

@memberofHashSet

@since2.0.0

@example

import { Equal, Hash, HashSet, pipe } from "effect"
import assert from "node:assert/strict"
class Character implements Equal.Equal {
readonly name: string
readonly trait: string
constructor(name: string, trait: string) {
this.name = name
this.trait = trait
}
// Define equality based on name, and trait
[Equal.symbol](that: Equal.Equal): boolean {
if (that instanceof Character) {
return (
Equal.equals(this.name, that.name) &&
Equal.equals(this.trait, that.trait)
)
}
return false
}
// Generate a hash code based on the sum of the character's name and trait
[Hash.symbol](): number {
return Hash.hash(this.name + this.trait)
}
static readonly of = (name: string, trait: string): Character => {
return new Character(name, trait)
}
}
assert.strictEqual(
Equal.equals(
HashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
),
// Is the same as adding each character to an empty set
pipe(
HashSet.empty(),
HashSet.add(Character.of("Alice", "Curious")),
HashSet.add(Character.of("Alice", "Curious")), // Alice tried to attend twice!
HashSet.add(Character.of("White Rabbit", "Always late")),
HashSet.add(Character.of("Mad Hatter", "Tea enthusiast"))
)
),
true,
"`HashSet.make` and `HashSet.empty() + HashSet.add()` should be equal"
)
assert.strictEqual(
Equal.equals(
HashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
),
HashSet.fromIterable([
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
])
),
true,
"`HashSet.make` and `HashSet.fromIterable` should be equal"
)

@seeOther HashSet constructors are {@linkcode fromIterable} {@linkcode empty}

make
(1, 2, 2, 3, 4, 5, 5),
// Keep even numbers
import HashSet
HashSet
.
const filter: <number>(predicate: Predicate<number>) => (self: HashSet.HashSet<number>) => HashSet.HashSet<number> (+3 overloads)

@example

import { HashSet, pipe, type Predicate } from "effect"
import * as assert from "node:assert/strict"
const filterPositiveNumbers: Predicate.Predicate<number> = (n) => n > 0
assert.deepStrictEqual(
pipe(
HashSet.make(-2, -1, 0, 1, 2),
HashSet.filter(filterPositiveNumbers)
),
HashSet.make(1, 2)
)

filter
((
n: number
n
) =>
n: number
n
% 2 === 0),
// Double each value
import HashSet
HashSet
.
const map: <number, number>(f: (a: number) => number) => (self: HashSet.HashSet<number>) => HashSet.HashSet<number> (+1 overload)

@example

import { HashSet, pipe } from "effect"
import * as assert from "node:assert/strict"
assert.deepStrictEqual(
pipe(
HashSet.make(0, 1, 2), // HashSet.HashSet<number>
HashSet.map((n) => String(n + 1)) // HashSet.HashSet<String>
),
HashSet.make("1", "2", "3")
)

map
((
n: number
n
) =>
n: number
n
* 2),
// Convert to array
import HashSet
HashSet
.
const toValues: <A>(self: HashSet.HashSet<A>) => Array<A>

Returns an Array of the values within the HashSet.

Time complexity: O(n) where n is the number of elements in the set

@memberofHashSet

@since3.13.0

@example

import { HashSet, pipe } from "effect"
import { deepStrictEqual } from "node:assert/strict"
deepStrictEqual(
pipe(
HashSet.make(0, 1, 1, 2), // HashSet<number>
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)

@seeOther HashSet getters are values size

toValues
)
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const result: number[]
result
)
// Output: [4, 8]

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.

CategoryOperationDescriptionComplexity
constructorsemptyCreates an empty MutableHashSetO(1)
constructorsfromIterableCreates a set from an iterableO(n)
constructorsmakeCreates a set from multiple valuesO(n)
elementshasChecks if a value exists in the setO(1) avg
elementsaddAdds a value to the setO(1) avg
elementsremoveRemoves a value from the setO(1) avg
getterssizeGets the number of elementsO(1)
mutationsclearRemoves all values from the setO(1)

Example (Working with a mutable set)

import {
import MutableHashSet
MutableHashSet
} from "effect"
// Create a mutable set with initial values
const
const set: MutableHashSet.MutableHashSet<number>
set
=
import MutableHashSet
MutableHashSet
.
const make: <[number, number, number]>(keys_0: number, keys_1: number, keys_2: number) => MutableHashSet.MutableHashSet<number>

@since2.0.0

make
(1, 2, 3)
// Add a new element (updates the set in place)
import MutableHashSet
MutableHashSet
.
const add: <number>(self: MutableHashSet.MutableHashSet<number>, key: number) => MutableHashSet.MutableHashSet<number> (+1 overload)

@since2.0.0

add
(
const set: MutableHashSet.MutableHashSet<number>
set
, 4)
// Check current contents
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
([...
const set: MutableHashSet.MutableHashSet<number>
set
])
// Output: [1, 2, 3, 4]
// Remove an element (modifies in place)
import MutableHashSet
MutableHashSet
.
const remove: <number>(self: MutableHashSet.MutableHashSet<number>, key: number) => MutableHashSet.MutableHashSet<number> (+1 overload)

@since2.0.0

remove
(
const set: MutableHashSet.MutableHashSet<number>
set
, 1)
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
([...
const set: MutableHashSet.MutableHashSet<number>
set
])
// Output: [2, 3, 4]
// Clear the set entirely
import MutableHashSet
MutableHashSet
.
const clear: <number>(self: MutableHashSet.MutableHashSet<number>) => MutableHashSet.MutableHashSet<number>

@since2.0.0

clear
(
const set: MutableHashSet.MutableHashSet<number>
set
)
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
import MutableHashSet
MutableHashSet
.
const size: <number>(self: MutableHashSet.MutableHashSet<number>) => number

@since2.0.0

size
(
const set: MutableHashSet.MutableHashSet<number>
set
))
// Output: 0

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)

import {
import HashSet
HashSet
,
import MutableHashSet
MutableHashSet
} from "effect"
// Immutable HashSet
const
const hashSet: HashSet.HashSet<number>
hashSet
=
import HashSet
HashSet
.
const make: <[number, number, number]>(elements_0: number, elements_1: number, elements_2: number) => HashSet.HashSet<number>

Construct a new HashSet from a variable number of values.

Time complexity: O(n) where n is the number of elements

@memberofHashSet

@since2.0.0

@example

import { Equal, Hash, HashSet, pipe } from "effect"
import assert from "node:assert/strict"
class Character implements Equal.Equal {
readonly name: string
readonly trait: string
constructor(name: string, trait: string) {
this.name = name
this.trait = trait
}
// Define equality based on name, and trait
[Equal.symbol](that: Equal.Equal): boolean {
if (that instanceof Character) {
return (
Equal.equals(this.name, that.name) &&
Equal.equals(this.trait, that.trait)
)
}
return false
}
// Generate a hash code based on the sum of the character's name and trait
[Hash.symbol](): number {
return Hash.hash(this.name + this.trait)
}
static readonly of = (name: string, trait: string): Character => {
return new Character(name, trait)
}
}
assert.strictEqual(
Equal.equals(
HashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
),
// Is the same as adding each character to an empty set
pipe(
HashSet.empty(),
HashSet.add(Character.of("Alice", "Curious")),
HashSet.add(Character.of("Alice", "Curious")), // Alice tried to attend twice!
HashSet.add(Character.of("White Rabbit", "Always late")),
HashSet.add(Character.of("Mad Hatter", "Tea enthusiast"))
)
),
true,
"`HashSet.make` and `HashSet.empty() + HashSet.add()` should be equal"
)
assert.strictEqual(
Equal.equals(
HashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
),
HashSet.fromIterable([
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
])
),
true,
"`HashSet.make` and `HashSet.fromIterable` should be equal"
)

@seeOther HashSet constructors are {@linkcode fromIterable} {@linkcode empty}

make
(1, 2, 3)
// Mutable variant
const
const mutableSet: MutableHashSet.MutableHashSet<number>
mutableSet
=
import MutableHashSet
MutableHashSet
.
const make: <[number, number, number]>(keys_0: number, keys_1: number, keys_2: number) => MutableHashSet.MutableHashSet<number>

@since2.0.0

make
(4, 5, 6)
// Convert HashSet to an iterator
//
// ┌─── IterableIterator<number>
// ▼
const
const iterable: IterableIterator<number>
iterable
=
import HashSet
HashSet
.
const values: <number>(self: HashSet.HashSet<number>) => IterableIterator<number>

Returns an IterableIterator of the values in the HashSet.

Time complexity: O(1)

@memberofHashSet

@since2.0.0

@example

import { HashSet, pipe } from "effect"
const numberIterable = pipe(
HashSet.make(0, 1, 1, 2), // HashSet.HashSet<number>
HashSet.values // takes an HashSet<A> and returns an IterableIterator<A>
)
for (const number of numberIterable) {
console.log(number) // it will logs: 0, 1, 2
}

@seeOther HashSet getters are toValues size

values
(
const hashSet: HashSet.HashSet<number>
hashSet
)
// Spread into console.log
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(...
const iterable: IterableIterator<number>
iterable
)
// Output: 1 2 3
// Use in a for...of loop
for (const
const value: number
value
of
const mutableSet: MutableHashSet.MutableHashSet<number>
mutableSet
) {
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const value: number
value
)
}
// Output: 4 5 6
// Convert to array with Array.from
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
var Array: ArrayConstructor
Array
.
ArrayConstructor.from<number>(iterable: Iterable<number> | ArrayLike<number>): number[] (+3 overloads)

Creates an array from an iterable object.

@paramiterable An iterable object to convert to an array.

from
(
const mutableSet: MutableHashSet.MutableHashSet<number>
mutableSet
))
// Output: [ 4, 5, 6 ]
// Convert immutable HashSet to array using toValues
//
// ┌─── Array<number>
// ▼
const
const array: number[]
array
=
import HashSet
HashSet
.
const toValues: <number>(self: HashSet.HashSet<number>) => number[]

Returns an Array of the values within the HashSet.

Time complexity: O(n) where n is the number of elements in the set

@memberofHashSet

@since3.13.0

@example

import { HashSet, pipe } from "effect"
import { deepStrictEqual } from "node:assert/strict"
deepStrictEqual(
pipe(
HashSet.make(0, 1, 1, 2), // HashSet<number>
HashSet.toValues // takes an HashSet<A> and returns an Array<A>
),
Array.of(0, 1, 2)
)

@seeOther HashSet getters are values size

toValues
(
const hashSet: HashSet.HashSet<number>
hashSet
)
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(new Error('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
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = 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(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

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()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const array: number[]
array
)
// Output: [ 1, 2, 3 ]