Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fn: add Filter to List #9087

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions fn/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,18 @@ func (l *List[A]) PushFrontList(other *List[A]) {
n = n.Prev()
}
}

// Filter gives a slice of all of the node values that satisfy the given
// predicate.
func (l *List[A]) Filter(f Pred[A]) []A {
var acc []A

for cursor := l.Front(); cursor != nil; cursor = cursor.Next() {
a := cursor.Value
if f(a) {
acc = append(acc, a)
}
}

return acc
}
93 changes: 93 additions & 0 deletions fn/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"reflect"
"testing"
"testing/quick"

"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)

func GenList(r *rand.Rand) *List[uint32] {
Expand Down Expand Up @@ -727,3 +730,93 @@ func TestMoveUnknownMark(t *testing.T) {
checkList(t, &l1, []int{1})
checkList(t, &l2, []int{2})
}

// TestFilterIdempotence ensures that the slice coming out of List.Filter is
// the same as that slice filtered again by the same predicate.
func TestFilterIdempotence(t *testing.T) {
require.NoError(
t, quick.Check(
func(l *List[uint32], modSize uint32) bool {
pred := func(a uint32) bool {
return a%modSize != 0
}

filtered := l.Filter(pred)

filteredAgain := Filter(pred, filtered)

return slices.Equal(filtered, filteredAgain)
},
&quick.Config{
Values: func(vs []reflect.Value, r *rand.Rand) {
l := GenList(r)
vs[0] = reflect.ValueOf(l)
vs[1] = reflect.ValueOf(
r.Uint32()%5 + 1,
)
},
},
),
)
}

// TestFilterShrinks ensures that the length of the slice returned from
// List.Filter is never larger than the length of the List.
func TestFilterShrinks(t *testing.T) {
require.NoError(
t, quick.Check(
func(l *List[uint32], modSize uint32) bool {
pred := func(a uint32) bool {
return a%modSize != 0
}

filteredSize := len(l.Filter(pred))

return filteredSize <= l.Len()
},
&quick.Config{
Values: func(vs []reflect.Value, r *rand.Rand) {
l := GenList(r)
vs[0] = reflect.ValueOf(l)
vs[1] = reflect.ValueOf(
r.Uint32()%5 + 1,
)
},
},
),
)
}

// TestFilterLawOfExcludedMiddle ensures that if we intersect a List.Filter
// with its negation that the intersection is the empty set.
func TestFilterLawOfExcludedMiddle(t *testing.T) {
require.NoError(
t, quick.Check(
func(l *List[uint32], modSize uint32) bool {
pred := func(a uint32) bool {
return a%modSize != 0
}

negatedPred := func(a uint32) bool {
return !pred(a)
}

positive := NewSet(l.Filter(pred)...)
negative := NewSet(l.Filter(negatedPred)...)

return positive.Intersect(negative).Equal(
NewSet[uint32](),
)
},
&quick.Config{
Values: func(vs []reflect.Value, r *rand.Rand) {
l := GenList(r)
vs[0] = reflect.ValueOf(l)
vs[1] = reflect.ValueOf(
r.Uint32()%5 + 1,
)
},
},
),
)
}
Loading