Chunk
A Chunk<A>
represents a chunk of values of type A
.
Chunks are usually backed by arrays, but expose a purely functional, safe interface to the underlying elements, and they become lazy on operations that would be costly with arrays, such as repeated concatenation. Like lists and arrays, Chunk
is an ordered collection.
Let’s explore the reasons behind using Chunk
:
-
Immutability: In JavaScript, there is no built-in immutable data type that can efficiently represent an array. While the
Array
type exists, it provides a mutable interface, which means it can be modified after creation.Chunk
, on the other hand, is an immutable array-like data structure that ensures the data remains unchanged. Immutability is beneficial in various scenarios, especially when dealing with concurrent programming. -
High Performance:
Chunk
not only offers immutability but also delivers high performance. It provides specialized operations for common array manipulations, such as appending a single element or concatenating two chunks together. These specialized operations are significantly faster than performing the same operations on regular JavaScript arrays.
To create an empty Chunk
, you can use the following code:
1import { import Chunk
Chunk } from "effect"2
3const const emptyChunk: Chunk.Chunk<never>
emptyChunk = import Chunk
Chunk.const empty: <never>() => Chunk.Chunk<never>
empty()
If you want to create a Chunk
with specific values, you can use the Chunk.make(...values)
function:
1import { import Chunk
Chunk } from "effect"2
3const const nonEmptyChunk: Chunk.NonEmptyChunk<number>
nonEmptyChunk = 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 of values.
Creating a chunk from a generic Iterable
:
1import { import Chunk
Chunk, import List
List } from "effect"2
3const 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
5const 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.fromIterable
makes a chunk by cloning the iterable, which can be an expensive process for large iterables or when making many chunks.
Creating a chunk from an Array
:
1import { import Chunk
Chunk } from "effect"2
3const const fromUnsafeArray: Chunk.Chunk<number>
fromUnsafeArray = 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])
Chunk.unsafeFromArray
doesn’t do any cloning which can have performance benefits, but this breaks the assumption that chunk is immutable.
You can concatenate two Chunks using the appendAll
function:
1import { import Chunk
Chunk } from "effect"2
3const const concatenatedChunk: Chunk.NonEmptyChunk<string | number>
concatenatedChunk = 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(4 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),5 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")6)7
8namespace 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 concatenatedChunk: Chunk.NonEmptyChunk<string | number>
concatenatedChunk)9/*10Output:11{12 _id: "Chunk",13 values: [ 1, 2, "a", "b" ]14}15*/
To drop elements from the beginning of a Chunk
, you can use the drop
function:
1import { import Chunk
Chunk } from "effect"2
3const const droppedChunk: Chunk.Chunk<number>
droppedChunk = 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) // Drops the first 2 elements from the Chunk
You can compare two Chunk
s for equality using the Equal.equals
function:
1import { import Chunk
Chunk, import Equal
Equal } from "effect"2
3const 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)4const 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
6const const areEqual: boolean
areEqual = 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)
To convert a Chunk
to a ReadonlyArray
, you can use the toReadonlyArray
function:
1import { import Chunk
Chunk } from "effect"2
3const const readonlyArray: readonly [number, ...number[]]
readonlyArray = 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))