Skip to content

Commit b590704

Browse files
authored
Merge pull request #5 from kacperwnuk/main
Use constraints package to define 'number' type. Enable calling sum o…
2 parents 4cdca65 + 688343a commit b590704

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ require github.com/stretchr/testify v1.7.1
77
require (
88
github.com/davecgh/go-spew v1.1.0 // indirect
99
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 // indirect
1011
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
1112
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
55
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
66
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
77
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 h1:TfdoLivD44QwvssI9Sv1xwa5DcL5XQr4au4sZ2F2NV4=
9+
golang.org/x/exp v0.0.0-20220428152302-39d4317da171/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
810
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
911
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1012
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

sliceutils/sliceutils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package sliceutils
55

6+
import "golang.org/x/exp/constraints"
7+
68
// Filter - given a slice of type T, executes the given predicate function on each element in the slice.
79
// The predicate is passed the current element, the current index and the slice itself as function arguments.
810
// If the predicate returns true, the value is included in the result, otherwise it is filtered out.
@@ -176,7 +178,7 @@ func Merge[T any](slices ...[]T) (mergedSlice []T) {
176178

177179
// numbers - an interface for all number types.
178180
type numbers interface {
179-
int | uint | uint8 | uint16 | uint32 | uint64 | int8 | int16 | int32 | int64 | float32 | float64 | complex64 | complex128
181+
constraints.Complex | constraints.Integer | constraints.Float
180182
}
181183

182184
// Sum - receives a slice of type T and returns a value T that is the sum of the numbers.

sliceutils/sliceutils_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12+
type MyInt int
13+
1214
var numerals = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
15+
var numeralsWithUserDefinedType = []MyInt{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
1316
var days = []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
1417
var lastNames = []string{"Jacobs", "Vin", "Jacobs", "Smith"}
1518

@@ -148,6 +151,11 @@ func TestSum(t *testing.T) {
148151
assert.Equal(t, 45, result)
149152
}
150153

154+
func TestSum2(t *testing.T) {
155+
result := sliceutils.Sum(numeralsWithUserDefinedType)
156+
assert.Equal(t, MyInt(45), result)
157+
}
158+
151159
func TestRemove(t *testing.T) {
152160
testSlice := []int{1, 2, 3}
153161
result := sliceutils.Remove(testSlice, 1)

0 commit comments

Comments
 (0)