From 02745010e9f9bd3c0e40fd0dd52627e4e5c780d1 Mon Sep 17 00:00:00 2001 From: makki_d Date: Mon, 16 Sep 2024 03:23:32 +0900 Subject: [PATCH] implove iterator func --- iter.go | 27 +++++++++++++++++++++------ iter_bench_test.go | 4 +--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/iter.go b/iter.go index c616d33..4425614 100644 --- a/iter.go +++ b/iter.go @@ -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 diff --git a/iter_bench_test.go b/iter_bench_test.go index 863f10b..7b6f734 100644 --- a/iter_bench_test.go +++ b/iter_bench_test.go @@ -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++ {