Skip to content

Chunk

Chunk<A> 表示类型为 A 的值的有序、不可变集合。虽然类似于数组,但 Chunk 提供了函数式接口,优化了某些在常规数组中可能代价高昂的操作,如重复连接。

  • 不可变性:与可变的标准 JavaScript 数组不同,Chunk 提供了真正的不可变集合,防止数据在创建后被修改。这在并发编程上下文中特别有用,不可变性可以增强数据一致性。

  • 高性能Chunk 支持高效数组操作的专门操作,如追加单个元素或连接块,使这些操作比其常规 JavaScript 数组等价物更快。

使用 Chunk.empty 创建一个空的 Chunk

示例(创建空 Chunk)

import {
import Chunk
Chunk
} from "effect"
// ┌─── Chunk<number>
// ▼
const
const chunk: Chunk.Chunk<number>
chunk
=
import Chunk
Chunk
.
const empty: <number>() => Chunk.Chunk<number>

@since2.0.0

empty
<number>()

要创建具有特定值的 Chunk,请使用 Chunk.make(...values)。请注意,结果块的类型为非空。

示例(创建非空 Chunk)

import {
import Chunk
Chunk
} from "effect"
// ┌─── NonEmptyChunk<number>
// ▼
const
const chunk: Chunk.NonEmptyChunk<number>
chunk
=
import Chunk
Chunk
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Chunk.NonEmptyChunk<number>

Builds a NonEmptyChunk from an non-empty collection of elements.

@since2.0.0

make
(1, 2, 3)

您可以通过提供集合来创建 Chunk,可以从可迭代对象或直接从数组创建。

示例(从可迭代对象创建 Chunk)

import {
import Chunk
Chunk
,
import List
List
} from "effect"
const
const fromArray: Chunk.Chunk<number>
fromArray
=
import Chunk
Chunk
.
const fromIterable: <number>(self: Iterable<number>) => Chunk.Chunk<number>

Creates a new Chunk from an iterable collection of values.

@since2.0.0

fromIterable
([1, 2, 3])
const
const fromList: Chunk.Chunk<number>
fromList
=
import Chunk
Chunk
.
const fromIterable: <number>(self: Iterable<number>) => Chunk.Chunk<number>

Creates a new Chunk from an iterable collection of values.

@since2.0.0

fromIterable
(
import List
List
.
const make: <[number, number, number]>(elements_0: number, elements_1: number, elements_2: number) => List.Cons<number>

Constructs a new List<A> from the specified values.

@since2.0.0

make
(1, 2, 3))

Chunk.unsafeFromArray 直接从数组创建 Chunk 而不进行克隆。这种方法可以通过避免复制数据的开销来提高性能,但需要谨慎,因为它绕过了通常的不可变性保证。

示例(直接从数组创建 Chunk)

import {
import Chunk
Chunk
} from "effect"
const
const chunk: Chunk.Chunk<number>
chunk
=
import Chunk
Chunk
.
const unsafeFromArray: <number>(self: readonly number[]) => Chunk.Chunk<number>

Wraps an array into a chunk without copying, unsafe on mutable arrays

@since2.0.0

unsafeFromArray
([1, 2, 3])

要将两个 Chunk 实例合并为一个,请使用 Chunk.appendAll

示例(将两个 Chunks 合并为一个)

import {
import Chunk
Chunk
} from "effect"
// 连接两个具有不同类型元素的块
//
// ┌─── NonEmptyChunk<string | number>
// ▼
const
const chunk: Chunk.NonEmptyChunk<string | number>
chunk
=
import Chunk
Chunk
.
const appendAll: <number, string>(self: Chunk.Chunk<number>, that: Chunk.NonEmptyChunk<string>) => Chunk.NonEmptyChunk<string | number> (+3 overloads)

Concatenates two chunks, combining their elements. If either chunk is non-empty, the result is also a non-empty chunk.

Example

import { Chunk } from "effect"
const result = Chunk.make(1, 2).pipe(Chunk.appendAll(Chunk.make("a", "b")), Chunk.toArray)
console.log(result)
// [ 1, 2, "a", "b" ]

@since2.0.0

appendAll
(
import Chunk
Chunk
.
const make: <[number, number]>(as_0: number, as_1: number) => Chunk.NonEmptyChunk<number>

Builds a NonEmptyChunk from an non-empty collection of elements.

@since2.0.0

make
(1, 2),
import Chunk
Chunk
.
const make: <[string, string]>(as_0: string, as_1: string) => Chunk.NonEmptyChunk<string>

Builds a NonEmptyChunk from an non-empty collection of elements.

@since2.0.0

make
("a", "b"))
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 chunk: Chunk.NonEmptyChunk<string | number>
chunk
)
/*
输出:
{ _id: 'Chunk', values: [ 1, 2, 'a', 'b' ] }
*/

要从 Chunk 的开头删除元素,请使用 Chunk.drop,指定要丢弃的元素数量。

示例(从开头删除元素)

import {
import Chunk
Chunk
} from "effect"
// 从 Chunk 中删除前 2 个元素
const
const chunk: Chunk.Chunk<number>
chunk
=
import Chunk
Chunk
.
const drop: <number>(self: Chunk.Chunk<number>, n: number) => Chunk.Chunk<number> (+1 overload)

