Skip to content

Commit

Permalink
Remove unused EOF error and use comma ok syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
yaa110 committed Aug 13, 2020
1 parent 1f37579 commit 70085c4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 32 deletions.
20 changes: 10 additions & 10 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ func (iter *Iterator) Reduce(initialState interface{}, f ReduceFunc) interface{}
}

// Find consumes elements and returns the first element that satisfies `f` (returning `true`).
// Returns an `EOF` error if no element is found.
func (iter *Iterator) Find(f PredicateFunc) (interface{}, error) {
// Returns `nil, false` if no element is found.
func (iter *Iterator) Find(f PredicateFunc) (interface{}, bool) {
var elem interface{}
err := End()
ok := false
iter.iter(func(element interface{}) bool {
if f(element) {
elem = element
err = nil
ok = true
return false
}
return true
})
return elem, err
return elem, ok
}

// Min consumes elements and returns the minimum element.
Expand Down Expand Up @@ -156,21 +156,21 @@ func (iter *Iterator) Last() interface{} {
}

// Nth consumes elements and returns the `n`th element. Indexing starts from `0`.
// Returns an `EOF` error if the length of iterator is less than `n`.
func (iter *Iterator) Nth(n int) (interface{}, error) {
// Returns an nil, false` if the length of iterator is less than `n`.
func (iter *Iterator) Nth(n int) (interface{}, bool) {
var nth interface{}
err := End()
ok := false
index := 0
iter.iter(func(element interface{}) bool {
if index == n {
nth = element
err = nil
ok = true
return false
}
index++
return true
})
return nth, err
return nth, ok
}

// Count consumes elements and returns the length of elements.
Expand Down
16 changes: 8 additions & 8 deletions consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ func TestFind(t *testing.T) {
gen := generator.NewSlice(elements)
iterator := goterator.New(gen)

three, err := iterator.Find(func(e interface{}) bool {
three, ok := iterator.Find(func(e interface{}) bool {
return e.(int) == 3
})
assert.Equal(3, three)
assert.Nil(err)
assert.True(ok)

gen = generator.NewSlice(elements)
iterator = goterator.New(gen)

_, err = iterator.Find(func(e interface{}) bool {
_, ok = iterator.Find(func(e interface{}) bool {
return e.(int) == 6
})
assert.NotNil(err)
assert.False(ok)
}

func TestMin(t *testing.T) {
Expand Down Expand Up @@ -160,15 +160,15 @@ func TestNth(t *testing.T) {
gen := generator.NewSlice(elements)
iterator := goterator.New(gen)

nth, err := iterator.Nth(5)
nth, ok := iterator.Nth(5)
assert.Equal(7, nth)
assert.Nil(err)
assert.True(ok)

gen = generator.NewSlice(elements)
iterator = goterator.New(gen)

_, err = iterator.Nth(len(elements))
assert.NotNil(err)
_, ok = iterator.Nth(len(elements))
assert.False(ok)
}

func TestCount(t *testing.T) {
Expand Down
14 changes: 0 additions & 14 deletions error.go

This file was deleted.

0 comments on commit 70085c4

Please sign in to comment.