Skip to content

feat: add KeysSeq and ItemsSeq range-over-func iterators - #219

Open
ChrisJr404 wants to merge 1 commit into
jellydator:v3from
ChrisJr404:feat-seq-iterators
Open

feat: add KeysSeq and ItemsSeq range-over-func iterators#219
ChrisJr404 wants to merge 1 commit into
jellydator:v3from
ChrisJr404:feat-seq-iterators

Conversation

@ChrisJr404

Copy link
Copy Markdown

This adds the iterator functions from the v3.5 idea list in #212 ("Add iterator funcs for Keys() etc"), following the strings.FieldsSeq naming convention referenced there.

What

Two range-over-func iterators, the lazy counterparts of the existing Keys and Items methods:

  • 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.
for key := range cache.KeysSeq() {
    // ...
}

for key, item := range cache.ItemsSeq() {
    // ...
}

Notes

  • Both are implemented on top of 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 loop breaks.
  • Unlike Keys/Items, they do not allocate an intermediate slice or map.
  • I kept the Seq suffix (rather than shadowing Keys/Items) both because those names are taken and to match the strings.FieldsSeq example from the issue.
  • The module is already on go 1.25, so iter is 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, and go test -race ./... all pass.

Happy to adjust naming or split ItemsSeq to yield values instead of items if you prefer a different shape.

Refs #212

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.
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 32181612747

Coverage increased (+0.004%) to 99.745%

Details

  • Coverage increased (+0.004%) from the base build.
  • Patch coverage: 10 of 10 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 783
Covered Lines: 781
Line Coverage: 99.74%
Coverage Strength: 26.52 hits per line

💛 - Coveralls

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.

2 participants