Skip to content

Commit 2ef6280

Browse files
committed
Simplify iterator chaining for Find, Drop, Take
1 parent 0887257 commit 2ef6280

28 files changed

+36
-698
lines changed

iter/chain.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,3 @@ func (iter *ChainIter[T]) Next() option.Option[T] {
3434
}
3535

3636
var _ Iterator[struct{}] = new(ChainIter[struct{}])
37-
38-
// ForEach is a convenience method for [Find], providing this iterator as an
39-
// argument.
40-
func (iter *ChainIter[T]) Find(predicate func(T) bool) option.Option[T] {
41-
return Find[T](iter, predicate)
42-
}
43-
44-
// Drop is a convenience method for [Drop], providing this iterator as an
45-
// argument.
46-
func (iter *ChainIter[T]) Drop(n uint) *DropIter[T] {
47-
return Drop[T](iter, n)
48-
}
49-
50-
// Take is a convenience method for [Take], providing this iterator as an
51-
// argument.
52-
func (iter *ChainIter[T]) Take(n uint) *TakeIter[T] {
53-
return Take[T](iter, n)
54-
}

iter/chain_test.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/BooleanCat/go-functional/internal/assert"
88
"github.com/BooleanCat/go-functional/internal/fakes"
99
"github.com/BooleanCat/go-functional/iter"
10-
"github.com/BooleanCat/go-functional/option"
1110
)
1211

1312
func ExampleChain() {
@@ -45,21 +44,3 @@ func TestChainExhausted(t *testing.T) {
4544
assert.Equal(t, delegate1.NextCallCount(), 1)
4645
assert.Equal(t, delegate2.NextCallCount(), 1)
4746
}
48-
49-
func TestChainFind(t *testing.T) {
50-
number := iter.Chain[int](iter.Lift([]int{1, 2}), iter.Lift([]int{3, 4})).Find(func(number int) bool {
51-
return number == 3
52-
})
53-
54-
assert.Equal(t, number, option.Some(3))
55-
}
56-
57-
func TestChainDrop(t *testing.T) {
58-
numbers := iter.Chain[int](iter.Lift([]int{1, 2}), iter.Lift([]int{3, 4})).Drop(1).Collect()
59-
assert.SliceEqual(t, numbers, []int{2, 3, 4})
60-
}
61-
62-
func TestChainTake(t *testing.T) {
63-
numbers := iter.Chain[int](iter.Lift([]int{1, 2}), iter.Lift([]int{3, 4})).Take(3).Collect()
64-
assert.SliceEqual(t, numbers, []int{1, 2, 3})
65-
}

iter/channel.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,3 @@ func (iter *ChannelIter[T]) Next() option.Option[T] {
2828
}
2929

3030
var _ Iterator[struct{}] = new(ChannelIter[struct{}])
31-
32-
// Find is a convenience method for [Find], providing this iterator as an
33-
// argument.
34-
func (iter *ChannelIter[T]) Find(predicate func(T) bool) option.Option[T] {
35-
return Find[T](iter, predicate)
36-
}
37-
38-
// Drop is a convenience method for [Drop], providing this iterator as an
39-
// argument.
40-
func (iter *ChannelIter[T]) Drop(n uint) *DropIter[T] {
41-
return Drop[T](iter, n)
42-
}
43-
44-
// Take is a convenience method for [Take], providing this iterator as an
45-
// argument.
46-
func (iter *ChannelIter[T]) Take(n uint) *TakeIter[T] {
47-
return Take[T](iter, n)
48-
}

iter/channel_test.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/BooleanCat/go-functional/internal/assert"
88
"github.com/BooleanCat/go-functional/iter"
9-
"github.com/BooleanCat/go-functional/option"
109
)
1110

1211
func ExampleFromChannel() {
@@ -45,53 +44,3 @@ func TestFromChannelEmpty(t *testing.T) {
4544
close(ch)
4645
assert.True(t, iter.FromChannel(ch).Next().IsNone())
4746
}
48-
49-
func TestFromChannelFind(t *testing.T) {
50-
ch := make(chan int)
51-
52-
go func() {
53-
defer close(ch)
54-
ch <- 1
55-
ch <- 2
56-
ch <- 3
57-
}()
58-
59-
numbers := iter.FromChannel(ch)
60-
defer numbers.Collect()
61-
62-
number := numbers.Find(func(number int) bool {
63-
return number == 2
64-
})
65-
66-
assert.Equal(t, number, option.Some(2))
67-
}
68-
69-
func TestFromChannelDrop(t *testing.T) {
70-
ch := make(chan int)
71-
72-
go func() {
73-
ch <- 1
74-
ch <- 2
75-
ch <- 3
76-
close(ch)
77-
}()
78-
79-
numbers := iter.FromChannel(ch).Drop(1).Collect()
80-
assert.SliceEqual(t, numbers, []int{2, 3})
81-
}
82-
83-
func TestFromChannelTake(t *testing.T) {
84-
ch := make(chan int)
85-
86-
go func() {
87-
defer close(ch)
88-
ch <- 1
89-
ch <- 2
90-
ch <- 3
91-
}()
92-
93-
iter := iter.FromChannel(ch)
94-
numbers := iter.Take(2).Collect()
95-
assert.SliceEqual(t, numbers, []int{1, 2})
96-
iter.Collect() // To close the channel
97-
}

iter/counter.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,3 @@ func (c *CountIter) Next() option.Option[int] {
2424
}
2525

2626
var _ Iterator[int] = new(CountIter)
27-
28-
// Find is a convenience method for [Find], providing this iterator as an
29-
// argument.
30-
func (iter *CountIter) Find(predicate func(int) bool) option.Option[int] {
31-
return Find[int](iter, predicate)
32-
}
33-
34-
// Drop is a convenience method for [Drop], providing this iterator as an
35-
// argument.
36-
func (c *CountIter) Drop(n uint) *DropIter[int] {
37-
return Drop[int](c, n)
38-
}
39-
40-
// Take is a convenience method for [Take], providing this iterator as an
41-
// argument.
42-
func (iter *CountIter) Take(n uint) *TakeIter[int] {
43-
return Take[int](iter, n)
44-
}

iter/counter_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/BooleanCat/go-functional/internal/assert"
88
"github.com/BooleanCat/go-functional/iter"
9-
"github.com/BooleanCat/go-functional/option"
109
)
1110

