<A>(chunk: Chunk.Chunk<A>): Channel<A>Creates a Channel that emits all elements from a chunk.
Example (Creating channels from chunks)
import { Channel, Chunk } from "effect"
const chunk = Chunk.make(1, 2, 3)
const channel = Channel.fromChunk(chunk)
// Emits: 1, 2, 3export const const fromChunk: <A>(
chunk: Chunk.Chunk<A>
) => Channel<A>
Creates a Channel that emits all elements from a chunk.
Example (Creating channels from chunks)
import { Channel, Chunk } from "effect"
const chunk = Chunk.make(1, 2, 3)
const channel = Channel.fromChunk(chunk)
// Emits: 1, 2, 3
fromChunk = <function (type parameter) A in <A>(chunk: Chunk.Chunk<A>): Channel<A>A>(chunk: Chunk.Chunk<A>(parameter) chunk: {
length: number;
right: Chunk<A>;
left: Chunk<A>;
backing: Backing<A>;
depth: number;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
chunk: import ChunkChunk.interface Chunk<out A>A Chunk is an immutable, ordered collection optimized for efficient concatenation and access patterns.
Example (Inspecting chunk values)
import { Chunk } from "effect"
const chunk: Chunk.Chunk<number> = Chunk.make(1, 2, 3)
console.log(chunk.length) // 3
console.log(Chunk.toArray(chunk)) // [1, 2, 3]
A namespace containing utility types for Chunk operations.
Example (Working with Chunk utility types)
import type { Chunk } from "effect"
// Extract the element type from a Chunk
declare const chunk: Chunk.Chunk<string>
type ElementType = Chunk.Chunk.Infer<typeof chunk> // string
// Create a preserving non-emptiness
declare const nonEmptyChunk: Chunk.NonEmptyChunk<number>
type WithString = Chunk.Chunk.With<typeof nonEmptyChunk, string> // Chunk.NonEmptyChunk<string>
Chunk<function (type parameter) A in <A>(chunk: Chunk.Chunk<A>): Channel<A>A>): interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<function (type parameter) A in <A>(chunk: Chunk.Chunk<A>): Channel<A>A> => const fromArray: <A>(
array: ReadonlyArray<A>
) => Channel<A>
Creates a Channel that emits all elements from an array.
Example (Creating channels from arrays)
import { Channel } from "effect"
const channel = Channel.fromArray([1, 2, 3, 4, 5])
// Emits: 1, 2, 3, 4, 5
fromArray(import ChunkChunk.const toReadonlyArray: <
S extends Chunk<any>
>(
self: S
) => S extends NonEmptyChunk<any>
? RA.NonEmptyReadonlyArray<Chunk.Infer<S>>
: ReadonlyArray<Chunk.Infer<S>>
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.
Example (Converting chunks to readonly arrays)
import { Chunk } from "effect"
const chunk = Chunk.make(1, 2, 3)
const readonlyArray = Chunk.toReadonlyArray(chunk)
console.log(readonlyArray) // [1, 2, 3]
// The result is read-only, modifications would cause TypeScript errors
// readonlyArray[0] = 10 // TypeScript error
// With empty chunk
const emptyChunk = Chunk.empty<number>()
console.log(Chunk.toReadonlyArray(emptyChunk)) // []
toReadonlyArray(chunk: Chunk.Chunk<A>(parameter) chunk: {
length: number;
right: Chunk<A>;
left: Chunk<A>;
backing: Backing<A>;
depth: number;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
chunk))