Drops the first up to n elements from the chunk

@since2.0.0

drop
(
import Chunk
Chunk
.
const make: <[number, number, number, number]>(as_0: number, as_1: number, as_2: number, as_3: number) => Chunk.NonEmptyChunk<number>

Builds a NonEmptyChunk from an non-empty collection of elements.

@since2.0.0

make
(1, 2, 3, 4), 2)

要检查两个 Chunk 实例是否相等,请使用 Equal.equals。此函数比较每个 Chunk 的内容以进行结构相等性比较。

示例(比较两个 Chunks)

import {
import Chunk
Chunk
,
import Equal
Equal
} from "effect"
const
const chunk1: Chunk.NonEmptyChunk<number>
chunk1
=
import Chunk
Chunk
.
const make: <[number, number]>(as_0: number, as_1: number) => Chunk.NonEmptyChunk<number>

Builds a NonEmptyChunk from an non-empty collection of elements.

@since2.0.0

make
(1, 2)
const
const chunk2: Chunk.NonEmptyChunk<number>
chunk2
=
import Chunk
Chunk
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Chunk.NonEmptyChunk<number>

Builds a NonEmptyChunk from an non-empty collection of elements.

@since2.0.0

make
(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 Equal
Equal
.
function equals<Chunk.NonEmptyChunk<number>, Chunk.NonEmptyChunk<number>>(self: Chunk.NonEmptyChunk<number>, that: Chunk.NonEmptyChunk<number>): boolean (+1 overload)

@since2.0.0

equals
(
const chunk1: Chunk.NonEmptyChunk<number>
chunk1
,
const chunk1: Chunk.NonEmptyChunk<number>
chunk1
))
// 输出: true
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<Chunk.NonEmptyChunk<number>, Chunk.NonEmptyChunk<number>>(self: Chunk.NonEmptyChunk<number>, that: Chunk.NonEmptyChunk<number>): boolean (+1 overload)

@since2.0.0

equals
(
const chunk1: Chunk.NonEmptyChunk<number>
chunk1
,
const chunk2: Chunk.NonEmptyChunk<number>
chunk2
))
// 输出: 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<Chunk.NonEmptyChunk<number>, Chunk.NonEmptyChunk<number>>(self: Chunk.NonEmptyChunk<number>, that: Chunk.NonEmptyChunk<number>): boolean (+1 overload)

@since2.0.0

equals
(
const chunk1: Chunk.NonEmptyChunk<number>
chunk1
,
import Chunk
Chunk
.
const make: <[number, number]>(as_0: number, as_1: number) => Chunk.NonEmptyChunk<number>

Builds a NonEmptyChunk from an non-empty collection of elements.

@since2.0.0

make
(1, 2)))
// 输出: true

使用 Chunk.toReadonlyArrayChunk 转换为 ReadonlyArray。结果类型根据 Chunk 的内容而变化,区分空、非空和通用块。

示例(将 Chunk 转换为 ReadonlyArray)

import {
import Chunk
Chunk
} from "effect"
// ┌─── readonly [number, ...number[]]
// ▼
const
const nonEmptyArray: readonly [number, ...number[]]
nonEmptyArray
=
import Chunk
Chunk
.
const toReadonlyArray: <Chunk.NonEmptyChunk<number>>(self: Chunk.NonEmptyChunk<number>) => readonly [number, ...number[]]

Converts a Chunk into a ReadonlyArray. If the provided Chunk is non-empty (NonEmptyChunk), the function will return a NonEmptyReadonlyArray, ensuring the non-empty property is preserved.

@since2.0.0

toReadonlyArray
(
import Chunk
Chunk
.
const make: <[number, number, number]>(as_0: number, as_1: number, as_2: number) => Chunk.NonEmptyChunk<number>

Builds a NonEmptyChunk from an non-empty collection of elements.

@since2.0.0

make
(1, 2, 3))
// ┌─── readonly never[]
// ▼
const
const emptyArray: readonly never[]
emptyArray
=
import Chunk
Chunk
.
const toReadonlyArray: <Chunk.Chunk<never>>(self: Chunk.Chunk<never>) => readonly never[]

Converts a Chunk into a ReadonlyArray. If the provided Chunk is non-empty (NonEmptyChunk), the function will return a NonEmptyReadonlyArray, ensuring the non-empty property is preserved.

@since2.0.0

toReadonlyArray
(
import Chunk
Chunk
.
const empty: <never>() => Chunk.Chunk<never>

@since2.0.0

empty
())
declare const
const chunk: Chunk.Chunk<number>
chunk
:
import Chunk
Chunk
.
interface Chunk<out A>

@since2.0.0

@since2.0.0

Chunk
<number>
// ┌─── readonly number[]
// ▼
const
const array: readonly number[]
array
=
import Chunk
Chunk
.
const toReadonlyArray: <Chunk.Chunk<number>>(self: Chunk.Chunk<number>) => readonly number[]

Converts a Chunk into a ReadonlyArray. If the provided Chunk is non-empty (NonEmptyChunk), the function will return a NonEmptyReadonlyArray, ensuring the non-empty property is preserved.

@since2.0.0

toReadonlyArray
(
const chunk: Chunk.Chunk<number>
chunk
)