<A>(self: MutableList<A>, n: number): Array<A>Takes up to N elements from the beginning of the MutableList and returns them as an array. The taken elements are removed from the list. This operation is optimized for performance and includes zero-copy optimizations when possible.
Example (Taking batches)
import { MutableList } from "effect"
const list = MutableList.make<number>()
MutableList.appendAll(list, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
console.log(list.length) // 10
// Take first 3 elements
const first3 = MutableList.takeN(list, 3)
console.log(first3) // [1, 2, 3]
console.log(list.length) // 7
// Take more than available
const remaining = MutableList.takeN(list, 20)
console.log(remaining) // [4, 5, 6, 7, 8, 9, 10]
console.log(list.length) // 0
// Take from empty list
const empty = MutableList.takeN(list, 5)
console.log(empty) // []
// Batch processing pattern
const queue = MutableList.make<string>()
MutableList.appendAll(queue, ["task1", "task2", "task3", "task4", "task5"])
while (queue.length > 0) {
const batch = MutableList.takeN(queue, 2) // Process 2 at a time
console.log("Processing batch:", batch)
}export const const takeN: <A>(
self: MutableList<A>,
n: number
) => Array<A>
Takes up to N elements from the beginning of the MutableList and returns them as an array.
The taken elements are removed from the list. This operation is optimized for performance
and includes zero-copy optimizations when possible.
Example (Taking batches)
import { MutableList } from "effect"
const list = MutableList.make<number>()
MutableList.appendAll(list, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
console.log(list.length) // 10
// Take first 3 elements
const first3 = MutableList.takeN(list, 3)
console.log(first3) // [1, 2, 3]
console.log(list.length) // 7
// Take more than available
const remaining = MutableList.takeN(list, 20)
console.log(remaining) // [4, 5, 6, 7, 8, 9, 10]
console.log(list.length) // 0
// Take from empty list
const empty = MutableList.takeN(list, 5)
console.log(empty) // []
// Batch processing pattern
const queue = MutableList.make<string>()
MutableList.appendAll(queue, ["task1", "task2", "task3", "task4", "task5"])
while (queue.length > 0) {
const batch = MutableList.takeN(queue, 2) // Process 2 at a time
console.log("Processing batch:", batch)
}
takeN = <function (type parameter) A in <A>(self: MutableList<A>, n: number): Array<A>A>(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>, n: number): Array<A>A>, n: numbern: number): interface Array<T>Array<function (type parameter) A in <A>(self: MutableList<A>, n: number): Array<A>A> => {
if (n: numbern <= 0 || !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) return []
n: numbern = var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.min(...values: number[]): numberReturns the smaller of a set of supplied numeric expressions.
min(n: numbern, self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<in out A>.length: numberlength)
if (n: numbern === self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<in out A>.length: numberlength && self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<A>.head: MutableList.Bucket<A>(property) MutableList<A>.head: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
head?.MutableList<in out A>.Bucket<A>.offset: numberoffset === 0 && !self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<A>.head: MutableList.Bucket<A>(property) MutableList<A>.head: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
head.MutableList<in out A>.Bucket<A>.next: MutableList.Bucket<A> | undefinednext) {
const const array: A[]array = self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<A>.head: MutableList.Bucket<A>(property) MutableList<A>.head: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
head.MutableList<in out A>.Bucket<A>.array: A[]array
const clear: <A>(
self: MutableList<A>
) => void
Removes all elements from the MutableList, resetting it to an empty state.
This operation is highly optimized and releases all internal memory.
Example (Clearing a mutable list)
import { MutableList } from "effect"
const list = MutableList.make<number>()
MutableList.appendAll(list, [1, 2, 3, 4, 5])
console.log(list.length) // 5
// Clear all elements
MutableList.clear(list)
console.log(list.length) // 0
console.log(MutableList.take(list)) // Empty
// Can still use the list after clearing
MutableList.append(list, 42)
console.log(list.length) // 1
// Useful for resetting queues or buffers
function resetBuffer<T>(buffer: MutableList.MutableList<T>) {
MutableList.clear(buffer)
console.log("Buffer cleared and ready for reuse")
}
clear(self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self)
return const array: A[]array
}
const const array: A[]array = new var Array: ArrayConstructor
new <A>(arrayLength: number) => A[] (+2 overloads)
Array<function (type parameter) A in <A>(self: MutableList<A>, n: number): Array<A>A>(n: numbern)
let let index: numberindex = 0
let let chunk:
| MutableList.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>, n: number): Array<A>A> | undefined = self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<A>.head: MutableList.Bucket<A>(property) MutableList<A>.head: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
head
while (let chunk:
| MutableList.Bucket<A>
| undefined
chunk) {
while (let chunk: MutableList.Bucket<A>let chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk.MutableList<in out A>.Bucket<A>.offset: numberoffset < let chunk: MutableList.Bucket<A>let chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk.MutableList<in out A>.Bucket<A>.array: A[]array.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length) {
const array: A[]array[let index: numberindex++] = let chunk: MutableList.Bucket<A>let chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk.MutableList<in out A>.Bucket<A>.array: A[]array[let chunk: MutableList.Bucket<A>let chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk.MutableList<in out A>.Bucket<A>.offset: numberoffset]
if (let chunk: MutableList.Bucket<A>let chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk.MutableList<in out A>.Bucket<A>.mutable: booleanmutable) let chunk: MutableList.Bucket<A>let chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk.MutableList<in out A>.Bucket<A>.array: A[]array[let chunk: MutableList.Bucket<A>let chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk.MutableList<in out A>.Bucket<A>.offset: numberoffset] = var undefinedundefined as any
let chunk: MutableList.Bucket<A>let chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk.MutableList<in out A>.Bucket<A>.offset: numberoffset++
if (let index: numberindex === n: numbern) {
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 = let chunk: MutableList.Bucket<A>let 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 -= n: numbern
if (self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self.MutableList<in out A>.length: numberlength === 0) const clear: <A>(
self: MutableList<A>
) => void
Removes all elements from the MutableList, resetting it to an empty state.
This operation is highly optimized and releases all internal memory.
Example (Clearing a mutable list)
import { MutableList } from "effect"
const list = MutableList.make<number>()
MutableList.appendAll(list, [1, 2, 3, 4, 5])
console.log(list.length) // 5
// Clear all elements
MutableList.clear(list)
console.log(list.length) // 0
console.log(MutableList.take(list)) // Empty
// Can still use the list after clearing
MutableList.append(list, 42)
console.log(list.length) // 1
// Useful for resetting queues or buffers
function resetBuffer<T>(buffer: MutableList.MutableList<T>) {
MutableList.clear(buffer)
console.log("Buffer cleared and ready for reuse")
}
clear(self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self)
return const array: A[]array
}
}
let chunk:
| MutableList.Bucket<A>
| undefined
chunk = let chunk: MutableList.Bucket<A>let chunk: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
chunk.MutableList<in out A>.Bucket<A>.next: MutableList.Bucket<A> | undefinednext
}
const clear: <A>(
self: MutableList<A>
) => void
Removes all elements from the MutableList, resetting it to an empty state.
This operation is highly optimized and releases all internal memory.
Example (Clearing a mutable list)
import { MutableList } from "effect"
const list = MutableList.make<number>()
MutableList.appendAll(list, [1, 2, 3, 4, 5])
console.log(list.length) // 5
// Clear all elements
MutableList.clear(list)
console.log(list.length) // 0
console.log(MutableList.take(list)) // Empty
// Can still use the list after clearing
MutableList.append(list, 42)
console.log(list.length) // 1
// Useful for resetting queues or buffers
function resetBuffer<T>(buffer: MutableList.MutableList<T>) {
MutableList.clear(buffer)
console.log("Buffer cleared and ready for reuse")
}
clear(self: MutableList<A>(parameter) self: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
self)
return const array: A[]array
}