<A>(self: MutableList<A>, n: number): Array<A>Copies up to n elements from the beginning of the MutableList into a new
array without modifying the list.
When to use
Use when you need to inspect or snapshot a bounded prefix of the list without consuming it.
export const const toArrayN: <A>(
self: MutableList<A>,
n: number
) => Array<A>
Copies up to n elements from the beginning of the MutableList into a new
array without modifying the list.
When to use
Use when you need to inspect or snapshot a bounded prefix of the list without
consuming it.
toArrayN = <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> => {
const const length: numberlength = 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)
const const out: A[]out = 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>(const length: numberlength)
let let index: numberindex = 0
let let bucket:
| MutableList.Bucket<A>
| undefined
bucket = 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
while (let bucket:
| MutableList.Bucket<A>
| undefined
bucket) {
for (let let i: numberi = let bucket: MutableList.Bucket<A>let bucket: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
bucket.MutableList<in out A>.Bucket<A>.offset: numberoffset; let i: numberi < let bucket: MutableList.Bucket<A>let bucket: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
bucket.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; let i: numberi++) {
const out: A[]out[let index: numberindex++] = let bucket: MutableList.Bucket<A>let bucket: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
bucket.MutableList<in out A>.Bucket<A>.array: A[]array[let i: numberi]
if (let index: numberindex === const length: numberlength) return const out: A[]out
}
let bucket:
| MutableList.Bucket<A>
| undefined
bucket = let bucket: MutableList.Bucket<A>let bucket: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
bucket.MutableList<in out A>.Bucket<A>.next: MutableList.Bucket<A> | undefinednext
}
return const out: A[]out
}