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)

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

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)

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)

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)

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

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])

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

Example (Combining Two Chunks into One)

import {
import Chunk
Chunk
} from "effect"
// Concatenate two chunks with different types of elements
//
// ┌─── 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"
assert.deepStrictEqual(
Chunk.make(1, 2).pipe(Chunk.appendAll(Chunk.make("a", "b")), Chunk.toArray),
[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
)
/*
Output:
{ _id: 'Chunk', values: [ 1, 2, 'a', 'b' ] }
*/

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)

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

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)

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
))
// Output: 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
))
// Output: 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)))
// 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)

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
)