<A>(self: MutableList<A>): Empty | ATakes a single element from the beginning of the MutableList. Returns the element if available, or the Empty symbol if the list is empty. The taken element is removed from the list.
Example (Taking one element)
import { MutableList } from "effect"
const list = MutableList.make<string>()
MutableList.appendAll(list, ["first", "second", "third"])
// Take elements one by one
console.log(MutableList.take(list)) // "first"
console.log(list.length) // 2
console.log(MutableList.take(list)) // "second"
console.log(MutableList.take(list)) // "third"
console.log(list.length) // 0
// Take from empty list
console.log(MutableList.take(list)) // Empty symbol
// Check for empty using the Empty symbol
const result = MutableList.take(list)
if (result === MutableList.Empty) {
console.log("List is empty")
} else {
console.log("Got element:", result)
}
// Consumer pattern
function processNext<T>(
queue: MutableList.MutableList<T>,
processor: (item: T) => void
): boolean {
const item = MutableList.take(queue)
if (item !== MutableList.Empty) {
processor(item)
return true
}
return false
}export const const take: <A>(
self: MutableList<A>
) => Empty | A
Takes a single element from the beginning of the MutableList.
Returns the element if available, or the Empty symbol if the list is empty.
The taken element is removed from the list.
Example (Taking one element)
import { MutableList } from "effect"
const list = MutableList.make<string>()
MutableList.appendAll(list, ["first", "second", "third"])
// Take elements one by one
console.log(MutableList.take(list)) // "first"
console.log(list.length) // 2
console.log(MutableList.take(list)) // "second"
console.log(MutableList.take(list)) // "third"
console.log(list.length) // 0
// Take from empty list
console.log(MutableList.take(list)) // Empty symbol
// Check for empty using the Empty symbol
const result = MutableList.take(list)
if (result === MutableList.Empty) {
console.log("List is empty")
} else {
console.log("Got element:", result)
}
// Consumer pattern
function processNext<T>(
queue: MutableList.MutableList<T>,
processor: (item: T) => void
): boolean {
const item = MutableList.take(queue)
if (item !== MutableList.Empty) {
processor(item)
return true
}
return false
}
take = <function (type parameter) A in <A>(self: MutableList<A>): Empty | AA>(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>): Empty | AA>): type Empty = typeof EmptyDefines the unique symbol used to represent an empty result when taking elements from a MutableList.
This symbol is returned by take when the list is empty, allowing for safe type checking.
When to use
Use to detect that take returned no element before handling the result as a
list item.
Example (Checking for empty results)
import { MutableList } from "effect"
const list = MutableList.make<string>()
// Take from empty list returns Empty symbol
const result = MutableList.take(list)
console.log(result === MutableList.Empty) // true
// Safe pattern for checking emptiness
const processNext = (queue: MutableList.MutableList<string>) => {
const item = MutableList.take(queue)
if (item === MutableList.Empty) {
console.log("Queue is empty")
return null
}
return item.toUpperCase()
}
// Compare with other empty results
MutableList.append(list, "hello")
const next = MutableList.take(list)
console.log(next !== MutableList.Empty) // true, got "hello"
const empty = MutableList.take(list)
console.log(empty === MutableList.Empty) // true, list is empty
The type of the Empty symbol, used for type checking when taking elements from a MutableList.
This provides compile-time safety when checking for empty results.
Example (Handling empty results type-safely)
import { MutableList } from "effect"
const list = MutableList.make<number>()
// Type-safe handling of empty results
const takeAndDouble = (
queue: MutableList.MutableList<number>
): number | null => {
const item: number | MutableList.Empty = MutableList.take(queue)
if (item === MutableList.Empty) {
return null
}
// TypeScript knows item is number here
return item * 2
}
console.log(takeAndDouble(list)) // null (empty list)
MutableList.append(list, 5)
console.log(takeAndDouble(list)) // 10
// Type guard function
const isEmpty = (
result: number | MutableList.Empty
): result is MutableList.Empty => {
return result === MutableList.Empty
}
const value = MutableList.take(list)
if (isEmpty(value)) {
console.log("List is empty")
} else {
console.log("Got value:", value)
}
Empty | function (type parameter) A in <A>(self: MutableList<A>): Empty | AA => {
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) return const Empty: typeof EmptyDefines the unique symbol used to represent an empty result when taking elements from a MutableList.
This symbol is returned by take when the list is empty, allowing for safe type checking.
When to use
Use to detect that take returned no element before handling the result as a
list item.
Example (Checking for empty results)
import { MutableList } from "effect"
const list = MutableList.make<string>()
// Take from empty list returns Empty symbol
const result = MutableList.take(list)
console.log(result === MutableList.Empty) // true
// Safe pattern for checking emptiness
const processNext = (queue: MutableList.MutableList<string>) => {
const item = MutableList.take(queue)
if (item === MutableList.Empty) {
console.log("Queue is empty")
return null
}
return item.toUpperCase()
}
// Compare with other empty results
MutableList.append(list, "hello")
const next = MutableList.take(list)
console.log(next !== MutableList.Empty) // true, got "hello"
const empty = MutableList.take(list)
console.log(empty === MutableList.Empty) // true, list is empty
The type of the Empty symbol, used for type checking when taking elements from a MutableList.
This provides compile-time safety when checking for empty results.
Example (Handling empty results type-safely)
import { MutableList } from "effect"
const list = MutableList.make<number>()
// Type-safe handling of empty results
const takeAndDouble = (
queue: MutableList.MutableList<number>
): number | null => {
const item: number | MutableList.Empty = MutableList.take(queue)
if (item === MutableList.Empty) {
return null
}
// TypeScript knows item is number here
return item * 2
}
console.log(takeAndDouble(list)) // null (empty list)
MutableList.append(list, 5)
console.log(takeAndDouble(list)) // 10
// Type guard function
const isEmpty = (
result: number | MutableList.Empty
): result is MutableList.Empty => {
return result === MutableList.Empty
}
const value = MutableList.take(list)
if (isEmpty(value)) {
console.log("List is empty")
} else {
console.log("Got value:", value)
}
Empty
const const message: Amessage = 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[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]
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>(property) MutableList<A>.head: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
head.MutableList<in out A>.Bucket<A>.mutable: booleanmutable) 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[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] = var undefinedundefined as any
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++
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 (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 === 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.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) {
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>(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) {
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>.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>(property) MutableList<in out A>.Bucket<A>.next: {
array: Array<A>;
mutable: boolean;
offset: number;
next: Bucket<A> | undefined;
}
next
} else {
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 message: Amessage
}