Created
March 20, 2025 03:18
-
-
Save wjkoh/30298bab14a5ee1a6a64e5e5bc4c324c to your computer and use it in GitHub Desktop.
Go: Limit the size of an iterator; Limit(n) or Take(n)
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
func Limit[E any](seq iter.Seq[E], limit int) iter.Seq[E] { | |
return func(yield func(E) bool) { | |
var count int | |
for e := range seq { | |
if count >= limit { | |
return | |
} | |
if !yield(e) { | |
return | |
} | |
count++ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment