Hyperlinkv0.8.0-beta.28

MutableList

MutableList.takeconsteffect/MutableList.ts:786
<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
}
elements
export const take = <A>(self: MutableList<A>): Empty | A => {
  if (!self.head) return Empty
  const message = self.head.array[self.head.offset]
  if (self.head.mutable) self.head.array[self.head.offset] = undefined as any
  self.head.offset++
  self.length--
  if (self.head.offset === self.head.array.length) {
    if (self.head.next) {
      self.head = self.head.next
    } else {
      clear(self)
    }
  }
  return message
}
Referenced by 5 symbols