Skip to content

HashSet

A HashSet represents an un-ordered, collection of unique values with efficient lookup, insertion and removal operations. The Effect library provides two variants of this data structure:

  1. HashSet<A> - An immutable set implementation
  2. MutableHashSet<A> - A mutable set implementation

Both implementations store unique values and provide O(1) average-time complexity for core operations, but they differ in how modifications are handled.

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)

The Effect library provides two implementations to accommodate different programming styles and performance needs:

The immutable HashSet follows functional programming principles where data structures are never modified in place. Instead, operations that would change the set return a new instance with the changes applied, leaving the original set unchanged.

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

The mutable MutableHashSet allows direct modification of the underlying collection. Operations like add and remove change the set in place rather than creating new instances.

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
  • 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
  • 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

Another option is to use the bounded mutation context provided by the immutable HashSet:

import {
import HashSet
HashSet
} from "effect"
// Start with an immutable set
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)
// Use the mutation API for a series of changes
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
) => {
// this is bounded mutation context!
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
)) // [1, 2, 3] - original 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
)) // [2, 3, 4, 5] - new version with changes

Both HashSet variants provide similar time performance characteristics for their core operations:

OperationHashSetMutableHashSetDescription
LookupO(1) averageO(1) averageChecking if an element exists
InsertionO(1) averageO(1) averageAdding a new element
RemovalO(1) averageO(1) averageRemoving an existing element
IterationO(n)O(n)Traversing all elements
Set operationsO(n)O(n)Union, intersection, difference, etc.

The primary performance difference lies in how modifications are handled:

  • Immutable HashSet creates new instances for every modification, which can impact performance when making many sequential changes.
  • MutableHashSet modifies the existing structure, making it more efficient for multiple sequential modifications.

Both HashSet variants use Effect’s Equal trait to determine when elements are the same, ensuring that each element appears exactly once in the set. This applies to:

  • Primitive Values: Equality is determined by value (similar to the === operator).
  • Objects and Custom Types: For objects and other custom types, equality is determined by whether those types implement the Equal interface themselves. If an element type implements Equal, the HashSet will delegate to that implementation to perform the equality check. This allows you to define custom logic for determining when two instances of your objects should be considered equal based on their properties, rather than just their object identity. The fallback strategy is equality by reference.
import {
import Equal
Equal
,
import Hash
Hash
,
import HashSet
HashSet
} from "effect"
class
class Person
Person
implements
import Equal
Equal
.
interface Equal

@since2.0.0

Equal
{
constructor(
readonly
Person.id: number
id
: number, // Unique identifier
readonly
Person.name: string
name
: string,
readonly
Person.age: number
age
: number
) { }
// Define equality based on id, name, and age
[
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
}
// Generate a hash code based on the unique id
[
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
)
}
}
// Creating a HashSet with objects that implement the Equal interface
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))
)
// HashSet recognizes them as equal, so only one element is stored
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

Simplifying Equality and Hashing with Data and Schema:

Using Effect’s Data or Schema.Data modules can automatically implement the necessary Equal trait for your custom types:

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"
import
const assert: Omit<typeof assert, "equal" | "notEqual" | "deepEqual" | "notDeepEqual" | "ok" | "strictEqual" | "deepStrictEqual" | "ifError" | "strict"> & {
(value: unknown, message?: string | Error): asserts value;
... 8 more ...;
strict: typeof assert;
}

In strict assertion mode, non-strict methods behave like their corresponding strict methods. For example,

deepEqual

will behave like

deepStrictEqual

.

In strict assertion mode, error messages for objects display a diff. In legacy assertion mode, error messages for objects display the objects, often truncated.

To use strict assertion mode:

import { strict as assert } from 'node:assert';
import assert from 'node:assert/strict';

Example error diff:

import { strict as assert } from 'node:assert';
assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected ... Lines skipped
//
// [
// [
// ...
// 2,
// + 3
// - '3'
// ],
// ...
// 5
// ]

To deactivate the colors, use the NO_COLOR or NODE_DISABLE_COLORS environment variables. This will also deactivate the colors in the REPL. For more on color support in terminal environments, read the tty getColorDepth() documentation.

@sincev15.0.0, v13.9.0, v12.16.2, v9.9.0

assert
from "node:assert/strict"
// Data.* implements the `Equal` traits automatically
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 })
const assert: Omit<typeof assert, "equal" | "notEqual" | "deepEqual" | "notDeepEqual" | "ok" | "strictEqual" | "deepStrictEqual" | "ifError" | "strict"> & {
(value: unknown, message?: string | Error): asserts value;
... 8 more ...;
strict: typeof assert;
}

In strict assertion mode, non-strict methods behave like their corresponding strict methods. For example,

deepEqual

will behave like

deepStrictEqual

.

In strict assertion mode, error messages for objects display a diff. In legacy assertion mode, error messages for objects display the objects, often truncated.

To use strict assertion mode:

import { strict as assert } from 'node:assert';
import assert from 'node:assert/strict';

Example error diff:

