Skip to content
Plugins (183)

ringbuffer

stable

A fixed-capacity circular buffer that overwrites the oldest element when full, with FIFO push/pop and random access.

use plugin ringbuffer::{RingBuffer.new, push, pop, …}
13 functions Utilities
Runs with the native Zolo host

Plugin snippets can be edited in the Playground, but native capabilities are executed locally with the Zolo CLI.

/ filter jk navigate Esc clear
Functions (13)
  1. RingBuffer.new Create a ring buffer with a fixed capacity
  2. push Add a value, overwriting oldest if full
  3. pop Remove and return the oldest value
  4. peek Read the oldest value without removing it
  5. peek_back Read the most recently pushed value
  6. get Read a value by index without removing it
  7. len Return the number of stored elements
  8. capacity Return the maximum capacity
  9. is_empty Check if the buffer is empty
  10. is_full Check if the buffer is at capacity
  11. contains Check if a value is present
  12. clear Remove all elements
  13. to_table Return all elements as an ordered table

From import to result

  1. 01Import only what you use
  2. 02Copy the smallest example
  3. 03Run it with the Zolo CLI

Create a ring buffer with a fixed capacity

Creates a new ring buffer with the given fixed capacity. Once full, pushing a new value silently overwrites the oldest element.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(4)
buf.push(10)
buf.push(20)
buf.push(30)
buf.push(40)
buf.push(50)  // overwrites 10
print(buf.len())  // 4

Add a value, overwriting oldest if full

Appends a value to the back of the buffer. If the buffer is full, the oldest (front) element is discarded to make room.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(3)
buf.push("a")
buf.push("b")
buf.push("c")
buf.push("d")  // "a" is dropped
print(buf.peek())  // b

Remove and return the oldest value

Removes and returns the oldest element (FIFO order). Returns nil if the buffer is empty.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(3)
buf.push(1)
buf.push(2)
print(buf.pop())  // 1
print(buf.pop())  // 2
print(buf.pop())  // nil

Read the oldest value without removing it

Returns the oldest element without removing it. Returns nil if empty.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(3)
buf.push("first")
print(buf.peek())  // first
print(buf.len())   // 1 (unchanged)

Read the most recently pushed value

Returns the most recently pushed element without removing it. Returns nil if empty. Useful for inspecting the latest value in a sensor stream.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(5)
buf.push(100)
buf.push(200)
buf.push(300)
print(buf.peek_back())  // 300

Read a value by index without removing it

Returns the element at the given zero-based index (oldest = 0). Returns nil if out of range. Does not remove the element.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(4)
buf.push("x")
buf.push("y")
buf.push("z")
print(buf.get(1))  // y

Return the number of stored elements

Returns the number of elements currently in the buffer.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(10)
buf.push(1)
buf.push(2)
print(buf.len())  // 2

Return the maximum capacity

Returns the fixed capacity the buffer was created with.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(8)
print(buf.capacity())  // 8

Check if the buffer is empty

Returns true if the buffer contains no elements.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(3)
print(buf.is_empty())  // true
buf.push(1)
print(buf.is_empty())  // false

Check if the buffer is at capacity

Returns true if the number of elements equals the capacity.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(2)
buf.push("a")
buf.push("b")
print(buf.is_full())  // true

Check if a value is present

Returns true if the buffer contains an element equal to value. Supports int, float, bool, string, and nil comparisons.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(5)
buf.push("apple")
buf.push("banana")
print(buf.contains("apple"))   // true
print(buf.contains("cherry"))  // false

Remove all elements

Removes all elements, resetting the buffer to empty while keeping its capacity.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(4)
buf.push(1)
buf.push(2)
buf.clear()
print(buf.len())  // 0

Return all elements as an ordered table

Returns all current elements as a 1-indexed table in FIFO order (oldest first). Does not modify the buffer.

use plugin ringbuffer::{RingBuffer}

let buf = RingBuffer.new(4)
buf.push("a")
buf.push("b")
buf.push("c")
let t = buf.to_table()
print(t[1])  // a
print(t[2])  // b

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

en