Skip to content

HashSet

HashSet 表示一个无序唯一值集合,具有高效的查找、插入和删除操作。

Effect 库提供了此结构的两个版本:

两个版本平均都提供常数时间操作。主要区别在于它们如何处理更改:一个返回新集合,另一个修改原始集合。

HashSet 解决了维护每个值只出现一次的无序集合的问题,并提供了检查成员资格和添加/删除值的快速操作。

一些常见的用例包括:

  • 跟踪唯一项目(例如,完成某个操作的用户)
  • 高效测试集合中的成员资格
  • 执行集合操作,如并集、交集和差集
  • 从集合中消除重复项

在以下情况下选择 HashSet(任一变体)而不是其他集合:

  • 您需要确保元素是唯一的
  • 您经常需要检查元素是否存在于集合中
  • 您需要执行集合操作,如并集、交集和差集
  • 元素的顺序对您的用例不重要

在以下情况下选择其他集合:

  • 您需要维护插入顺序(使用 ListArray
  • 您需要键值关联(使用 HashMapMutableHashMap
  • 您需要频繁按索引访问元素(使用 Array

Effect 提供不可变和可变版本以支持不同的编码风格和性能需求。

HashSet

此版本从不修改原始集合。相反,它为每次更改返回一个新集合。

特征:

  • 操作返回新实例而不是修改原始实例
  • 保留先前状态
  • 设计上线程安全
  • 适用于函数式编程模式
  • 适合在应用程序的不同部分之间共享

MutableHashSet

此版本允许直接更新:添加和删除值会就地更改集合。

特征:

  • 操作直接修改原始集合
  • 在增量构建集合时更高效
  • 需要小心处理以避免意外的副作用
  • 在有许多修改的场景中性能更好
  • 适用于变更不会在其他地方引起问题的局部使用

在以下情况下使用 HashSet

  • 您需要没有副作用的可预测行为
  • 您想要保留数据的先前状态
  • 您在应用程序的不同部分之间共享集合
  • 您偏好函数式编程模式
  • 您在并发环境中需要纤程安全

在以下情况下使用 MutableHashSet

  • 性能至关重要,您需要避免创建新实例
  • 您正在通过许多添加/删除操作增量构建集合
  • 您在受控范围内工作,变更是安全的
  • 您需要在性能关键代码中优化内存使用

您可以使用 HashSet.mutate 在临时可变上下文中对 HashSet 应用多个更新。这允许您一次执行多个更改而不修改原始集合。

示例(批量更改而不变更原始集合)

import {
import HashSet
HashSet
} from "effect"
// 创建一个不可变的 HashSet
const
const original: HashSet.HashSet<number>
original
=
import HashSet
HashSet
.
const make: <[number, number, number]>(elements_0: number, elements_1: number, elements_2: number) => HashSet.HashSet<number>

Construct a new HashSet from a variable number of values.

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

@memberofHashSet

@since2.0.0

@example

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

@seeOther HashSet constructors are module:HashSet.fromIterable module:HashSet.empty

make
(1, 2, 3)
// 在临时可变草稿内应用多个更新
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

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

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

@example

// `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

// `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

// `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 module:HashSet.values module:HashSet.size

toValues
(
const original: HashSet.HashSet<number>
original
))
// 输出: [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 module:HashSet.values module:HashSet.size

toValues
(
const modified: HashSet.HashSet<number>
modified
))
// 输出: [2, 3, 4, 5] - 更改应用到新版本

HashSetMutableHashSet 都为核心操作提供类似的平均时间性能:

操作HashSetMutableHashSet描述
查找O(1) 平均O(1) 平均检查值是否存在
插入O(1) 平均O(1) 平均添加值
删除O(1) 平均O(1) 平均删除值
迭代O(n)O(n)迭代所有值
集合操作O(n)O(n)并集、交集、差集

主要区别在于如何处理更新:

  • HashSet 为每次更改返回一个新集合。如果连续进行许多更改,这可能会较慢。
  • MutableHashSet 就地更新同一个集合。在执行许多更改时,这通常更快。

HashSetMutableHashSet 都使用 Effect 的 Equal 特征来确定两个元素是否相同。这确保每个值在集合中只出现一次。

  • 原始值(如数字或字符串)按值比较,类似于 === 操作符。
  • 对象和自定义类型必须实现 Equal 接口来定义两个实例相等的含义。如果没有提供实现,等价性会回退到引用比较。

示例(使用自定义等价性和哈希)

import {
import Equal
Equal
,
import Hash
Hash
,
import HashSet
HashSet
} from "effect"
// 定义一个实现 Equal 接口的自定义类
class
class Person
Person
implements
import Equal
Equal
.
interface Equal

@since2.0.0

Equal
{
constructor(
readonly
Person.id: number
id
: number,
readonly
Person.name: string
name
: string,
readonly
Person.age: number
age
: number
) {}
// 如果两个 Person 实例的 id、name 和 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
}
// 哈希码基于 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
)
}
}
// 添加两个具有相同内容的不同实例
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 module:HashSet.make module:HashSet.fromIterable

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

