Stream: beginners

Topic: circular buffer?


view this post on Zulip Becker A. (Dec 26 2023 at 19:37):

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!

view this post on Zulip Brendan Hansknecht (Dec 26 2023 at 19:53):

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

view this post on Zulip Brendan Hansknecht (Dec 26 2023 at 19:54):

The main annoyance is that users will be required to specify an empty element.

view this post on Zulip Brendan Hansknecht (Dec 26 2023 at 19:55):

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)

view this post on Zulip Brendan Hansknecht (Dec 26 2023 at 19:55):

Roc will not give you access to something uninitialized.

view this post on Zulip Becker A. (Dec 26 2023 at 20:29):

Yeah exactly, I wasn’t sure how best to deal with uninitialized memory chunks in Roc

view this post on Zulip Becker A. (Dec 26 2023 at 20:30):

Is there any way to implement this as a Zig/Rust extension, without making it into a whole platform or anything?

view this post on Zulip Brendan Hansknecht (Dec 26 2023 at 20:34):

Not really. Some context in #ideas > FFI

view this post on Zulip Brendan Hansknecht (Dec 26 2023 at 20:37):

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

view this post on Zulip Becker A. (Dec 26 2023 at 20:54):

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?

view this post on Zulip Brendan Hansknecht (Dec 26 2023 at 21:03):

yes (that or reset them to the default/empty value if you require one)


Last updated: Jul 06 2025 at 12:14 UTC