Skip to content

Commit

Permalink
implove iterator func
Browse files Browse the repository at this point in the history
  • Loading branch information
makiuchi-d committed Sep 15, 2024
1 parent ba82e40 commit 0274501
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
27 changes: 21 additions & 6 deletions iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,33 @@ package linq

import "iter"

// All returns an iterator function
func (e Enumerable[T]) All() iter.Seq2[T, error] {
return iterAll(e())
}
er := e()
return func(yield func(T, error) bool) {
for {
v, err := er.Next()
if err != nil {
if isEOC(err) {
return
}
yield(v, err)
return
}

func (e OrderedEnumerable[T]) All() iter.Seq2[T, error] {
return iterAll(e())
if !yield(v, nil) {
return
}
}
}
}

func iterAll[T any, E Enumerator[T]](e E) iter.Seq2[T, error] {
// All returns an iterator function
func (e OrderedEnumerable[T]) All() iter.Seq2[T, error] {
er := e()
return func(yield func(T, error) bool) {
for {
v, err := e.Next()
v, err := er.Next()
if err != nil {
if isEOC(err) {
return
Expand Down
4 changes: 1 addition & 3 deletions iter_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"github.com/makiuchi-d/linq/v2"
)

var src = linq.Where(
linq.Range(0, 10000),
func(v int) (bool, error) { return v%2 == 0, nil })
var src = linq.Range(0, 100000)

func BenchmarkFor(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit 0274501

Please sign in to comment.