@example

// `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

// `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))
)
// 只保留一个实例
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 module:HashSet.values module:HashSet.toValues

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

Effect 的 DataSchema.Data 模块基于结构等价性自动为您实现 Equal

示例(使用 Data.struct

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

Pipes the value of an expression into a pipeline of functions.

Details

The pipe function is a utility that allows us to compose functions in a readable and sequential manner. It takes the output of one function and passes it as the input to the next function in the pipeline. This enables us to build complex transformations by chaining multiple functions together.

import { pipe } from "effect"
const result = pipe(input, func1, func2, ..., funcN)

In this syntax, input is the initial value, and func1, func2, ..., funcN are the functions to be applied in sequence. The result of each function becomes the input for the next function, and the final result is returned.

Here's an illustration of how pipe works:

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

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

When to Use

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

as.map(f).filter(g)

becomes:

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

Example (Chaining Arithmetic Operations)

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

@since2.0.0

pipe
} from "effect"
// 定义两个具有相同内容的记录
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 })
// 它们是不同的对象引用
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

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

See util.format() for more information.

@sincev0.1.100

log
(
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

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

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

@paramvalue1 The first value.

@paramvalue2 The second value.

is
(
const person1: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person1
,
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
))
// 输出: false
// 但它们在值上相等(基于内容)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

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

See util.format() for more information.

@sincev0.1.100

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

@since2.0.0

equals
(
const person1: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person1
,
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
))
// 输出: true
// 将两者都添加到 HashSet — 只会存储一个
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<never>, ab: (a: HashSet.HashSet<never>) => HashSet.HashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>, bc: (b: HashSet.HashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>) => HashSet.HashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>): 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 module:HashSet.make module:HashSet.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<{
readonly id: number;
readonly name: string;
readonly age: number;
}> (+1 overload)

@example

// `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<{
readonly id: number;
readonly name: string;
readonly age: number;
}> (+1 overload)

@example

// `data-last` a.k.a. `pipeable` API
import { HashSet, pipe } from "effect"
import assert from "node:assert/strict"
assert.deepStrictEqual(
pipe(
HashSet.empty<number>(), // HashSet.HashSet<number>
HashSet.add(0),
HashSet.add(1),
HashSet.add(1),
HashSet.add(2),
HashSet.toValues
),
Array.of(0, 1, 2)
)

add
(
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
)
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

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

See util.format() for more information.

@sincev0.1.100

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

Calculates the number of values in the HashSet.

Time complexity: O(1)

@memberofHashSet

@since2.0.0

@example

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

@seeOther HashSet getters are module:HashSet.values module:HashSet.toValues

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

示例(使用 Schema.Data

import {
import Equal
Equal
,
import MutableHashSet
MutableHashSet
,
import Schema
Schema
} from "effect"
// 定义一个描述 Person 结构的 schema
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;
}, {
readonly id: number;
readonly name: string;
readonly age: number;
}>(value: Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}> & Schema.Schema<{
readonly id: number;
readonly name: string;
readonly age: number;
}, {
readonly id: number;
readonly name: string;
readonly age: number;
}, never>) => Schema.Data<Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>>

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

