feat: add KeysSeq and ItemsSeq range-over-func iterators - #219
Open
ChrisJr404 wants to merge 1 commit into
Open
Conversation
Add lazy, range-over-func counterparts of Keys and Items for use with Go 1.23+ range-over-func. KeysSeq returns iter.Seq[K] and ItemsSeq returns iter.Seq2[K, *Item[K, V]]. Both visit unexpired items in the same order as Range and reuse it internally, so they share its locking and early-stop semantics and do not allocate an intermediate slice or map. Includes tests and a README example.
Coverage Report for CI Build 32181612747Coverage increased (+0.004%) to 99.745%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
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.
This adds the iterator functions from the v3.5 idea list in #212 ("Add iterator funcs for
Keys()etc"), following thestrings.FieldsSeqnaming convention referenced there.What
Two range-over-func iterators, the lazy counterparts of the existing
KeysandItemsmethods:KeysSeq() iter.Seq[K]— yields the key of each unexpired item.ItemsSeq() iter.Seq2[K, *Item[K, V]]— yields the key and item of each unexpired item.Notes
Range, so they inherit its exact behaviour: unexpired items only, most-to-least-recently-added order, the cache lock released while a value is yielded (so calling other cache methods from within the loop is safe), and early termination when the loopbreaks.Keys/Items, they do not allocate an intermediate slice or map.Seqsuffix (rather than shadowingKeys/Items) both because those names are taken and to match thestrings.FieldsSeqexample from the issue.go 1.25, soiteris available without any version bump.Tests cover full iteration, expired-item exclusion, ordering, early
break, empty caches, and calling cache methods during iteration (all with-race). README updated with a short example.go build,go vet, andgo test -race ./...all pass.Happy to adjust naming or split
ItemsSeqto yield values instead of items if you prefer a different shape.Refs #212