Skip to content

feat: add Extras.Queue, an immutable FIFO queue - #1

Merged
magnus-madsen merged 2 commits into
masterfrom
add-extras-queue
Sep 12, 2026
Merged

magnus-madsen merged 2 commits into
masterfrom
add-extras-queue

Conversation

@magnus-madsen

@magnus-madsen magnus-madsen commented Sep 12, 2026

Copy link
Copy Markdown
Member

Adds Extras.Queue, a persistent first-in, first-out queue — the first module in this library.

Why a queue

The standard library has MutDeque and MutPriorityQueue, and persistent Chain, Nec, and Nel, but no immutable queue. This fills that gap.

Representation

A pair of lists: a front list in queue order, and a back list in reverse queue order. Elements are dequeued from the front and enqueued onto the back; when the front is exhausted the back is reversed to become the new front. This gives enqueue, dequeue, and peek amortized constant time.

The private unit smart constructor maintains the invariant that the front list is empty only if the queue is empty, which lets isEmpty and peek be constant-time reads of the front.

Add `Extras.Queue`: a persistent first-in, first-out queue. The standard
library provides `MutDeque` and `MutPriorityQueue`, but no immutable queue.

The queue is represented as a pair of lists -- a front list in queue order
and a back list in reverse queue order -- maintaining the invariant that the
front is empty only if the queue is empty. This gives `enqueue`, `dequeue`,
and `peek` amortized constant time.

The interface is deliberately minimal: `empty`, `isEmpty`, `size`, `enqueue`,
`dequeue`, `peek`, `toList`, and `fromList`, plus `Eq` and `ToString`
instances. `Eq` compares queue order rather than representation, since the
same elements can be split differently across the front and back lists.

Includes 30 tests, three per operation.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V83KMjbi6D8deDr3zyiinK
@magnus-madsen magnus-madsen changed the title Add Extras.Queue, an immutable FIFO queue feat: add Extras.Queue, an immutable FIFO queue Sep 12, 2026
The `Queue` enum is declared in the body of `mod Extras.Queue`, so it is
already in scope both as a type and for its case constructor. The `use` was
redundant.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V83KMjbi6D8deDr3zyiinK
@magnus-madsen
magnus-madsen merged commit 223c4eb into master Sep 12, 2026
1 check passed
@magnus-madsen
magnus-madsen deleted the add-extras-queue branch September 12, 2026 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant