Skip to content

Commit

Permalink
Restore coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
BooleanCat committed Jun 30, 2024
1 parent 1f69572 commit b7edecd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 31 deletions.
22 changes: 22 additions & 0 deletions it/exhausted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"maps"
"slices"
"testing"

"github.com/BooleanCat/go-functional/v2/internal/assert"
"github.com/BooleanCat/go-functional/v2/it"
)

Expand All @@ -17,3 +19,23 @@ func ExampleExhausted2() {
fmt.Println(len(maps.Collect(it.Exhausted2[int, string]())))
// Output: 0
}

func TestEnumerateYieldFalse(t *testing.T) {
t.Parallel()

iterator := it.Enumerate(slices.Values([]int{1, 2, 3, 4, 5}))

var (
index int
number int
)

iterator(func(i int, n int) bool {
index = i
number = n
return false
})

assert.Equal(t, index, 0)
assert.Equal(t, number, 1)
}
30 changes: 13 additions & 17 deletions it/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@ func Filter[V any](delegate iter.Seq[V], predicate func(V) bool) iter.Seq[V] {

// Exclude yields values from an iterator that do not satisfy a predicate.
func Exclude[V any](delegate iter.Seq[V], predicate func(V) bool) iter.Seq[V] {
return func(yield func(V) bool) {
for value := range delegate {
if !predicate(value) {
if !yield(value) {
return
}
}
}
}
return Filter(delegate, not(predicate))
}

// Filter2 yields values from an iterator that satisfy a predicate.
Expand All @@ -43,13 +35,17 @@ func Filter2[V, W any](delegate iter.Seq2[V, W], predicate func(V, W) bool) iter

// Exclude2 yields values from an iterator that do not satisfy a predicate.
func Exclude2[V, W any](delegate iter.Seq2[V, W], predicate func(V, W) bool) iter.Seq2[V, W] {
return func(yield func(V, W) bool) {
for k, v := range delegate {
if !predicate(k, v) {
if !yield(k, v) {
return
}
}
}
return Filter2(delegate, not2(predicate))
}

func not[V any](predicate func(V) bool) func(V) bool {
return func(value V) bool {
return !predicate(value)
}
}

func not2[V, W any](predicate func(V, W) bool) func(V, W) bool {
return func(k V, v W) bool {
return !predicate(k, v)
}
}
42 changes: 28 additions & 14 deletions it/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package it_test

import (
"fmt"
"iter"
"maps"
"slices"
"testing"
Expand All @@ -28,11 +27,19 @@ func TestFilterEmpty(t *testing.T) {
assert.Empty[int](t, slices.Collect(it.Filter(it.Exhausted[int](), filter.IsEven)))
}

func TestFilterTerminateEarly(t *testing.T) {
func TestFilterYieldsFalse(t *testing.T) {
t.Parallel()

_, stop := iter.Pull(it.Filter(slices.Values([]int{1, 2, 3}), filter.IsEven))
stop()
seq := it.Filter(slices.Values([]int{1, 2, 3, 4, 5}), filter.IsEven)

var value int

seq(func(v int) bool {
value = v
return false
})

assert.Equal(t, value, 2)
}

func ExampleExclude() {
Expand Down Expand Up @@ -63,11 +70,25 @@ func TestFilter2Empty(t *testing.T) {
assert.Equal(t, len(maps.Collect(it.Filter2(it.Exhausted2[int, int](), filter.Passthrough2))), 0)
}

func TestFilter2TerminateEarly(t *testing.T) {
func TestFilter2YieldsFalse(t *testing.T) {
t.Parallel()

_, stop := iter.Pull2(it.Filter2(maps.All(map[int]string{1: "one", 2: "two"}), filter.Passthrough2))
stop()
seq := it.Filter2(maps.All(map[int]string{1: "one", 2: "two"}), filter.Passthrough2)

var (
key int
value string
)

seq(func(k int, v string) bool {
key = k
value = v
return false
})

assert.Equal(t, key, 1)
assert.Equal(t, value, "one")

}

func ExampleExclude2() {
Expand All @@ -86,10 +107,3 @@ func TestExclude2Empty(t *testing.T) {

assert.Equal(t, len(maps.Collect(it.Exclude2(it.Exhausted2[int, int](), filter.Passthrough2))), 0)
}

func TestExclude2TerminateEarly(t *testing.T) {
t.Parallel()

_, stop := iter.Pull2(it.Exclude2(it.Exhausted2[int, int](), filter.Passthrough2))
stop()
}

0 comments on commit b7edecd

Please sign in to comment.