Skip to content

Commit 0563dae

Browse files
committed
Add Drain consumer
1 parent 50bcb91 commit 0563dae

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,38 @@ for number := range channel {
287287
> Unlike most consumers, the iterator is not immediately consumed by ToChannel. Instead is it
288288
> consumed as values are pulled from the channel.
289289
290+
### Drain
291+
292+
Drain consumes an iterator's values and drops them.
293+
294+
```go
295+
printValue := func(n int) int {
296+
fmt.Println(n)
297+
return n
298+
}
299+
300+
it.Drain(it.Map(slices.Values([]int{1, 2, 3}), printValue))
301+
302+
// Chainable
303+
itx.From(it.Map(slices.Values([]int{1, 2, 3}), printValue)).Drain()
304+
305+
// Drain an iter.Seq2
306+
printValue2 := func(i, n int) (int, int) {
307+
fmt.Println(n)
308+
return i, n
309+
}
310+
311+
it.Drain2(it.Map2(slices.All([]int{1, 2, 3}), printValue2))
312+
313+
// As above, but chainable
314+
itx.From2(it.Map2(slices.All([]int{1, 2, 3}), printValue2)).Drain()
315+
```
316+
317+
<!-- prettier-ignore -->
318+
> [!TIP]
319+
> Use Drain to consume an iterator to invoke any side effects when you don't need to collect the
320+
> values.
321+
290322
## Iterators
291323

292324
This library contains two kinds of iterators in the `it` and `itx` packages. In most cases you'll

it/iter.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,21 @@ func Contains[V comparable](iterator func(func(V) bool), v V) bool {
192192

193193
return false
194194
}
195+
196+
// Drain consumes an [iter.Seq] completely, dropping all values.
197+
//
198+
// You may wish to use this to execute side effects without needing to collect
199+
// values.
200+
func Drain[V any](iterator func(func(V) bool)) {
201+
for range iterator {
202+
}
203+
}
204+
205+
// Drain2 consumes an [iter.Seq2] completely, dropping all values.
206+
//
207+
// You may wish to use this to execute side effects without needing to collect
208+
// values.
209+
func Drain2[V, W any](iterator func(func(V, W) bool)) {
210+
for range iterator {
211+
}
212+
}

it/iter_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,31 @@ func ExampleContains() {
197197
func TestContainsFalse(t *testing.T) {
198198
assert.False(t, it.Contains(slices.Values([]int{1, 2, 3}), 4))
199199
}
200+
201+
func ExampleDrain() {
202+
numbers := it.Map(slices.Values([]int{1, 2, 3}), func(n int) int {
203+
fmt.Println(n)
204+
return n
205+
})
206+
207+
it.Drain(numbers)
208+
209+
// Output:
210+
// 1
211+
// 2
212+
// 3
213+
}
214+
215+
func ExampleDrain2() {
216+
numbers := it.Map2(slices.All([]int{1, 2, 3}), func(i, n int) (int, int) {
217+
fmt.Println(n)
218+
return i, n
219+
})
220+
221+
it.Drain2(numbers)
222+
223+
// Output:
224+
// 1
225+
// 2
226+
// 3
227+
}

it/itx/iter.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,13 @@ func (iterator Iterator[V]) Len() int {
8888
func (iterator Iterator2[V, W]) Len() int {
8989
return it.Len2(iterator)
9090
}
91+
92+
// Drain is a convenience method for chaining [it.Drain] on [Iterator]s.
93+
func (iterator Iterator[V]) Drain() {
94+
it.Drain(iterator)
95+
}
96+
97+
// Drain2 is a convenience method for chaining [it.Drain2] on [Iterator2]s.
98+
func (iterator Iterator2[V, W]) Drain() {
99+
it.Drain2(iterator)
100+
}

it/itx/iter_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"maps"
66
"slices"
77

8+
"github.com/BooleanCat/go-functional/v2/it"
89
"github.com/BooleanCat/go-functional/v2/it/itx"
910
)
1011

@@ -97,3 +98,27 @@ func ExampleIterator2_Len() {
9798
fmt.Println(itx.FromSlice([]int{1, 2, 3}).Enumerate().Len())
9899
// Output: 3
99100
}
101+
102+
func ExampleIterator_Drain() {
103+
itx.From(it.Map(slices.Values([]int{1, 2, 3}), func(n int) int {
104+
fmt.Println(n)
105+
return n
106+
})).Drain()
107+
108+
// Output:
109+
// 1
110+
// 2
111+
// 3
112+
}
113+
114+
func ExampleIterator2_Drain() {
115+
itx.From2(it.Map2(slices.All([]int{1, 2, 3}), func(i, n int) (int, int) {
116+
fmt.Println(n)
117+
return i, n
118+
})).Drain()
119+
120+
// Output:
121+
// 1
122+
// 2
123+
// 3
124+
}

0 commit comments

Comments
 (0)