Skip to content

Commit

Permalink
feat: improve iterutil.Take function
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerwill90 committed Nov 30, 2024
1 parent e88a4b3 commit bb977b8
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions internal/iterutil/iterutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ func Len2[K, V any](seq iter.Seq2[K, V]) int {

func Take[I constraints.Integer, E any](seq iter.Seq[E], count I) iter.Seq[E] {
return func(yield func(E) bool) {
count += 1
for e := range seq {
count--
if count <= 0 || !yield(e) {
return
if count > 0 {
if !yield(e) {
return
}
count--
continue
}
return
}
}
}
Expand Down

0 comments on commit bb977b8

Please sign in to comment.