@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<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}> (+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) => {
readonly id: number;
readonly name: string;
readonly age: number;
}
Person
=
import Schema
Schema
.
decodeSync<{
readonly id: number;
readonly name: string;
readonly age: number;
}, {
readonly id: number;
readonly name: string;
readonly age: number;
}>(schema: Schema.Schema<{
readonly id: number;
readonly name: string;
readonly age: number;
}, {
readonly id: number;
readonly name: string;
readonly age: number;
}, never>, options?: ParseOptions): (i: {
readonly id: number;
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly age: number;
}
export decodeSync

@since3.10.0

decodeSync
(
const PersonSchema: Schema.Data<Schema.Struct<{
id: typeof Schema.Number;
name: typeof Schema.String;
age: typeof Schema.Number;
}>>
PersonSchema
)
const
const person1: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person1
=
const Person: (i: {
readonly id: number;
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly age: number;
}
Person
({
id: number
id
: 1,
name: string
name
: "Alice",
age: number
age
: 30 })
const
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
=
const Person: (i: {
readonly id: number;
readonly name: string;
readonly age: number;
}, overrideOptions?: ParseOptions) => {
readonly id: number;
readonly name: string;
readonly age: number;
}
Person
({
id: number
id
: 1,
name: string
name
: "Alice",
age: number
age
: 30 })
// person1 和 person2 是不同的实例但在值上相等
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

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

See util.format() for more information.

@sincev0.1.100

log
(
import 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
// 将两者都添加到 MutableHashSet — 只会存储一个
const
const set: MutableHashSet.MutableHashSet<{
readonly id: number;
readonly name: string;
readonly age: number;
}>
set
=
import MutableHashSet
MutableHashSet
.
const empty: <never>() => MutableHashSet.MutableHashSet<never>

Creates an empty mutable hash set.

This function initializes and returns an empty MutableHashSet instance, which allows for efficient storage and manipulation of unique elements.

Time complexity: O(1)

@memberofMutableHashSet

@since2.0.0

@example

import { MutableHashSet } from "effect"
type T = unknown // replace with your type
// in places where the type can't be inferred, replace with your type
const set: MutableHashSet.MutableHashSet<T> = MutableHashSet.empty<T>()

@returnsA new mutable instance of MutableHashSet containing no elements for the specified type K.

@seeOther MutableHashSet constructors are module:MutableHashSet.make module:MutableHashSet.fromIterable

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

data-last a.k.a. pipeable API

import { MutableHashSet, pipe } from "effect"
import assert from "node:assert/strict"
const mutableHashSet = pipe(
MutableHashSet.empty<number>(), // MutableHashSet.MutableHashSet<number>
MutableHashSet.add(0),
MutableHashSet.add(1),
MutableHashSet.add(1),
MutableHashSet.add(2)
)
assert.deepStrictEqual(
Array.from(mutableHashSet), // remember that MutableHashSet is also an Iterable
Array.of(0, 1, 2)
)

@paramkey - The key to be added to the MutableHashSet if not already present.

@returnsA function that accepts a MutableHashSet and returns the reference of the updated MutableHashSet including the key.

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

data-last a.k.a. pipeable API

import { MutableHashSet, pipe } from "effect"
import assert from "node:assert/strict"
const mutableHashSet = pipe(
MutableHashSet.empty<number>(), // MutableHashSet.MutableHashSet<number>
MutableHashSet.add(0),
MutableHashSet.add(1),
MutableHashSet.add(1),
MutableHashSet.add(2)
)
assert.deepStrictEqual(
Array.from(mutableHashSet), // remember that MutableHashSet is also an Iterable
Array.of(0, 1, 2)
)

@paramkey - The key to be added to the MutableHashSet if not already present.

@returnsA function that accepts a MutableHashSet and returns the reference of the updated MutableHashSet including the key.

add
(
const person2: {
readonly id: number;
readonly name: string;
readonly age: number;
}
person2
)
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

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

See util.format() for more information.

@sincev0.1.100

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

Calculates the number of values in the HashSet.

Time complexity: O(1)

@memberofMutableHashSet

@since2.0.0

@example

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

@paramself - The MutableHashSet instance for which the size is to be determined.

@returnsThe total number of elements within the MutableHashSet.

@seeOther MutableHashSet elements are module:MutableHashSet.add module:MutableHashSet.has module:MutableHashSet.remove module:MutableHashSet.clear

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

HashSet<A> 是一个不可变无序唯一值集合。 它保证每个值只出现一次,并支持查找、插入和删除等快速操作。

任何会修改集合的操作(如添加或删除值)都会返回一个新的 HashSet,保持原始集合不变。

类别操作描述时间复杂度
构造器empty创建空的 HashSetO(1)
构造器fromIterable从可迭代对象创建 HashSetO(n)
构造器make从多个值创建 HashSetO(n)
元素has检查值是否存在于集合中O(1) 平均
元素some检查是否有元素满足谓词O(n)
元素every检查是否所有元素满足谓词O(n)
元素isSubset检查集合是否为另一个的子集O(n)
获取器values获取所有值的 IteratorO(1)
获取器toValues获取所有值的 ArrayO(n)
获取器size获取元素数量O(1)
变更add向集合添加值O(1) 平均
变更remove从集合中删除值O(1) 平均
变更toggle切换值的存在状态O(1) 平均
操作difference计算集合差集 (A - B)O(n)
操作intersection计算集合交集 (A ∩ B)O(n)
操作union计算集合并集 (A ∪ B)O(n)
映射map转换每个元素O(n)
序列化flatMap转换并展平元素O(n)
遍历forEach对每个元素应用函数O(n)
折叠reduce将集合归约为单个值O(n)
过滤filter保留满足谓词的元素O(n)
分区partition按谓词将集合分为两个O(n)

示例(基本创建和操作)

import {
import HashSet
HashSet
} from "effect"
// 创建一个包含 3 个值的初始集合
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 module:HashSet.fromIterable module:HashSet.empty

make
(1, 2, 3)
// 添加一个值(返回新集合)
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

// `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)
// 原始集合保持不变
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 module:HashSet.values module:HashSet.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 module:HashSet.values module:HashSet.size

toValues
(
const set2: HashSet.HashSet<number>
set2
))
// 输出: [1, 2, 3, 4]
// 与另一个集合执行集合操作
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 module:HashSet.fromIterable module:HashSet.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

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

union
(
const set2: HashSet.HashSet<number>
set2
,
const set3: HashSet.HashSet<number>
set3
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

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

See util.format() for more information.

@sincev0.1.100

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

Returns an Array of the values within the HashSet.

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

@memberofHashSet

@since3.13.0

@example

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

@seeOther HashSet getters are module:HashSet.values module:HashSet.size

toValues
(
const union: HashSet.HashSet<number>
union
))
// 输出: [1, 2, 3, 4, 5]
// 共享值
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

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

intersection
(
const set2: HashSet.HashSet<number>
set2
,
const set3: HashSet.HashSet<number>
set3
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

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

See util.format() for more information.

@sincev0.1.100

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

Returns an Array of the values within the HashSet.

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

@memberofHashSet

@since3.13.0

@example

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

@seeOther HashSet getters are module:HashSet.values module:HashSet.size

toValues
(
const intersection: HashSet.HashSet<number>
intersection
))
// 输出: [3, 4]
// 仅在 set2 中的值
const
const difference: HashSet.HashSet<number>
difference
=
import HashSet
HashSet
.
const difference: <number>(self: HashSet.HashSet<number>, that: Iterable<number>) => HashSet.HashSet<number> (+1 overload)

@example

// `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 module:HashSet.values module:HashSet.size

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

示例(使用 pipe 链式操作)

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

Pipes the value of an expression into a pipeline of functions.

Details

The pipe function is a utility that allows us to compose functions in a readable and sequential manner. It takes the output of one function and passes it as the input to the next function in the pipeline. This enables us to build complex transformations by chaining multiple functions together.

import { pipe } from "effect"
const result = pipe(input, func1, func2, ..., funcN)

In this syntax, input is the initial value, and func1, func2, ..., funcN are the functions to be applied in sequence. The result of each function becomes the input for the next function, and the final result is returned.

Here's an illustration of how pipe works:

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

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

When to Use

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

as.map(f).filter(g)

becomes:

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

Example (Chaining Arithmetic Operations)

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

@since2.0.0

pipe
} from "effect"
const
const result: number[]
result
=
pipe<HashSet.HashSet<number>, HashSet.HashSet<number>, HashSet.HashSet<number>, number[]>(a: HashSet.HashSet<number>, ab: (a: HashSet.HashSet<number>) => HashSet.HashSet<number>, bc: (b: HashSet.HashSet<number>) => HashSet.HashSet<number>, cd: (c: HashSet.HashSet<number>) => 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<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 module:HashSet.fromIterable module:HashSet.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 module:HashSet.values module:HashSet.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]

MutableHashSet<A> 是一个可变无序唯一值集合。 与 HashSet 不同,它允许直接修改,addremoveclear 等操作会更新原始集合而不是返回新集合。

当您需要重复构建或更新集合时,这种可变性可以提高性能,特别是在局部或隔离的范围内。

类别操作描述复杂度
构造器empty创建空的 MutableHashSetO(1)
构造器fromIterable从可迭代对象创建集合O(n)
构造器make从多个值创建集合O(n)
元素has检查值是否存在于集合中O(1) 平均
元素add向集合添加值O(1) 平均
元素remove从集合中删除值O(1) 平均
获取器size获取元素数量O(1)
变更clear从集合中删除所有值O(1)

示例(使用可变集合)

import {
import MutableHashSet
MutableHashSet
} from "effect"
// 创建一个带有初始值的可变集合
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>

Construct a new MutableHashSet from a variable number of values.

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

@memberofMutableHashSet

@since2.0.0

@example

import { Equal, Hash, MutableHashSet } 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)
}
}
const mutableCharacterHashSet = MutableHashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
)
assert.equal(
MutableHashSet.has(
mutableCharacterHashSet,
Character.of("Alice", "Curious")
),
true
)
assert.equal(
MutableHashSet.has(
mutableCharacterHashSet,
Character.of("Fluffy", "Kind")
),
false
)

