feat: add Extras.Queue, an immutable FIFO queue - #1
Merged
Merged
Conversation
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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
Extras.Queue, a persistent first-in, first-out queue — the first module in this library.Why a queue
The standard library has
MutDequeandMutPriorityQueue, and persistentChain,Nec, andNel, 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, andpeekamortized constant time.The private
unitsmart constructor maintains the invariant that the front list is empty only if the queue is empty, which letsisEmptyandpeekbe constant-time reads of the front.