Skip to content

Instantly share code, notes, and snippets.

@wjkoh
Created March 20, 2025 03:18
Show Gist options
  • Save wjkoh/30298bab14a5ee1a6a64e5e5bc4c324c to your computer and use it in GitHub Desktop.
Save wjkoh/30298bab14a5ee1a6a64e5e5bc4c324c to your computer and use it in GitHub Desktop.
Go: Limit the size of an iterator; Limit(n) or Take(n)
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