@seeOther MutableHashSet constructors are module:MutableHashSet.fromIterable module:MutableHashSet.empty

make
(1, 2, 3)
// 添加新元素(就地更新集合)
import MutableHashSet
MutableHashSet
.
const add: <number>(self: MutableHashSet.MutableHashSet<number>, key: number) => MutableHashSet.MutableHashSet<number> (+1 overload)

data-first API

import { MutableHashSet } from "effect"
import assert from "node:assert/strict"
const empty = MutableHashSet.empty<number>()
const withZero = MutableHashSet.add(empty, 0)
const withOne = MutableHashSet.add(withZero, 1)
const withTwo = MutableHashSet.add(withOne, 2)
const withTwoTwo = MutableHashSet.add(withTwo, 2)
assert(Object.is(withTwoTwo, empty)) // proof that it does mutate the original set
assert.deepStrictEqual(
Array.from(withTwoTwo), // remember that MutableHashSet is also an Iterable
Array.of(0, 1, 2)
)

@paramself - The MutableHashSet instance from which the key should be added to.

@paramkey - The key to be added to the MutableHashSet if not already present.

@returnsThe reference of the updated MutableHashSet including the key.

add
(
const set: MutableHashSet.MutableHashSet<number>
set
, 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
([...
const set: MutableHashSet.MutableHashSet<number>
set
])
// 输出: [1, 2, 3, 4]
// 删除元素(就地修改)
import MutableHashSet
MutableHashSet
.
const remove: <number>(self: MutableHashSet.MutableHashSet<number>, key: number) => MutableHashSet.MutableHashSet<number> (+1 overload)

data-first API

import { MutableHashSet, pipe } from "effect"
import assert from "node:assert/strict"
const set = MutableHashSet.make(0, 1, 2)
const result = MutableHashSet.remove(set, 0)
assert(Object.is(set, result)) // set and result have the same identity
assert.equal(MutableHashSet.has(result, 0), false) // it has correctly removed 0
assert.equal(MutableHashSet.has(set, 0), false) // it mutates the original MutableHashSet
assert.equal(MutableHashSet.has(result, 1), true)
assert.equal(MutableHashSet.has(result, 2), true)

@paramself - The MutableHashSet to which the key will be removed from.

@paramkey - The value to be removed from the MutableHashSet if present.

@returnsThe reference to the updated MutableHashSet.

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]
// 完全清空集合
import MutableHashSet
MutableHashSet
.
const clear: <number>(self: MutableHashSet.MutableHashSet<number>) => MutableHashSet.MutableHashSet<number>

Removes all values from the MutableHashSet.

This function operates by delegating the clearing action to the underlying key map associated with the given MutableHashSet. It ensures that the hash set becomes empty while maintaining its existence and structure.

@memberofMutableHashSet

@since2.0.0

@example

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

@paramself - The MutableHashSet to clear.

@returnsThe same MutableHashSet after all elements have been removed.

@seeOther MutableHashSet elements are module:MutableHashSet.add module:MutableHashSet.has module:MutableHashSet.remove module:MutableHashSet.size

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

Calculates the number of values in the HashSet.

Time complexity: O(1)

@memberofMutableHashSet

@since2.0.0

@example

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

@paramself - The MutableHashSet instance for which the size is to be determined.

@returnsThe total number of elements within the MutableHashSet.

@seeOther MutableHashSet elements are module:MutableHashSet.add module:MutableHashSet.has module:MutableHashSet.remove module:MutableHashSet.clear

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

HashSetMutableHashSet 都实现了 Iterable 接口,因此您可以将它们与 JavaScript 功能一起使用,例如:

  • 展开操作符 (...)
  • for...of 循环
  • Array.from

您还可以使用 .toValues 将值提取为数组。

示例(以 JS 原生方式使用 HashSet 值)

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

Construct a new HashSet from a variable number of values.

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

@memberofHashSet

@since2.0.0

@example

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

@seeOther HashSet constructors are module:HashSet.fromIterable module:HashSet.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>

Construct a new MutableHashSet from a variable number of values.

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

@memberofMutableHashSet

@since2.0.0

@example

import { Equal, Hash, MutableHashSet } 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)
}
}
const mutableCharacterHashSet = MutableHashSet.make(
Character.of("Alice", "Curious"),
Character.of("Alice", "Curious"),
Character.of("White Rabbit", "Always late"),
Character.of("Mad Hatter", "Tea enthusiast")
)
assert.equal(
MutableHashSet.has(
mutableCharacterHashSet,
Character.of("Alice", "Curious")
),
true
)
assert.equal(
MutableHashSet.has(
mutableCharacterHashSet,
Character.of("Fluffy", "Kind")
),
false
)

@seeOther MutableHashSet constructors are module:MutableHashSet.fromIterable module:MutableHashSet.empty

make
(4, 5, 6)
// 将 HashSet 转换为迭代器
//
// ┌─── IterableIterator<number>
// ▼
const
const iterable: IterableIterator<number>
iterable
=
import HashSet
HashSet
.
const values: <number>(self: HashSet.HashSet<number>) => IterableIterator<number>

Returns an IterableIterator of the values in the HashSet.

Time complexity: O(1)

@memberofHashSet

@since2.0.0

@example

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

@seeOther HashSet getters are module:HashSet.toValues module:HashSet.size

values
(
const hashSet: HashSet.HashSet<number>
hashSet
)
// 展开到 console.log
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

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

See util.format() for more information.

@sincev0.1.100

log
(...
const iterable: IterableIterator<number>
iterable
)
// 输出: 1 2 3
// 在 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
)
}
// 输出: 4 5 6
// 使用 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
))
// 输出: [ 4, 5, 6 ]
// 使用 toValues 将不可变 HashSet 转换为数组
//
// ┌─── Array<number>
// ▼
const
const array: number[]
array
=
import HashSet
HashSet
.
const toValues: <number>(self: HashSet.HashSet<number>) => number[]

Returns an Array of the values within the HashSet.

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

@memberofHashSet

@since3.13.0

@example

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

@seeOther HashSet getters are module:HashSet.values module:HashSet.size

toValues
(
const hashSet: HashSet.HashSet<number>
hashSet
)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

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

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

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

See util.format() for more information.

@sincev0.1.100

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