Hi all! How do I make something like a circular buffer in Roc? specifically, I mean a data structure with amortized-constant-time push/pop operations at both the front and back, based on a dynamically allocated contiguous array?
Afaik, to do it properly, it requires low-level twiddling with allocation beyond what Roc itself exposes. but I’m still new to Roc so I thought I’d reach out. Thanks!
Should be doable in roc built off of a List. Generate the list with a specific capacity and then use the start and end index into the buffer
The main annoyance is that users will be required to specify an empty element.
Cause you will generate a list of the max capacity of the circular buffer and that requires it being filled in with some sort of element. (That or you wrap all list elements on essentially a maybe type)
Roc will not give you access to something uninitialized.
Yeah exactly, I wasn’t sure how best to deal with uninitialized memory chunks in Roc
Is there any way to implement this as a Zig/Rust extension, without making it into a whole platform or anything?
Not really. Some context in #ideas > FFI
I guess you could also start with an empty list and just correctly cap it when you hit the max circular buffer size. So to begin with, the list would slowly initialize elements. That would enable building the circular buffer without the need for a default/empty element to initialize the list
Hmm so if you pop values, you actually just move the front index up and leave the “popped” value in the dead section of the list?
yes (that or reset them to the default/empty value if you require one)
Last updated: Jul 06 2025 at 12:14 UTC