Skip to content

Commit a984430

Browse files
committed
Simplify iterator chaining for Collect
1 parent f873e83 commit a984430

25 files changed

+106
-221
lines changed

iter/chain.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import "github.com/BooleanCat/go-functional/option"
44

55
// ChainIter iterator, see [Chain].
66
type ChainIter[T any] struct {
7+
BaseIter[T]
78
iterators []Iterator[T]
89
iteratorIndex int
910
}
1011

1112
// Chain instantiates a [*ChainIter] that will yield all items in the provided
1213
// iterators to exhaustion first to last.
1314
func Chain[T any](iterators ...Iterator[T]) *ChainIter[T] {
14-
return &ChainIter[T]{iterators, 0}
15+
iter := &ChainIter[T]{iterators: iterators}
16+
iter.BaseIter = BaseIter[T]{iter}
17+
return iter
1518
}
1619

1720
// Next implements the [Iterator] interface.
@@ -32,12 +35,6 @@ func (iter *ChainIter[T]) Next() option.Option[T] {
3235

3336
var _ Iterator[struct{}] = new(ChainIter[struct{}])
3437

35-
// Collect is a convenience method for [Collect], providing this iterator as an
36-
// argument.
37-
func (iter *ChainIter[T]) Collect() []T {
38-
return Collect[T](iter)
39-
}
40-
4138
// ForEach is a convenience method for [ForEach], providing this iterator as an
4239
// argument.
4340
func (iter *ChainIter[T]) ForEach(callback func(T)) {

iter/chain_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ func TestChainExhausted(t *testing.T) {
4646
assert.Equal(t, delegate2.NextCallCount(), 1)
4747
}
4848

49-
func TestChainCollect(t *testing.T) {
50-
numbers := iter.Chain[int](iter.Lift([]int{1, 2}), iter.Lift([]int{3, 4})).Collect()
51-
assert.SliceEqual(t, numbers, []int{1, 2, 3, 4})
52-
}
53-
5449
func TestChainForEach(t *testing.T) {
5550
total := 0
5651

iter/channel.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import "github.com/BooleanCat/go-functional/option"
44

55
// ChannelIter iterator, see [FromChannel].
66
type ChannelIter[T any] struct {
7+
BaseIter[T]
78
item chan T
89
}
910

1011
// FromChannel instantiates a [*ChannelIter] that will yield each value from
1112
// the provided channel.
1213
func FromChannel[T any](ch chan T) *ChannelIter[T] {
13-
return &ChannelIter[T]{ch}
14+
iter := &ChannelIter[T]{item: ch}
15+
iter.BaseIter = BaseIter[T]{iter}
16+
return iter
1417
}
1518

1619
// Next implements the [Iterator] interface.
@@ -26,12 +29,6 @@ func (iter *ChannelIter[T]) Next() option.Option[T] {
2629

2730
var _ Iterator[struct{}] = new(ChannelIter[struct{}])
2831

29-
// Collect is a convenience method for [Collect], providing this iterator as an
30-
// argument.
31-
func (iter *ChannelIter[T]) Collect() []T {
32-
return Collect[T](iter)
33-
}
34-
3532
// ForEach is a convenience method for [ForEach], providing this iterator as an
3633
// argument.
3734
func (iter *ChannelIter[T]) ForEach(callback func(T)) {

iter/channel_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,6 @@ func TestFromChannelEmpty(t *testing.T) {
4646
assert.True(t, iter.FromChannel(ch).Next().IsNone())
4747
}
4848

49-
func TestFromChannelCollect(t *testing.T) {
50-
ch := make(chan int)
51-
52-
go func() {
53-
ch <- 1
54-
ch <- 2
55-
close(ch)
56-
}()
57-
58-
numbers := iter.FromChannel(ch).Collect()
59-
assert.SliceEqual(t, numbers, []int{1, 2})
60-
}
61-
6249
func TestFromChannelForEach(t *testing.T) {
6350
ch := make(chan int)
6451

iter/counter.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import "github.com/BooleanCat/go-functional/option"
44

55
// CountIter iterator, see [Count].
66
type CountIter struct {
7+
BaseIter[int]
78
index int
89
}
910

1011
// Count instantiates a [*CountIter] that will iterate over 0 and the
1112
// natural numbers. Count is functionally "unlimited" although it does not
1213
// protect against the integer limit.
1314
func Count() *CountIter {
14-
return new(CountIter)
15+
iter := new(CountIter)
16+
iter.BaseIter = BaseIter[int]{iter}
17+
return iter
1518
}
1619

1720
// Next implements the [Iterator] interface.

iter/cycle.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "github.com/BooleanCat/go-functional/option"
44

55
// CycleIter iterator, see [Cycle].
66
type CycleIter[T any] struct {
7+
BaseIter[T]
78
iter Iterator[T]
89
items []T
910
index int
@@ -20,7 +21,9 @@ type CycleIter[T any] struct {
2021
// is exhausted before the first call to Next() in which case this iterator
2122
// will always yield None.
2223
func Cycle[T any](iter Iterator[T]) *CycleIter[T] {
23-
return &CycleIter[T]{iter, make([]T, 0), 0}
24+
iterator := &CycleIter[T]{iter: iter, items: make([]T, 0)}
25+
iterator.BaseIter = BaseIter[T]{iterator}
26+
return iterator
2427
}
2528

2629
// Next implements the [Iterator] interface.

iter/drop.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "github.com/BooleanCat/go-functional/option"
44

55
// DropIter iterator, see [Drop].
66
type DropIter[T any] struct {
7+
BaseIter[T]
78
iter Iterator[T]
89
count uint
910
dropped bool
@@ -13,7 +14,9 @@ type DropIter[T any] struct {
1314
// Drop instantiates a [*DropIter] that will skip the number of items of its
1415
// wrapped iterator by the provided count.
1516
func Drop[T any](iter Iterator[T], count uint) *DropIter[T] {
16-
return &DropIter[T]{iter, count, false, false}
17+
iterator := &DropIter[T]{iter: iter, count: count}
18+
iterator.BaseIter = BaseIter[T]{iterator}
19+
return iterator
1720
}
1821

1922
// Next implements the [Iterator] interface.
@@ -46,12 +49,6 @@ func (iter *DropIter[T]) delegateNext() option.Option[T] {
4649

4750
var _ Iterator[struct{}] = new(DropIter[struct{}])
4851

49-
// Collect is a convenience method for [Collect], providing this iterator as
50-
// an argument.
51-
func (iter *DropIter[T]) Collect() []T {
52-
return Collect[T](iter)
53-
}
54-
5552
// ForEach is a convenience method for [ForEach], providing this iterator as an
5653
// argument.
5754
func (iter *DropIter[T]) ForEach(callback func(T)) {

iter/drop_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ func TestDropExhaustedLater(t *testing.T) {
5050
assert.Equal(t, delegate.NextCallCount(), 3)
5151
}
5252

53-
func TestDropCollect(t *testing.T) {
54-
numbers := iter.Drop[int](iter.Lift([]int{1, 2, 3}), 2).Collect()
55-
assert.SliceEqual(t, numbers, []int{3})
56-
}
57-
5853
func TestDropForEach(t *testing.T) {
5954
total := 0
6055

iter/exhausted.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package iter
33
import "github.com/BooleanCat/go-functional/option"
44

55
// ExhaustedIter iterator, see [Exhausted].
6-
type ExhaustedIter[T any] struct{}
6+
type ExhaustedIter[T any] struct {
7+
BaseIter[T]
8+
}
79

810
// Exhausted instantiates an [*ExhaustedIter] that will immediately be
911
// exhausted (Next will always return a None variant).
1012
func Exhausted[T any]() *ExhaustedIter[T] {
11-
return new(ExhaustedIter[T])
13+
iter := new(ExhaustedIter[T])
14+
iter.BaseIter = BaseIter[T]{iter}
15+
return iter
1216
}
1317

1418
// Next implements the [Iterator] interface.
@@ -18,12 +22,6 @@ func (iter *ExhaustedIter[T]) Next() option.Option[T] {
1822

1923
var _ Iterator[struct{}] = new(ExhaustedIter[struct{}])
2024

21-
// Collect is a convenience method for [Collect], providing this iterator as
22-
// an argument.
23-
func (iter *ExhaustedIter[T]) Collect() []T {
24-
return Collect[T](iter)
25-
}
26-
2725
// Find is a convenience method for [Find], providing this iterator as an
2826
// argument.
2927
func (iter *ExhaustedIter[T]) Find(predicate func(T) bool) option.Option[T] {

iter/exhausted_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ func TestExhausted(t *testing.T) {
1717
assert.True(t, iter.Exhausted[int]().Next().IsNone())
1818
}
1919

20-
func TestExhaustedCollect(t *testing.T) {
21-
assert.Empty[int](t, iter.Exhausted[int]().Collect())
22-
}
23-
2420
func TestExhaustedForEach(t *testing.T) {
2521
total := 0
2622

0 commit comments

Comments
 (0)