import { strict as assert } from 'node:assert';
assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected ... Lines skipped
//
// [
// [
// ...
// 2,
// + 3
// - '3'
// ],
// ...
// 5
// ]

To deactivate the colors, use the NO_COLOR or NODE_DISABLE_COLORS environment variables. This will also deactivate the colors in the REPL. For more on color support in terminal environments, read the tty getColorDepth() documentation.

@sincev15.0.0, v13.9.0, v12.16.2, v9.9.0

assert
.
equal: <false>(actual: unknown, expected: false, message?: string | Error) => asserts actual is false

Tests strict equality between the actual and expected parameters as determined by Object.is().

import assert from 'node:assert/strict';
assert.strictEqual(1, 2);
// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
//
// 1 !== 2
assert.strictEqual(1, 1);
// OK
assert.strictEqual('Hello foobar', 'Hello World!');
// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
// + actual - expected
//
// + 'Hello foobar'
// - 'Hello World!'
// ^
const apples = 1;
const oranges = 2;
assert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);
// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2
assert.strictEqual(1, '1', new TypeError('Inputs are not identical'));
// TypeError: Inputs are not identical

If the values are not strictly equal, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.

@sincev0.1.21

equal
(
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
), false) // They are different references
const assert: Omit<typeof assert, "equal" | "notEqual" | "deepEqual" | "notDeepEqual" | "ok" | "strictEqual" | "deepStrictEqual" | "ifError" | "strict"> & {
(value: unknown, message?: string | Error): asserts value;
... 8 more ...;
strict: typeof assert;
}

In strict assertion mode, non-strict methods behave like their corresponding strict methods. For example,

deepEqual

will behave like

deepStrictEqual

.

In strict assertion mode, error messages for objects display a diff. In legacy assertion mode, error messages for objects display the objects, often truncated.

To use strict assertion mode:

import { strict as assert } from 'node:assert';
import assert from 'node:assert/strict';

Example error diff:

import { strict as assert } from 'node:assert';
assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected ... Lines skipped
//
// [
// [
// ...
// 2,
// + 3
// - '3'
// ],
// ...
// 5
// ]

To deactivate the colors, use the NO_COLOR or NODE_DISABLE_COLORS environment variables. This will also deactivate the colors in the REPL. For more on color support in terminal environments, read the tty getColorDepth() documentation.

@sincev15.0.0, v13.9.0, v12.16.2, v9.9.0

assert
.
equal: <true>(actual: unknown, expected: true, message?: string | Error) => asserts actual is true

Tests strict equality between the actual and expected parameters as determined by Object.is().

import assert from 'node:assert/strict';
assert.strictEqual(1, 2);
// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
//
// 1 !== 2
assert.strictEqual(1, 1);
// OK
assert.strictEqual('Hello foobar', 'Hello World!');
// AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
// + actual - expected
//
// + 'Hello foobar'
// - 'Hello World!'
// ^
const apples = 1;
const oranges = 2;
assert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);
// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2
assert.strictEqual(1, '1', new TypeError('Inputs are not identical'));
// TypeError: Inputs are not identical

If the values are not strictly equal, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.

@sincev0.1.21

equal
(
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
), true) // But they are equal in value
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
) // since `person1` and `person2` are equal, only one will be stored
)
// Only one element is stored because they're considered equal
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
import {
import Equal
Equal
,
import MutableHashSet
MutableHashSet
,
import Schema
Schema
} from "effect"
import
const assert: Omit<typeof assert, "equal" | "notEqual" | "deepEqual" | "notDeepEqual" | "ok" | "strictEqual" | "deepStrictEqual" | "ifError" | "strict"> & {
(value: unknown, message?: string | Error): asserts value;
... 8 more ...;
strict: typeof assert;
}

In strict assertion mode, non-strict methods behave like their corresponding strict methods. For example,

deepEqual

will behave like

deepStrictEqual

.

In strict assertion mode, error messages for objects display a diff. In legacy assertion mode, error messages for objects display the objects, often truncated.

To use strict assertion mode:

import { strict as assert } from 'node:assert';
import assert from 'node:assert/strict';

Example error diff:

import { strict as assert } from 'node:assert';
assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected ... Lines skipped
//
// [
// [
// ...
// 2,
// + 3
// - '3'
// ],
// ...
// 5
// ]

To deactivate the colors, use the NO_COLOR or NODE_DISABLE_COLORS environment variables. This will also deactivate the colors in the REPL. For more on color support in terminal environments, read the tty getColorDepth() documentation.

@sincev15.0.0, v13.9.0, v12.16.2, v9.9.0

