# Chunk
import { Aside } from "@astrojs/starlight/components"
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.
<Aside type="caution" title="Use Chunk Only for Repeated Concatenation">
`Chunk` is optimized to manage the performance cost of repeated array
concatenation. For cases that do not involve repeated concatenation,
using `Chunk` may introduce unnecessary overhead, resulting in slower
performance.
</Aside>
## Why Use Chunk?
- **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.
## Creating a Chunk
### empty
Create an empty `Chunk` with `Chunk.empty`.
**Example** (Creating an Empty Chunk)
```ts twoslash
import { Chunk } from "effect"
// ┌─── Chunk<number>
// ▼
const chunk = Chunk.empty<number>()
```
### make
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)
```ts twoslash
import { Chunk } from "effect"
// ┌─── NonEmptyChunk<number>
// ▼
const chunk = Chunk.make(1, 2, 3)
```
### fromIterable
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)
```ts twoslash
import { Chunk, List } from "effect"
const fromArray = Chunk.fromIterable([1, 2, 3])
const fromList = Chunk.fromIterable(List.make(1, 2, 3))
```
<Aside type="caution" title="Performance Consideration">
`Chunk.fromIterable` creates a new copy of the iterable's elements. For
large data sets or repeated use, this cloning process can impact
performance.
</Aside>
### unsafeFromArray
`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)
```ts twoslash
import { Chunk } from "effect"
const chunk = Chunk.unsafeFromArray([1, 2, 3])
```
<Aside type="caution" title="Risk of Mutable Data">
Using `Chunk.unsafeFromArray` can lead to unexpected behavior if the
original array is modified after the chunk is created. For safer,
immutable behavior, use `Chunk.fromIterable` instead.
</Aside>
## Concatenating
To combine two `Chunk` instances into one, use `Chunk.appendAll`.
**Example** (Combining Two Chunks into One)
```ts twoslash
import { Chunk } from "effect"
// Concatenate two chunks with different types of elements
//
// ┌─── NonEmptyChunk<string | number>
// ▼
const chunk = Chunk.appendAll(Chunk.make(1, 2), Chunk.make("a", "b"))
console.log(chunk)
/*
Output:
{ _id: 'Chunk', values: [ 1, 2, 'a', 'b' ] }
*/
```
## Dropping
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)
```ts twoslash
import { Chunk } from "effect"
// Drops the first 2 elements from the Chunk
const chunk = Chunk.drop(Chunk.make(1, 2, 3, 4), 2)
```
## Comparing
To check if two `Chunk` instances are equal, use [`Equal.equals`](/docs/trait/equal/). This function compares the contents of each `Chunk` for structural equality.
**Example** (Comparing Two Chunks)
```ts twoslash
import { Chunk, Equal } from "effect"
const chunk1 = Chunk.make(1, 2)
const chunk2 = Chunk.make(1, 2, 3)
console.log(Equal.equals(chunk1, chunk1))
// Output: true
console.log(Equal.equals(chunk1, chunk2))
// Output: false
console.log(Equal.equals(chunk1, Chunk.make(1, 2)))
// Output: true
```
## Converting
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)
```ts twoslash
import { Chunk } from "effect"
// ┌─── readonly [number, ...number[]]
// ▼
const nonEmptyArray = Chunk.toReadonlyArray(Chunk.make(1, 2, 3))
// ┌─── readonly never[]
// ▼
const emptyArray = Chunk.toReadonlyArray(Chunk.empty())
declare const chunk: Chunk.Chunk<number>
// ┌─── readonly number[]
// ▼
const array = Chunk.toReadonlyArray(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.
Why Use Chunk?
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.
Creating a Chunk
empty
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
constchunk:Chunk.Chunk<number>
chunk=
import Chunk
Chunk.
constempty: <number>() =>Chunk.Chunk<number>
@since ― 2.0.0
empty<number>()
make
To create a Chunk with specific values, use Chunk.make(...values). Note that the resulting chunk is typed as non-empty.
Constructs a new List<A> from the specified values.
@since ― 2.0.0
make(1, 2, 3))
unsafeFromArray
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.
Builds a NonEmptyChunk from an non-empty collection of elements.
@since ― 2.0.0
make("a", "b"))
8
9
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Builds a NonEmptyChunk from an non-empty collection of elements.
@since ― 2.0.0
make(1, 2, 3)
5
6
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
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(newError('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
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=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(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
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()).
Builds a NonEmptyChunk from an non-empty collection of elements.
@since ― 2.0.0
make(1, 2)))
13
// Output: true
Converting
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.
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.
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.
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.