Skip to content

Commit 09a2279

Browse files
committed
use for loops in iterx
1 parent 7102ee9 commit 09a2279

File tree

5 files changed

+50
-80
lines changed

5 files changed

+50
-80
lines changed

internal/ext/iterx/consume.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,32 @@ import (
2626
// If p is nil, it is treated as func(_ T) bool { return true }.
2727
func Count[T any](seq iter.Seq[T]) int {
2828
var total int
29-
seq(func(_ T) bool {
29+
for range seq {
3030
total++
31-
return true
32-
})
31+
}
3332
return total
3433
}
3534

3635
// Join is like [strings.Join], but works on an iterator. Elements are
3736
// stringified as if by [fmt.Print].
3837
func Join[T any](seq iter.Seq[T], sep string) string {
3938
var out strings.Builder
40-
first := true
41-
seq(func(v T) bool {
42-
if !first {
39+
for i, v := range Enumerate(seq) {
40+
if i > 0 {
4341
out.WriteString(sep)
4442
}
45-
first = false
46-
4743
fmt.Fprint(&out, v)
48-
return true
49-
})
44+
}
5045
return out.String()
5146
}
5247

5348
// Every returns whether every element of an iterator satisfies the given
5449
// predicate. Returns true if seq yields no values.
5550
func Every[T any](seq iter.Seq[T], p func(T) bool) bool {
56-
all := true
57-
seq(func(v T) bool {
58-
all = p(v)
59-
return all
60-
})
61-
return all
51+
for v := range seq {
52+
if !p(v) {
53+
return false
54+
}
55+
}
56+
return true
6257
}

internal/ext/iterx/filtermap.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ func Filter[T any](seq iter.Seq[T], p func(T) bool) iter.Seq[T] {
3434
// FilterMap combines the operations of [Map] and [Filter].
3535
func FilterMap[T, U any](seq iter.Seq[T], f func(T) (U, bool)) iter.Seq[U] {
3636
return func(yield func(U) bool) {
37-
seq(func(v T) bool {
38-
v2, ok := f(v)
39-
return !ok || yield(v2)
40-
})
37+
for v := range seq {
38+
if v2, ok := f(v); ok && !yield(v2) {
39+
return
40+
}
41+
}
4142
}
4243
}
4344

internal/ext/iterx/get.go

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,23 @@ import (
2020

2121
// First retrieves the first element of an iterator.
2222
func First[T any](seq iter.Seq[T]) (v T, ok bool) {
23-
seq(func(x T) bool {
24-
v = x
25-
ok = true
26-
return false
27-
})
28-
return v, ok
23+
for x := range seq {
24+
return x, true
25+
}
26+
return v, false
2927
}
3028

3129
// OnlyOne retrieves the only element of an iterator.
3230
func OnlyOne[T any](seq iter.Seq[T]) (v T, ok bool) {
33-
var found T
34-
seq(func(x T) bool {
35-
if !ok {
36-
found = x
31+
for i, x := range Enumerate(seq) {
32+
if i > 0 {
33+
var z T
34+
// Ensure we return the zero value if there is more
35+
// than one element.
36+
return z, false
3737
}
38-
ok = !ok
39-
return ok
40-
})
41-
if ok {
42-
// Ensure we return the zero value if there is more
43-
// than one element.
44-
v = found
38+
v = x
39+
ok = true
4540
}
4641
return v, ok
4742
}
@@ -51,43 +46,27 @@ func OnlyOne[T any](seq iter.Seq[T]) (v T, ok bool) {
5146
// Returns the value and the index at which it was found, or -1 if it wasn't
5247
// found.
5348
func Find[T any](seq iter.Seq[T], p func(T) bool) (int, T) {
54-
var v T
55-
var idx int
56-
var found bool
57-
seq(func(x T) bool {
49+
for i, x := range Enumerate(seq) {
5850
if p(x) {
59-
v = x
60-
found = true
61-
return false
51+
return i, x
6252
}
63-
idx++
64-
return true
65-
})
66-
if !found {
67-
idx = -1
6853
}
69-
return idx, v
54+
var z T
55+
return -1, z
7056
}
7157

7258
// Find2 is like [Find] but for two-element iterators.
7359
func Find2[T, U any](seq iter.Seq2[T, U], p func(T, U) bool) (int, T, U) {
74-
var v1 T
75-
var v2 U
76-
var idx int
77-
var found bool
78-
seq(func(x1 T, x2 U) bool {
60+
var i int
61+
for x1, x2 := range seq {
7962
if p(x1, x2) {
80-
v1, v2 = x1, x2
81-
found = true
82-
return false
63+
return i, x1, x2
8364
}
84-
idx++
85-
return true
86-
})
87-
if !found {
88-
idx = -1
65+
i++
8966
}
90-
return idx, v1, v2
67+
var z1 T
68+
var z2 U
69+
return -1, z1, z2
9170
}
9271

9372
// Index returns the index of the first element of seq that satisfies p.

internal/ext/iterx/iterx.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ import (
2323
// Take returns an iterator over the first n elements of a sequence.
2424
func Take[T any](seq iter.Seq[T], n int) iter.Seq[T] {
2525
return func(yield func(T) bool) {
26-
seq(func(v T) bool {
27-
if n > 0 && yield(v) {
28-
n--
29-
return true
26+
for v := range seq {
27+
if n == 0 || !yield(v) {
28+
return
3029
}
31-
return false
32-
})
30+
n--
31+
}
3332
}
3433
}
3534

@@ -56,15 +55,12 @@ func Strings[T any](seq iter.Seq[T]) iter.Seq[string] {
5655
// Chain returns an iterator that calls a sequence of iterators in sequence.
5756
func Chain[T any](seqs ...iter.Seq[T]) iter.Seq[T] {
5857
return func(yield func(T) bool) {
59-
var done bool
6058
for _, seq := range seqs {
61-
if done {
62-
return
59+
for v := range seq {
60+
if !yield(v) {
61+
return
62+
}
6363
}
64-
seq(func(v T) bool {
65-
done = !yield(v)
66-
return !done
67-
})
6864
}
6965
}
7066
}

internal/ext/mapsx/collect.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ func CollectSet[K comparable](seq iter.Seq[K]) map[K]struct{} {
2525
// InsertKeys is like [maps.Insert], but it implicitly fills in each map value
2626
// with the zero value.
2727
func InsertKeys[M ~map[K]V, K comparable, V any](m M, seq iter.Seq[K]) M {
28-
seq(func(k K) bool {
28+
for k := range seq {
2929
var zero V
3030
m[k] = zero
31-
return true
32-
})
31+
}
3332
return m
3433
}

0 commit comments

Comments
 (0)