feat: add issorted (#152)

This commit is contained in:
fffzlfk
2022-07-25 04:47:12 +08:00
committed by GitHub
parent 03b4947e91
commit add5ee4bb2
5 changed files with 62 additions and 6 deletions

View File

@@ -92,6 +92,8 @@ Supported helpers for slices:
- [Replace](#replace)
- [ReplaceAll](#replaceall)
- [Compact](#compact)
- [IsSorted](#issorted)
- [IsSortedByKey](#issortedbykey)
Supported helpers for maps:

4
go.mod
View File

@@ -3,7 +3,7 @@ module github.com/samber/lo
go 1.18
require (
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.7.1
github.com/thoas/go-funk v0.9.1
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17
)
@@ -14,5 +14,5 @@ require (
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

8
go.sum
View File

@@ -12,8 +12,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
github.com/thoas/go-funk v0.9.1/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
@@ -23,5 +23,5 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8X
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -2,6 +2,8 @@ package lo
import (
"math/rand"
"golang.org/x/exp/constraints"
)
// Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.
@@ -446,6 +448,31 @@ func ReplaceAll[T comparable](collection []T, old T, new T) []T {
return Replace(collection, old, new, -1)
}
// IsSorted checks if a slice is sorted.
func IsSorted[T constraints.Ordered](collection []T) bool {
for i := 1; i < len(collection); i++ {
if collection[i-1] > collection[i] {
return false
}
}
return true
}
// IsSortedBy checks if a slice is sorted by iteratee.
func IsSortedByKey[K constraints.Ordered, V any](collection []V, iteratee func(V) K) bool {
size := len(collection)
for i := 0; i < size-1; i++ {
if iteratee(collection[i]) > iteratee(collection[i+1]) {
return false
}
}
return true
}
// Compact returns a slice of all non-zero elements.
func Compact[T comparable](collection []T) []T {
var zero T

View File

@@ -503,6 +503,33 @@ func TestReplaceAll(t *testing.T) {
is.Equal([]int{0, 1, 0, 1, 2, 3, 0}, out2)
}
func TestIsSorted(t *testing.T) {
is := assert.New(t)
is.True(IsSorted([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}))
is.True(IsSorted([]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}))
is.False(IsSorted([]int{0, 1, 4, 3, 2, 5, 6, 7, 8, 9, 10}))
is.False(IsSorted([]string{"a", "b", "d", "c", "e", "f", "g", "h", "i", "j"}))
}
func TestIsSortedByKey(t *testing.T) {
is := assert.New(t)
is.True(IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int {
return len(s)
}))
is.False(IsSortedByKey([]string{"aa", "b", "ccc"}, func(s string) int {
return len(s)
}))
is.True(IsSortedByKey([]string{"1", "2", "3", "11"}, func(s string) int {
ret, _ := strconv.Atoi(s)
return ret
}))
}
func TestCompact(t *testing.T) {
is := assert.New(t)