Skip to content

Commit

Permalink
feat: add xslices.Max(), xslices.Min()
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Apr 12, 2024
1 parent ee8833f commit 9307054
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 4 deletions.
47 changes: 44 additions & 3 deletions generic/xslices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ import "github.com/fufuok/utils/generic/xslices"

## Index

- [func Deduplication[E comparable](s []E) []E](<#func-deduplication>)
- [func Filter[E any, S ~[]E](s S, pred func(E) bool) S](<#func-filter>)
- [func Merge[E any](s []E, ss ...[]E) []E](<#func-merge>)
- [func Average\[T generic.Numeric\]\(xs \[\]T\) float64](<#Average>)
- [func Deduplication\[E comparable\]\(s \[\]E\) \[\]E](<#Deduplication>)
- [func Filter\[E any, S \~\[\]E\]\(s S, pred func\(E\) bool\) S](<#Filter>)
- [func Max\[T cmp.Ordered\]\(xs ...T\) \(y T\)](<#Max>)
- [func Merge\[E any\]\(s \[\]E, ss ...\[\]E\) \[\]E](<#Merge>)
- [func Min\[T cmp.Ordered\]\(xs ...T\) \(y T\)](<#Min>)
- [func ToString\[T any\]\(xs \[\]T, sep string\) string](<#ToString>)


<a name="Average"></a>
## func Average

```go
func Average[T generic.Numeric](xs []T) float64
```

Average 求数字切片的平均值

<a name="Deduplication"></a>
## func Deduplication

```go
Expand All @@ -21,6 +35,7 @@ func Deduplication[E comparable](s []E) []E

Deduplication removes repeatable elements from s.

<a name="Filter"></a>
## func Filter

```go
Expand All @@ -29,6 +44,16 @@ func Filter[E any, S ~[]E](s S, pred func(E) bool) S

Filter removes any elements from s for which pred\(element\) is false.

<a name="Max"></a>
## func Max

```go
func Max[T cmp.Ordered](xs ...T) (y T)
```



<a name="Merge"></a>
## func Merge

```go
Expand All @@ -37,6 +62,22 @@ func Merge[E any](s []E, ss ...[]E) []E

Merge 浅拷贝合并多个切片, 不影响原切片

<a name="Min"></a>
## func Min

```go
func Min[T cmp.Ordered](xs ...T) (y T)
```



<a name="ToString"></a>
## func ToString

```go
func ToString[T any](xs []T, sep string) string
```

ToString 将切片拼接成字符串

Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)
44 changes: 44 additions & 0 deletions generic/xslices/cmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//go:build go1.21
// +build go1.21

package xslices

import (
"cmp"
)

func Max[T cmp.Ordered](xs ...T) (y T) {
switch len(xs) {
case 0:
return
case 1:
return xs[0]
case 2:
return max(xs[0], xs[1])
default:
}

y = xs[0]
for _, v := range xs[1:] {
y = max(y, v)
}
return
}

func Min[T cmp.Ordered](xs ...T) (y T) {
switch len(xs) {
case 0:
return
case 1:
return xs[0]
case 2:
return min(xs[0], xs[1])
default:
}

y = xs[0]
for _, v := range xs[1:] {
y = min(y, v)
}
return
}
28 changes: 28 additions & 0 deletions generic/xslices/cmp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build go1.21
// +build go1.21

package xslices

import (
"math"
"testing"

"github.com/fufuok/utils/assert"
)

func TestMaxMin(t *testing.T) {
xs := []int{-1, 0, 1, 2}
assert.Equal(t, 2, Max(xs...))
assert.Equal(t, -1, Min(xs...))
xu := []uint{1, 0, 1, 2}
assert.Equal(t, uint(2), Max(xu...))
assert.Equal(t, uint(0), Min(xu...))

xf := []float64{-3.14, 0, 1, 2.1}
assert.Equal(t, 2.1, Max(xf...))
assert.Equal(t, -3.14, Min(xf...))
x := math.NaN()
xf = append(xf, x)
assert.True(t, math.IsNaN(Max(xf...)))
assert.True(t, math.IsNaN(Min(xf...)))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/fufuok/utils

go 1.20
go 1.21

0 comments on commit 9307054

Please sign in to comment.