Skip to content

Commit

Permalink
#61: Add ArrayMin/ArrayMax impls (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman authored May 17, 2022
1 parent d60d159 commit d0914cd
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Go library for PHP community with convenient functions
* [ArrayUdiff](#user-content-arrayudiff)
* [ArraySum](#user-content-arraysum)
* [ArrayIntersect](#user-content-arrayintersect)
* [ArrayMin/ArrayMax](#user-content-arrayminarraymax)
* [Range](#user-content-range)
* [EqualSlices](#user-content-equalslices)
* [Collections](#user-content-collections)
Expand Down Expand Up @@ -399,6 +400,17 @@ pgo.ArrayIntersect([]int{12, 54, 32, 12, 33}, []int{3, 12, 54, 9}, []int{12, 33,
pgo.ArrayIntersect([]string{"foo", "bar", "baz", "fizz", "bazz", "fizz", "fizz"}, []string{"bar", "fizz"}, []string{"foo", "bar", "hey"}) // []string{"foo", "bar", "fizz"}
```

#### ArrayMin/ArrayMax

finds minimum/maximum value from []T

```go
res = pgo.ArrayMax([]int{3, 1, 2, 9}) // res == 9
res = pgo.ArrayMax([]float64{-3.12, -1.678, -2.01, -9.007}) // res == -1.678
res = pgo.ArrayMin([]int{3, 1, 2, 9}) // res == 1
res = pgo.ArrayMin([]float64{3.2, 1.0837, 2.123, 9.87}) // res == 1.0837
```

#### Range

creates an int slice of min to max range
Expand Down
32 changes: 32 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,35 @@ func EqualSlices[T comparable](a, b []T) bool {

return true
}

// ArrayMax finds maximum value from []T
func ArrayMax[T constraints.IntFloat](a []T) T {
var max T
if len(a) > 0 {
max = a[0]
}

for _, v := range a {
if max < v {
max = v
}
}

return max
}

// ArrayMin finds minimum value from []T
func ArrayMin[T constraints.IntFloat](a []T) T {
var min T
if len(a) > 0 {
min = a[0]
}

for _, v := range a {
if min > v {
min = v
}
}

return min
}
52 changes: 52 additions & 0 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,55 @@ func TestEqual(t *testing.T) {
}
}
}

func TestArrayMax(t *testing.T) {
res := pgo.ArrayMax([]int{})
assert.Equal(t, 0, res)

res = pgo.ArrayMax([]int{3, 1, 2, 9})
assert.Equal(t, 9, res)

res = pgo.ArrayMax([]int{-3, -1, -2, -9})
assert.Equal(t, -1, res)

res = pgo.ArrayMax([]int{-3, -1, -2, -9, 3, 1, 2, 9})
assert.Equal(t, 9, res)

resF := pgo.ArrayMax([]float64{})
assert.Equal(t, float64(0), resF)

resF = pgo.ArrayMax([]float64{3.2, 1.0837, 2.123, 9.87})
assert.Equal(t, 9.87, resF)

resF = pgo.ArrayMax([]float64{-3.12, -1.678, -2.01, -9.007})
assert.Equal(t, -1.678, resF)

resF = pgo.ArrayMax([]float64{3.2, 1.0837, 2.123, 9.87, -3.12, -1.678, -2.01, -9.007})
assert.Equal(t, 9.87, resF)
}

func TestArrayMin(t *testing.T) {
res := pgo.ArrayMin([]int{})
assert.Equal(t, 0, res)

res = pgo.ArrayMin([]int{3, 1, 2, 9})
assert.Equal(t, 1, res)

res = pgo.ArrayMin([]int{-3, -1, -2, -9})
assert.Equal(t, -9, res)

res = pgo.ArrayMin([]int{-3, -1, -2, -9, 3, 1, 2, 9})
assert.Equal(t, -9, res)

resF := pgo.ArrayMin([]float64{})
assert.Equal(t, float64(0), resF)

resF = pgo.ArrayMin([]float64{3.2, 1.0837, 2.123, 9.87})
assert.Equal(t, 1.0837, resF)

resF = pgo.ArrayMin([]float64{-3.12, -1.678, -2.01, -9.007})
assert.Equal(t, -9.007, resF)

resF = pgo.ArrayMin([]float64{-3.12, -1.678, -2.01, -9.007, 3.2, 1.0837, 2.123, 9.87})
assert.Equal(t, -9.007, resF)
}

0 comments on commit d0914cd

Please sign in to comment.