<A>(
self: MutableList<A>,
messages: ReadonlyArray<A>,
mutable?: boolean
): numberAppends all elements from a ReadonlyArray to the end of the MutableList. This is an optimized version that can reuse the array when mutable=true. Returns the number of elements added.
When to use
Use when appending a trusted array directly is worth the optimized path and you control whether the input may be reused.
Gotchas
When mutable=true, the input array may be modified internally. Only use mutable=true when you control the array lifecycle.
Example (Appending arrays with optional mutation)
import { MutableList } from "effect"
const list = MutableList.make<number>()
MutableList.append(list, 1)
// Safe usage (default mutable=false)
const items = [2, 3, 4]
const added = MutableList.appendAllUnsafe(list, items)
console.log(added) // 3
console.log(items) // [2, 3, 4] - unchanged
// Unsafe but efficient usage (mutable=true)
const mutableItems = [5, 6, 7]
MutableList.appendAllUnsafe(list, mutableItems, true)
// mutableItems may be modified internally for efficiency
console.log(MutableList.takeAll(list)) // [1, 2, 3, 4, 5, 6, 7]
// High-performance bulk operations
const bigArray = new Array(10000).fill(0).map((_, i) => i)
MutableList.appendAllUnsafe(list, bigArray, true) // Very efficientexport const const appendAllUnsafe: <A>(
self: MutableList<A>,
messages: ReadonlyArray<A>,
mutable?: boolean
) => number
Appends all elements from a ReadonlyArray to the end of the MutableList.
This is an optimized version that can reuse the array when mutable=true.
Returns the number of elements added.
When to use
Use when appending a trusted array directly is worth the optimized path and
you control whether the input may be reused.
Gotchas
When mutable=true, the input array may be modified internally. Only use
mutable=true when you control the array lifecycle.
Example (Appending arrays with optional mutation)
import { MutableList } from "effect"
const list = MutableList.make<number>()
MutableList.append(list, 1)
// Safe usage (default mutable=false)
const items = [2, 3, 4]
const added = MutableList.appendAllUnsafe(list, items)
console.log(added) // 3
console.log(items) // [2, 3, 4] - unchanged
// Unsafe but efficient usage (mutable=true)
const mutableItems = [5, 6, 7]
MutableList.appendAllUnsafe(list, mutableItems, true)
// mutableItems may be modified internally for efficiency
console.log(MutableList.takeAll(list)) // [1, 2, 3, 4, 5, 6, 7]
// High-performance bulk operations
const bigArray = new Array(10000).fill(0).map((_, i) => i)
MutableList.appendAllUnsafe(list, bigArray, true) // Very efficient
appendAllUnsafe = <function (type parameter) A in <A>(self: MutableList<A>, messages: ReadonlyArray<A>, mutable?: boolean): numberA>(self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self: interface MutableList<in out A>A mutable linked list data structure optimized for high-throughput operations.
MutableList provides efficient append/prepend operations and is ideal for
producer-consumer patterns, queues, and streaming scenarios.
Example (Creating and consuming a mutable list)
import { MutableList } from "effect"
// Create a mutable list
const list: MutableList.MutableList<number> = MutableList.make()
// Add elements
MutableList.append(list, 1)
MutableList.append(list, 2)
MutableList.prepend(list, 0)
// Access properties
console.log(list.length) // 3
console.log(list.head?.array) // Contains elements from head bucket
console.log(list.tail?.array) // Contains elements from tail bucket
// Take elements
console.log(MutableList.take(list)) // 0
console.log(MutableList.take(list)) // 1
console.log(MutableList.take(list)) // 2
The MutableList namespace contains type definitions and utilities for working
with mutable linked lists.
Example (Typing queue processors)
import { MutableList } from "effect"
// Type annotation using the namespace
const processQueue = (queue: MutableList.MutableList<string>) => {
while (queue.length > 0) {
const item = MutableList.take(queue)
if (item !== MutableList.Empty) {
console.log("Processing:", item)
}
}
}
// Using the namespace for type definitions
const createProcessor = <T>(): {
queue: MutableList.MutableList<T>
add: (item: T) => void
process: () => Array<T>
} => {
const queue = MutableList.make<T>()
return {
queue,
add: (item) => MutableList.append(queue, item),
process: () => MutableList.takeAll(queue)
}
}
MutableList<function (type parameter) A in <A>(self: MutableList<A>, messages: ReadonlyArray<A>, mutable?: boolean): numberA>, messages: readonly A[]messages: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A>(self: MutableList<A>, messages: ReadonlyArray<A>, mutable?: boolean): numberA>, mutable: booleanmutable = false): number => {
if (messages: readonly A[]messages.ReadonlyArray<A>.length: numberGets the length of the array. This is a number one higher than the highest element defined in an array.
length === 0) {
return 0
}
const const chunk: MutableList.Bucket<A>const chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk: MutableList.interface MutableList<in out A>.Bucket<A>Storage node used by the exposed head and tail fields of a
MutableList.
Details
Most code should treat buckets as an implementation detail and use
MutableList operations such as append, prepend, and take instead
of constructing or mutating buckets directly.
Example (Inspecting buckets)
import { MutableList } from "effect"
const list = MutableList.make<number>()
MutableList.append(list, 1)
MutableList.append(list, 2)
// Access bucket information (for debugging or advanced usage)
const inspectBucket = (
bucket: MutableList.MutableList.Bucket<number> | undefined
) => {
if (bucket) {
console.log("Bucket array:", bucket.array)
console.log("Bucket offset:", bucket.offset)
console.log("Bucket mutable:", bucket.mutable)
console.log("Has next bucket:", bucket.next !== undefined)
}
}
inspectBucket(list.head)
inspectBucket(list.tail)
Bucket<function (type parameter) A in <A>(self: MutableList<A>, messages: ReadonlyArray<A>, mutable?: boolean): numberA> = {
MutableList<in out A>.Bucket<A>.array: A[]array: messages: readonly A[]messages as interface Array<T>Array<function (type parameter) A in <A>(self: MutableList<A>, messages: ReadonlyArray<A>, mutable?: boolean): numberA>,
MutableList<in out A>.Bucket<A>.mutable: booleanmutable,
MutableList<in out A>.Bucket<A>.offset: numberoffset: 0,
MutableList<in out A>.Bucket<A>.next: MutableList.Bucket<A> | undefinednext: var undefinedundefined
}
if (self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<A>.head: MutableList.Bucket<A> | undefinedhead) {
self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<A>.tail: MutableList.Bucket<A> | undefinedtail = self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<A>.tail: MutableList.Bucket<A> | undefinedtail!.MutableList<in out A>.Bucket<A>.next: MutableList.Bucket<A> | undefinednext = const chunk: MutableList.Bucket<A>const chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk
} else {
self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<A>.head: MutableList.Bucket<A> | undefinedhead = self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<A>.tail: MutableList.Bucket<A> | undefinedtail = const chunk: MutableList.Bucket<A>const chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk
}
self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<in out A>.length: numberlength += messages: readonly A[]messages.ReadonlyArray<A>.length: numberGets the length of the array. This is a number one higher than the highest element defined in an array.
length
return messages: readonly A[]messages.ReadonlyArray<A>.length: numberGets the length of the array. This is a number one higher than the highest element defined in an array.
length
}