diff options
author | Runxi Yu <me@runxiyu.org> | 2025-04-05 23:22:51 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-04-05 23:22:51 +0800 |
commit | 38b24afcb653da88765c99049f850c3ec7e234db (patch) | |
tree | 9fa48eab9bd4ddff5323571b5746a72371628996 /internal/misc/iter.go | |
parent | Remove unused types/functions (diff) | |
download | forge-38b24afcb653da88765c99049f850c3ec7e234db.tar.gz forge-38b24afcb653da88765c99049f850c3ec7e234db.tar.zst forge-38b24afcb653da88765c99049f850c3ec7e234db.zip |
misc: Move IterSeqLimit to misc
Diffstat (limited to 'internal/misc/iter.go')
-rw-r--r-- | internal/misc/iter.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/internal/misc/iter.go b/internal/misc/iter.go new file mode 100644 index 0000000..61a96f4 --- /dev/null +++ b/internal/misc/iter.go @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> + +package misc + +import "iter" + +// iterSeqLimit returns an iterator equivalent to the supplied one, but stops +// after n iterations. +func IterSeqLimit[T any](s iter.Seq[T], n uint) iter.Seq[T] { + return func(yield func(T) bool) { + var iterations uint + for v := range s { + if iterations > n-1 { + return + } + if !yield(v) { + return + } + iterations++ + } + } +} |