aboutsummaryrefslogtreecommitdiff
path: root/iter.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-03-31 16:59:18 +0800
committerRunxi Yu <me@runxiyu.org>2025-03-31 16:59:18 +0800
commit655b6b211ae6df0186abd740f248939f7ddeaec1 (patch)
treeec5cdbbc52222f62c8fbb0bcf2a1aa7a9f6eb8b6 /iter.go
parentCorrect table headers in MR indices (diff)
downloadforge-655b6b211ae6df0186abd740f248939f7ddeaec1.tar.gz
forge-655b6b211ae6df0186abd740f248939f7ddeaec1.tar.zst
forge-655b6b211ae6df0186abd740f248939f7ddeaec1.zip
Add descriptive comments to most Go functions
Diffstat (limited to '')
-rw-r--r--iter.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/iter.go b/iter.go
new file mode 100644
index 0000000..d4c7175
--- /dev/null
+++ b/iter.go
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
+
+package main
+
+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++
+ }
+ }
+}