assert
from "node:assert/strict"
// Schema.Data implements the `Equal` traits for us
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
})
)
const
const Person: (i: {
readonly id: number;
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>
Person
=
import Schema
Schema
.
const decode: <{
readonly id: number;
readonly name: string;
readonly age: number;
}, {
readonly id: number;
readonly name: string;
readonly age: number;
}, never>(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) => Effect<...>

@since3.10.0

decode
(
const PersonSchema: Schema.Data<Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>>
PersonSchema
)
const
const person1: Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>
person1
=
const Person: (i: {
readonly id: number;
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>
Person
({
id: number
id
: 1,
name: string
name
: "Alice",
age: number
age
: 30 })
const
const person2: Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>
person2
=
const Person: (i: {
readonly id: number;
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>
Person
({
id: number
id
: 1,
name: string
name
: "Alice",
age: number
age
: 30 })
function assert(value: unknown, message?: string | Error): asserts value

In strict assertion mode, non-strict methods behave like their corresponding strict methods. For example,

deepEqual

will behave like

deepStrictEqual

.

In strict assertion mode, error messages for objects display a diff. In legacy assertion mode, error messages for objects display the objects, often truncated.

To use strict assertion mode:

import { strict as assert } from 'node:assert';
import assert from 'node:assert/strict';

Example error diff:

import { strict as assert } from 'node:assert';
assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);
// AssertionError: Expected inputs to be strictly deep-equal:
// + actual - expected ... Lines skipped
//
// [
// [
// ...
// 2,
// + 3
// - '3'
// ],
// ...
// 5
// ]

To deactivate the colors, use the NO_COLOR or NODE_DISABLE_COLORS environment variables. This will also deactivate the colors in the REPL. For more on color support in terminal environments, read the tty getColorDepth() documentation.

@sincev15.0.0, v13.9.0, v12.16.2, v9.9.0

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

@since2.0.0

equals
(
const person1: Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>
person1
,
const person2: Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>
person2
)) // Output: true
const
const set: MutableHashSet.MutableHashSet<Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>>
set
=
import MutableHashSet
MutableHashSet
.
const empty: <never>() => MutableHashSet.MutableHashSet<never>

@since2.0.0

empty
().
Pipeable.pipe<MutableHashSet.MutableHashSet<never>, MutableHashSet.MutableHashSet<Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>>, 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: <Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>>(key: Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>) => (self: MutableHashSet.MutableHashSet<...>) => MutableHashSet.MutableHashSet<...> (+1 overload)

@since2.0.0

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

@since2.0.0

add
(
const person2: Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>
person2
)
)
// HashSet, thanks to Schema.Data implementation of the `Equal` trait, recognizes the two Person as equal, so only one element is stored
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: <Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>>(self: MutableHashSet.MutableHashSet<Effect<{
readonly id: number;
readonly name: string;
readonly age: number;
}, ParseError, never>>) => number

@since2.0.0

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

An HashSet<A> is an immutable collection of unique values. Once created, a HashSet cannot be modified; any operation that would alter the set instead returns a new HashSet with the changes. This immutability offers benefits like predictable state management and easier reasoning about your code.

Immutable HashSet provides operations for:

  • Transforming sets with map and flatMap
  • Filtering elements with filter
  • Combining sets with union, intersection and difference
  • Controlled mutability with mutate, beginMutation, and endMutation
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)

Creating and using an immutable HashSet:

import {
import HashSet
HashSet
} from "effect"
// Creating a new set
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)
// Adding elements (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 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 set1: HashSet.HashSet<number>
set1
)) // [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
)) // [1, 2, 3, 4]
// Set operations
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)
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
)
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
)
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 union: HashSet.HashSet<number>
union
)) // [1, 2, 3, 4, 5]
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
)) // [3, 4]
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
)) // [1, 2]

Using pipe for a more fluent API:

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
(
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),
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),
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),
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
) // [4, 8]

A MutableHashSet<A> is a mutable collection of unique values. Operations like add, remove, and clear directly modify the original set rather than creating a new one. This mutability offers benefits like improved performance in scenarios where you need to build or modify a set incrementally.

MutableHashSet provides operations for:

  • Directly modifying the set with add, remove, and clear
  • Checking for element existence with has
  • Converting to immutable collections when needed
  • Building sets incrementally with optimal performance
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)

Creating and using a mutable HashSet:

import {
import MutableHashSet
MutableHashSet
} from "effect"
// Creating a new mutable set
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)
// Adding elements (modifies the original set)
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)
// Set is modified in place
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
]) // [1, 2, 3, 4]
// Removing elements
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
]) // [2, 3, 4]
// Clearing the set
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
)) // 0

Both HashSet variants work well with standard JavaScript collection operations since they implement the Iterable interface; Plus they also provide methods to access its elements in formats readily usable by JavaScript APIs:

  • .values, which returns an IterableIterator<A>, and
  • .toValues which returns an Array<A>.
import {
import HashSet
HashSet
,
import MutableHashSet
MutableHashSet
} from "effect"
// ┌─── HashSet.HashSet<number>
// ▼
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)
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)
// Using HashSet.values to convert HashSet.HashSet<A> to IterableIterator<A>
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
)
// ▲
// └── IterableIterator<number>
// Using spread operator
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
) // Logs: 1 2 3
// Using for...of
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
)
}
// Using 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
)) // Logs: [ 4, 5, 6 ]
// Using HashSet.toValues to convert HashSet.HashSet<A> to Array<A>
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
)
// ▲
// └── Array<number>
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
) // Logs: [ 1, 2, 3 ]