1211
func ExampleCount() {
@@ -27,19 +26,3 @@ func TestCount(t *testing.T) {
2726
assert.Equal(t, counter.Next().Unwrap(), 1)
2827
assert.Equal(t, counter.Next().Unwrap(), 2)
2928
}
30-
31-
func TestCountFind(t *testing.T) {
32-
assert.Equal(t, iter.Count().Find(func(number int) bool {
33-
return number == 5
34-
}), option.Some(5))
35-
}
36-
37-
func TestCountDrop(t *testing.T) {
38-
counter := iter.Count().Drop(5)
39-
assert.Equal(t, counter.Next().Unwrap(), 5)
40-
}
41-
42-
func TestCountTake(t *testing.T) {
43-
numbers := iter.Count().Take(3).Collect()
44-
assert.SliceEqual(t, numbers, []int{0, 1, 2})
45-
}

iter/cycle.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,3 @@ func (iter *CycleIter[T]) Next() option.Option[T] {
5151
}
5252

5353
var _ Iterator[struct{}] = new(CycleIter[struct{}])
54-
55-
// Find is a convenience method for [Find], providing this iterator as an
56-
// argument.
57-
func (iter *CycleIter[T]) Find(predicate func(T) bool) option.Option[T] {
58-
return Find[T](iter, predicate)
59-
}
60-
61-
// Drop is a convenience method for [Drop], providing this iterator as an
62-
// argument.
63-
func (iter *CycleIter[T]) Drop(n uint) *DropIter[T] {
64-
return Drop[T](iter, n)
65-
}
66-
67-
// Take is a convenience method for [Take], providing this iterator as an
68-
// argument.
69-
func (iter *CycleIter[T]) Take(n uint) *TakeIter[T] {
70-
return Take[T](iter, n)
71-
}

iter/cycle_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/BooleanCat/go-functional/internal/assert"
88
"github.com/BooleanCat/go-functional/internal/fakes"
99
"github.com/BooleanCat/go-functional/iter"
10-
"github.com/BooleanCat/go-functional/option"
1110
)
1211

1312
func ExampleCycle() {
@@ -39,19 +38,3 @@ func TestCycleExhausted(t *testing.T) {
3938
assert.True(t, ones.Next().IsNone())
4039
assert.Equal(t, delegate.NextCallCount(), 1)
4140
}
42-
43-
func TestCycleFind(t *testing.T) {
44-
assert.Equal(t, iter.Cycle[int](iter.Lift([]int{1, 2})).Find(func(number int) bool {
45-
return number == 2
46-
}), option.Some(2))
47-
}
48-
49-
func TestCycleDrop(t *testing.T) {
50-
items := iter.Cycle[int](iter.Lift([]int{1, 2})).Drop(1).Take(5).Collect()
51-
assert.SliceEqual(t, items, []int{2, 1, 2, 1, 2})
52-
}
53-
54-
func TestCycleTake(t *testing.T) {
55-
items := iter.Cycle[int](iter.Lift([]int{1, 2})).Take(5).Collect()
56-
assert.SliceEqual(t, items, []int{1, 2, 1, 2, 1})
57-
}

iter/drop.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,3 @@ func (iter *DropIter[T]) delegateNext() option.Option[T] {
4848
}
4949

5050
var _ Iterator[struct{}] = new(DropIter[struct{}])
51-
52-
// Find is a convenience method for [Find], providing this iterator as an
53-
// argument.
54-
func (iter *DropIter[T]) Find(predicate func(T) bool) option.Option[T] {
55-
return Find[T](iter, predicate)
56-
}
57-
58-
// Drop is a convenience method for [Drop], providing this iterator as an
59-
// argument.
60-
func (iter *DropIter[T]) Drop(n uint) *DropIter[T] {
61-
return Drop[T](iter, n)
62-
}
63-
64-
// Take is a convenience method for [Take], providing this iterator as an
65-
// argument.
66-
func (iter *DropIter[T]) Take(n uint) *TakeIter[T] {
67-
return Take[T](iter, n)
68-
}

iter/drop_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,3 @@ func TestDropExhaustedLater(t *testing.T) {
4949
assert.True(t, iterator.Next().IsNone())
5050
assert.Equal(t, delegate.NextCallCount(), 3)
5151
}
52-
53-
func TestDropFind(t *testing.T) {
54-
assert.Equal(t, iter.Drop[int](iter.Count(), 5).Find(func(number int) bool {
55-
return number%4 == 0
56-
}), option.Some(8))
57-
}
58-
59-
func TestDropDrop(t *testing.T) {
60-
counter := iter.Count().Drop(2).Drop(3)
61-
assert.Equal(t, counter.Next().Unwrap(), 5)
62-
}
63-
64-
func TestDropTake(t *testing.T) {
65-
numbers := iter.Count().Drop(2).Take(3).Collect()
66-
assert.SliceEqual(t, numbers, []int{2, 3, 4})
67-
}

0 commit comments

Comments
 (0)