Skip to content

Commit 5351fab

Browse files
committed
Add TryCollect and Collect2 consumers and remove CollectErr
1 parent cc19074 commit 5351fab

File tree

6 files changed

+100
-47
lines changed

6 files changed

+100
-47
lines changed

it/enumerate_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package it_test
33
import (
44
"fmt"
55
"slices"
6+
"testing"
67

8+
"github.com/BooleanCat/go-functional/v2/internal/assert"
79
"github.com/BooleanCat/go-functional/v2/it"
810
)
911

@@ -17,3 +19,23 @@ func ExampleEnumerate() {
1719
// 1 2
1820
// 2 3
1921
}
22+
23+
func TestEnumerateYieldFalse(t *testing.T) {
24+
t.Parallel()
25+
26+
iterator := it.Enumerate(slices.Values([]int{1, 2, 3, 4, 5}))
27+
28+
var (
29+
index int
30+
number int
31+
)
32+
33+
iterator(func(i int, n int) bool {
34+
index = i
35+
number = n
36+
return false
37+
})
38+
39+
assert.Equal(t, index, 0)
40+
assert.Equal(t, number, 1)
41+
}

it/exhausted_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"fmt"
55
"maps"
66
"slices"
7-
"testing"
87

9-
"github.com/BooleanCat/go-functional/v2/internal/assert"
108
"github.com/BooleanCat/go-functional/v2/it"
119
)
1210

@@ -19,23 +17,3 @@ func ExampleExhausted2() {
1917
fmt.Println(len(maps.Collect(it.Exhausted2[int, string]())))
2018
// Output: 0
2119
}
22-
23-
func TestEnumerateYieldFalse(t *testing.T) {
24-
t.Parallel()
25-
26-
iterator := it.Enumerate(slices.Values([]int{1, 2, 3, 4, 5}))
27-
28-
var (
29-
index int
30-
number int
31-
)
32-
33-
iterator(func(i int, n int) bool {
34-
index = i
35-
number = n
36-
return false
37-
})
38-
39-
assert.Equal(t, index, 0)
40-
assert.Equal(t, number, 1)
41-
}

it/iter.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package it
22

33
import (
44
"cmp"
5-
"errors"
65
"iter"
76
)
87

@@ -127,20 +126,35 @@ func Find2[V, W any](iterator func(func(V, W) bool), pred func(V, W) bool) (V, W
127126
return zeroV, zeroW, false
128127
}
129128

