<A>(self: MutableList<A>, f: (value: A, i: number) => boolean): voidFilters the MutableList in place, keeping only elements that satisfy the predicate. This operation modifies the list and rebuilds its internal structure for efficiency.
Example (Filtering in place)
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
// Keep only even numbers
MutableList.filter(list, (n) => n % 2 === 0)
console.log(MutableList.takeAll(list)) // [2, 4, 6, 8, 10]
// Filter with index
const indexed = MutableList.make<string>()
MutableList.appendAll(indexed, ["a", "b", "c", "d", "e"])
// Keep elements at even indices
MutableList.filter(indexed, (value, index) => index % 2 === 0)
console.log(MutableList.takeAll(indexed)) // ["a", "c", "e"]
// Real-world example: filtering a log queue
const logs = MutableList.make<{ level: string; message: string }>()
MutableList.appendAll(logs, [
{ level: "INFO", message: "App started" },
{ level: "ERROR", message: "Connection failed" },
{ level: "DEBUG", message: "Cache hit" },
{ level: "ERROR", message: "Timeout" }
])
// Keep only errors
MutableList.filter(logs, (log) => log.level === "ERROR")
console.log(MutableList.takeAll(logs).map((log) => log.message)) // ["Connection failed", "Timeout"]export const const filter: <A>(
self: MutableList<A>,
f: (value: A, i: number) => boolean
) => void
Filters the MutableList in place, keeping only elements that satisfy the predicate.
This operation modifies the list and rebuilds its internal structure for efficiency.
Example (Filtering in place)
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
// Keep only even numbers
MutableList.filter(list, (n) => n % 2 === 0)
console.log(MutableList.takeAll(list)) // [2, 4, 6, 8, 10]
// Filter with index
const indexed = MutableList.make<string>()
MutableList.appendAll(indexed, ["a", "b", "c", "d", "e"])
// Keep elements at even indices
MutableList.filter(indexed, (value, index) => index % 2 === 0)
console.log(MutableList.takeAll(indexed)) // ["a", "c", "e"]
// Real-world example: filtering a log queue
const logs = MutableList.make<{ level: string; message: string }>()
MutableList.appendAll(logs, [
{ level: "INFO", message: "App started" },
{ level: "ERROR", message: "Connection failed" },
{ level: "DEBUG", message: "Cache hit" },
{ level: "ERROR", message: "Timeout" }
])
// Keep only errors
MutableList.filter(logs, (log) => log.level === "ERROR")
console.log(MutableList.takeAll(logs).map((log) => log.message)) // ["Connection failed", "Timeout"]
filter = <function (type parameter) A in <A>(self: MutableList<A>, f: (value: A, i: number) => boolean): voidA>(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>, f: (value: A, i: number) => boolean): voidA>, f: (value: A, i: number) => booleanf: (value: Avalue: function (type parameter) A in <A>(self: MutableList<A>, f: (value: A, i: number) => boolean): voidA, i: numberi: number) => boolean): void => {
const const array: A[]array: interface Array<T>Array<function (type parameter) A in <A>(self: MutableList<A>, f: (value: A, i: number) => boolean): voidA> = []
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>, f: (value: A, i: number) => boolean): voidA> | 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> | undefinedhead
while (let chunk:
| MutableList.Bucket<A>
| undefined
chunk) {
for (let let i: numberi = 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 i: numberi < 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; let i: numberi++) {
if (f: (value: A, i: number) => booleanf(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 i: numberi], let i: numberi)) {
const array: A[]array.Array<A>.push(...items: A[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(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 i: numberi])
}
}
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
}
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 = {
MutableList<in out A>.Bucket<A>.array: A[]array,
MutableList<in out A>.Bucket<A>.mutable: booleanmutable: true,
MutableList<in out A>.Bucket<A>.offset: numberoffset: 0,
MutableList<in out A>.Bucket<A>.next: MutableList.Bucket<A> | undefinednext: var undefinedundefined
}
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 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
}