Skip to content

Commit

Permalink
Merge pull request #33 from dwurf/dedupby-for-non-comparable
Browse files Browse the repository at this point in the history
Remove comparable constraint from DedupBy
  • Loading branch information
orsinium authored Apr 23, 2024
2 parents 83c9345 + d66071c commit b5993eb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion slices/slice_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func CountBy[S ~[]T, T any](items S, f func(el T) bool) int {

// DedupBy returns a copy of items, but without consecutive elements
// for which f returns the same result.
func DedupBy[S ~[]T, T comparable, G comparable](items S, f func(el T) G) S {
func DedupBy[S ~[]T, T any, G comparable](items S, f func(el T) G) S {
result := make(S, 0, len(items))
if len(items) == 0 {
return result
Expand Down
9 changes: 9 additions & 0 deletions slices/slice_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ func TestDedupBy(t *testing.T) {
f([]int{1, 2, 3}, []int{1, 2, 3})
f([]int{1, 2, 2, 3}, []int{1, 2, 3})
f([]int{1, 2, 4, 3, 5, 7, 10}, []int{1, 2, 3, 10})

// DedupBy supports non-comparable types.
g := func(given [][]int, expected [][]int) {
first := func(s []int) int { return s[0] }
actual := slices.DedupBy(given, first)
is.Equal(actual, expected)
}
g([][]int{{1}, {1}}, [][]int{{1}})
g([][]int{{1}, {1, 2}}, [][]int{{1}})
}

func TestDropWhile(t *testing.T) {
Expand Down

0 comments on commit b5993eb

Please sign in to comment.