Skip to content

Commit

Permalink
Merge pull request #785 from lavanet/feat-add-helper-slices-filter
Browse files Browse the repository at this point in the history
feat: add slices helper Filter()
  • Loading branch information
Yaroms authored Sep 13, 2023
2 parents d943ed4 + b9b6827 commit b9cf6de
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion testutil/common/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func NewCoin(amount int64) sdk.Coin {
}

func NewCoins(amount ...int64) []sdk.Coin {
return slices.Filter(amount, NewCoin)
return slices.Map(amount, NewCoin)
}

// keeper helpers
Expand Down
12 changes: 11 additions & 1 deletion utils/slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,24 @@ func UnionByFunc[T ComparableByFunc](arrays ...[]T) []T {
return res
}

func Filter[T, V any](slice []T, filter func(T) V) []V {
func Map[T, V any](slice []T, filter func(T) V) []V {
values := make([]V, len(slice))
for i := range slice {
values[i] = filter(slice[i])
}
return values
}

func Filter[T any](slice []T, filter func(T) bool) []T {
values := make([]T, 0)
for _, v := range slice {
if filter(v) {
values = append(values, v)
}
}
return values
}

func UnorderedEqual[T comparable](slices ...[]T) bool {
var length int

Expand Down
15 changes: 11 additions & 4 deletions utils/slices/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,16 @@ func TestUnorderedEqual(t *testing.T) {
}
}

func TestMap(t *testing.T) {
mapFunc := func(_ int) int { return 10 }
require.Equal(t, []int{}, Map([]int{}, mapFunc))
require.Equal(t, []int{10}, Map([]int{1}, mapFunc))
require.Equal(t, []int{10, 10, 10}, Map([]int{1, 2, 3}, mapFunc))
}

func TestFilter(t *testing.T) {
filter := func(_ int) int { return 10 }
require.Equal(t, Filter([]int{}, filter), []int{})
require.Equal(t, Filter([]int{1}, filter), []int{10})
require.Equal(t, Filter([]int{1, 2, 3}, filter), []int{10, 10, 10})
filter := func(v int) bool { return v%2 == 0 }
require.Equal(t, []int{}, Filter([]int{}, filter))
require.Equal(t, []int{}, Filter([]int{1}, filter))
require.Equal(t, []int{2, 4}, Filter([]int{1, 2, 3, 4}, filter))
}
8 changes: 4 additions & 4 deletions x/pairing/keeper/pairing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func TestPairingUniqueness(t *testing.T) {
pairing2, err := ts.QueryPairingGetPairing(ts.spec.Index, sub2Addr)
require.Nil(t, err)

filter := func(p epochstoragetypes.StakeEntry) string { return p.Address }
mapFunc := func(p epochstoragetypes.StakeEntry) string { return p.Address }

providerAddrs1 := slices.Filter(pairing1.Providers, filter)
providerAddrs2 := slices.Filter(pairing2.Providers, filter)
providerAddrs1 := slices.Map(pairing1.Providers, mapFunc)
providerAddrs2 := slices.Map(pairing2.Providers, mapFunc)

require.Equal(t, len(pairing1.Providers), len(pairing2.Providers))
require.False(t, slices.UnorderedEqual(providerAddrs1, providerAddrs2))
Expand All @@ -60,7 +60,7 @@ func TestPairingUniqueness(t *testing.T) {
pairing11, err := ts.QueryPairingGetPairing(ts.spec.Index, sub1Addr)
require.Nil(t, err)

providerAddrs11 := slices.Filter(pairing11.Providers, filter)
providerAddrs11 := slices.Map(pairing11.Providers, mapFunc)

require.Equal(t, len(pairing1.Providers), len(pairing11.Providers))
require.False(t, slices.UnorderedEqual(providerAddrs1, providerAddrs11))
Expand Down

0 comments on commit b9cf6de

Please sign in to comment.