Skip to content

Chunk

A Chunk<A> represents an ordered, immutable collection of values of type A. While similar to an array, Chunk provides a functional interface, optimizing certain operations that can be costly with regular arrays, like repeated concatenation.

  • Immutability: Unlike standard JavaScript arrays, which are mutable, Chunk provides a truly immutable collection, preventing data from being modified after creation. This is especially useful in concurrent programming contexts where immutability can enhance data consistency.

  • High Performance: Chunk supports specialized operations for efficient array manipulation, such as appending single elements or concatenating chunks, making these operations faster than their regular JavaScript array equivalents.

Create an empty Chunk with Chunk.empty.

Example (Creating an Empty Chunk)

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

To create a Chunk with specific values, use Chunk.make(...values). Note that the resulting chunk is typed as non-empty.

Example (Creating a Non-Empty Chunk)

1
import {
import Chunk
Chunk
} from "effect"
2
3
// ┌─── NonEmptyChunk<number>
4
// ▼
5
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.

make
(1, 2, 3)

You can create a Chunk by providing a collection, either from an iterable or directly from an array.

Example (Creating a Chunk from an Iterable)

1
import {
import Chunk
Chunk
,
import List
List
} from "effect"
2
3
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.

fromIterable
([1, 2, 3])
4
5
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.

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.

make
(1, 2, 3))

Chunk.unsafeFromArray creates a Chunk directly from an array without cloning. This approach can improve performance by avoiding the overhead of copying data but requires caution, as it bypasses the usual immutability guarantees.

Example (Directly Creating a Chunk from an Array)

1
import {
import Chunk
Chunk
} from "effect"
2
3
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

unsafeFromArray
([1, 2, 3])

To combine two Chunk instances into one, use Chunk.appendAll.

Example (Combining Two Chunks into One)

1
import {
import Chunk
Chunk
} from "effect"
2
3
// Concatenate two chunks with different types of elements
4
//
5
// ┌─── NonEmptyChunk<string | number>
6
// ▼
7
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.

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.

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.

make
("a", "b"))
8
9
namespace console 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`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). 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`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js 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: ```js 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 ```

console
.
(method) 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)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js 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()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
const chunk: Chunk.NonEmptyChunk<string | number>
chunk
)
10
/*
11
Output:
12
{ _id: 'Chunk', values: [ 1, 2, 'a', 'b' ] }
13
*/

To remove elements from the beginning of a Chunk, use Chunk.drop, specifying the number of elements to discard.

Example (Dropping Elements from the Start)

1
import {
import Chunk
Chunk
} from "effect"
2
3
// Drops the first 2 elements from the Chunk
4
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

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.

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

To check if two Chunk instances are equal, use Equal.equals. This function compares the contents of each Chunk for structural equality.

Example (Comparing Two Chunks)

1
import {
import Chunk
Chunk
,
import Equal
Equal
} from "effect"
2
3
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.

make
(1, 2)
4
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.

make
(1, 2, 3)
5
6
namespace console 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`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). 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`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js 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: ```js 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 ```

console
.
(method) 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)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js 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()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<Chunk.NonEmptyChunk<number>, Chunk.NonEmptyChunk<number>>(self: Chunk.NonEmptyChunk<number>, that: Chunk.NonEmptyChunk<number>): boolean (+1 overload)
equals
(
const chunk1: Chunk.NonEmptyChunk<number>
chunk1
,
const chunk1: Chunk.NonEmptyChunk<number>
chunk1
))
7
// Output: true
8
9
namespace console 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`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). 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`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js 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: ```js 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 ```

console
.
(method) 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)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js 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()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
import Equal
Equal
.
function equals<Chunk.NonEmptyChunk<number>, Chunk.NonEmptyChunk<number>>(self: Chunk.NonEmptyChunk<number>, that: Chunk.NonEmptyChunk<number>): boolean (+1 overload)
equals
(
const chunk1: Chunk.NonEmptyChunk<number>
chunk1
,
const chunk2: Chunk.NonEmptyChunk<number>
chunk2
))
10
// Output: false
11
12
namespace console 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`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). 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`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js 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: ```js 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 ```

console
.
(method) 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)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js 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()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

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

make
(1, 2)))
13
// Output: true

Convert a Chunk to a ReadonlyArray using Chunk.toReadonlyArray. The resulting type varies based on the Chunk’s contents, distinguishing between empty, non-empty, and generic chunks.

Example (Converting a Chunk to a ReadonlyArray)

1
import {
import Chunk
Chunk
} from "effect"
2
3
// ┌─── readonly [number, ...number[]]
4
// ▼
5
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.

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.

make
(1, 2, 3))
6
7
// ┌─── readonly never[]
8
// ▼
9
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.

toReadonlyArray
(
import Chunk
Chunk
.
const empty: <never>() => Chunk.Chunk<never>
empty
())
10
11
declare const
const chunk: Chunk.Chunk<number>
chunk
:
import Chunk
Chunk
.
interface Chunk<out A> namespace Chunk
Chunk
<number>
12
13
// ┌─── readonly number[]
14
// ▼
15
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.

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