130-
// CollectErr consumes an [iter.Seq2] where the right side yields errors and
131-
// returns a slice of values and all errors joined together.
132-
func CollectErr[V any](delegate func(func(V, error) bool)) ([]V, error) {
129+
// TryCollect consumes an [iter.Seq2] where the right side yields errors and
130+
// returns a slice of values and the first error encountered. Iteration stops
131+
// at the first error.
132+
func TryCollect[V any](iterator func(func(V, error) bool)) ([]V, error) {
133+
var values []V
134+
135+
for v, err := range iterator {
136+
if err != nil {
137+
return values, err
138+
}
139+
values = append(values, v)
140+
}
141+
142+
return values, nil
143+
}
144+
145+
// Collect2 consumes an [iter.Seq2] and returns two slices of values.
146+
func Collect2[V, W any](iterator func(func(V, W) bool)) ([]V, []W) {
133147
var (
134-
values []V
135-
errs []error
148+
lefts []V
149+
rights []W
136150
)
137151

138-
for v, err := range delegate {
139-
values = append(values, v)
140-
errs = append(errs, err)
152+
for v, w := range iterator {
153+
lefts = append(lefts, v)
154+
rights = append(rights, w)
141155
}
142156

143-
return values, errors.Join(errs...)
157+
return lefts, rights
144158
}
145159

146160
// Len consumes an [iter.Seq] and returns the number of values yielded.

it/iter_test.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestForEachEmpty(t *testing.T) {
3030
}
3131

3232
func ExampleForEach2() {
33-
it.ForEach2(it.Enumerate(slices.Values([]int{1, 2, 3})), func(index int, number int) {
33+
it.ForEach2(slices.All([]int{1, 2, 3}), func(index int, number int) {
3434
fmt.Println(index, number)
3535
})
3636
// Output:
@@ -59,7 +59,7 @@ func TestReduceEmpty(t *testing.T) {
5959
}
6060

6161
func ExampleReduce2() {
62-
fmt.Println(it.Reduce2(it.Enumerate(slices.Values([]int{1, 2, 3})), func(i, a, b int) int {
62+
fmt.Println(it.Reduce2(slices.All([]int{1, 2, 3}), func(i, a, b int) int {
6363
return i + 1
6464
}, 0))
6565

@@ -115,7 +115,7 @@ func ExampleFind_notFound() {
115115
}
116116

117117
func ExampleFind2() {
118-
index, value, ok := it.Find2(it.Enumerate(slices.Values([]int{1, 2, 3})), func(i, v int) bool {
118+
index, value, ok := it.Find2(slices.All([]int{1, 2, 3}), func(i, v int) bool {
119119
return i == 2
120120
})
121121
fmt.Println(index, value, ok)
@@ -124,24 +124,46 @@ func ExampleFind2() {
124124
}
125125

126126
func ExampleFind2_notFound() {
127-
index, value, ok := it.Find2(it.Enumerate(slices.Values([]int{1, 2, 3})), func(i, v int) bool {
127+
index, value, ok := it.Find2(slices.All([]int{1, 2, 3}), func(i, v int) bool {
128128
return i == 4
129129
})
130130

131131
fmt.Println(index, value, ok)
132132
// Output: 0 0 false
133133
}
134134

135-
func ExampleCollectErr() {
136-
data := strings.NewReader("one\ntwo\nthree\n")
137-
lines, err := it.CollectErr(it.LinesString(data))
135+
func ExampleCollect2() {
136+
indicies, values := it.Collect2(slices.All([]int{1, 2, 3}))
137+
fmt.Println(values)
138+
fmt.Println(indicies)
139+
140+
// Output:
141+
// [1 2 3]
142+
// [0 1 2]
143+
}
144+
145+
func ExampleTryCollect() {
146+
text := strings.NewReader("one\ntwo\nthree\n")
147+
148+
lines, err := it.TryCollect(it.LinesString(text))
138149
fmt.Println(err)
139150
fmt.Println(lines)
151+
140152
// Output:
141153
// <nil>
142154
// [one two three]
143155
}
144156

157+
func TestTryCollectError(t *testing.T) {
158+
t.Parallel()
159+
160+
text := new(failSecondTime)
161+
lines, err := it.TryCollect(it.LinesString(text))
162+
163+
assert.Equal(t, err.Error(), "read error")
164+
assert.SliceEqual(t, lines, []string{"o"})
165+
}
166+
145167
func ExampleLen() {
146168
fmt.Println(it.Len(slices.Values([]int{1, 2, 3})))
147169

@@ -155,7 +177,7 @@ func TestLenEmpty(t *testing.T) {
155177
}
156178

157179
func ExampleLen2() {
158-
fmt.Println(it.Len2(it.Enumerate(slices.Values([]int{1, 2, 3}))))
180+
fmt.Println(it.Len2(slices.All([]int{1, 2, 3})))
159181

160182
// Output: 3
161183
}

it/itx/iter.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,16 @@ func (iterator Iterator2[V, W]) Find(predicate func(V, W) bool) (V, W, bool) {
7474
return it.Find2(iterator, predicate)
7575
}
7676

77-
// CollectErr consumes an [Iterator2] where the right side yields errors and
78-
// returns a slice of values and all errors yielded joined together.
79-
func CollectErr[V any](iterator Iterator2[V, error]) ([]V, error) {
80-
return it.CollectErr(iter.Seq2[V, error](iterator))
77+
// TryCollect consumes an [iter.Seq2] where the right side yields errors and
78+
// returns a slice of values and the first error encountered. Iteration stops
79+
// at the first error.
80+
func TryCollect[V any](iterator func(func(V, error) bool)) ([]V, error) {
81+
return it.TryCollect(iterator)
82+
}
83+
84+
// Collect2 consumes an [iter.Seq2] and returns two slices of values.
85+
func (iterator Iterator2[V, W]) Collect() ([]V, []W) {
86+
return it.Collect2(iterator)
8187
}
8288

8389
// Len is a convenience method for chaining [it.Len] on [Iterator]s.

it/itx/iter_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,27 @@ func ExampleFromMap() {
7979
// Output: [2]
8080
}
8181

82-
func ExampleCollectErr() {
83-
data := strings.NewReader("one\ntwo\nthree\n")
84-
lines, err := itx.CollectErr(itx.LinesString(data))
82+
func ExampleTryCollect() {
83+
text := strings.NewReader("one\ntwo\nthree\n")
84+
lines, err := itx.TryCollect(itx.LinesString(text))
8585
fmt.Println(err)
8686
fmt.Println(lines)
87+
8788
// Output:
8889
// <nil>
8990
// [one two three]
9091
}
9192

93+
func ExampleIterator2_Collect() {
94+
indicies, values := itx.FromSlice([]int{1, 2, 3}).Enumerate().Collect()
95+
fmt.Println(values)
96+
fmt.Println(indicies)
97+
98+
// Output:
99+
// [1 2 3]
100+
// [0 1 2]
101+
}
102+
92103
func ExampleIterator_Len() {
93104
fmt.Println(itx.FromSlice([]int{1, 2, 3}).Len())
94105
// Output: 3

0 commit comments

Comments
 (0)