Skip to content

Commit

Permalink
Add Select and Map to set API
Browse files Browse the repository at this point in the history
  • Loading branch information
moorara committed Jan 17, 2024
1 parent 90c888c commit 4dcf183
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
18 changes: 18 additions & 0 deletions set/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package set

import "github.com/moorara/algo/generic"

// Mapper
type Mapper[T, U any] func(T) U

func (f Mapper[T, U]) Map(s Set[T], equal generic.EqualFunc[U]) Set[U] {
members := make([]U, 0)
for _, m := range s.Members() {
members = append(members, f(m))
}

return &set[U]{
equal: equal,
members: members,
}
}
56 changes: 56 additions & 0 deletions set/map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package set

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/moorara/algo/generic"
)

func TestMapper_Map(t *testing.T) {
eqFunc := generic.NewEqualFunc[string]()
mapper := func(s string) string {
return strings.ToUpper(s)
}

tests := []struct {
name string
f Mapper[string, string]
s *set[string]
expected Set[string]
}{
{
name: "Empty",
f: mapper,
s: &set[string]{
equal: eqFunc,
members: []string{},
},
expected: &set[string]{
equal: eqFunc,
members: []string{},
},
},
{
name: "NonEmpty",
f: mapper,
s: &set[string]{
equal: eqFunc,
members: []string{"a", "b", "c", "d"},
},
expected: &set[string]{
equal: eqFunc,
members: []string{"A", "B", "C", "D"},
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
set := tc.f.Map(tc.s, eqFunc)
assert.True(t, set.Equals(tc.expected))
})
}
}
15 changes: 15 additions & 0 deletions set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Set[T any] interface {
Members() []T
Clone() Set[T]
CloneEmpty() Set[T]
Select(Predicate[T]) Set[T]
Union(...Set[T]) Set[T]
Intersection(...Set[T]) Set[T]
Difference(...Set[T]) Set[T]
Expand Down Expand Up @@ -150,6 +151,20 @@ func (s *set[T]) CloneEmpty() Set[T] {
return t
}

func (s *set[T]) Select(p Predicate[T]) Set[T] {
members := make([]T, 0)
for _, m := range s.members {
if p(m) {
members = append(members, m)
}
}

return &set[T]{
equal: s.equal,
members: members,
}
}

func (s *set[T]) Union(sets ...Set[T]) Set[T] {
t := s.Clone()

Expand Down
58 changes: 58 additions & 0 deletions set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,64 @@ func TestSet_CloneEmpty(t *testing.T) {
}
}

func TestSet_Select(t *testing.T) {
eqFunc := generic.NewEqualFunc[string]()
predicate := func(s string) bool {
return strings.ToUpper(s) == s
}

tests := []struct {
name string
s *set[string]
p Predicate[string]
expected Set[string]
}{
{
name: "Empty",
s: &set[string]{
equal: eqFunc,
members: []string{},
},
p: predicate,
expected: &set[string]{
equal: eqFunc,
members: []string{},
},
},
{
name: "SelectNone",
s: &set[string]{
equal: eqFunc,
members: []string{"a", "b", "c", "d"},
},
p: predicate,
expected: &set[string]{
equal: eqFunc,
members: []string{},
},
},
{
name: "SelectSome",
s: &set[string]{
equal: eqFunc,
members: []string{"A", "c", "C", "d"},
},
p: predicate,
expected: &set[string]{
equal: eqFunc,
members: []string{"A", "C"},
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
set := tc.s.Select(tc.p)
assert.True(t, set.Equals(tc.expected))
})
}
}

func TestSet_Union(t *testing.T) {
eqFunc := generic.NewEqualFunc[string]()

Expand Down

0 comments on commit 4dcf183

Please sign in to comment.