1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-15 02:02:27 +08:00

Compare commits

...

7 Commits

Author SHA1 Message Date
dudaodong
730f061b95 feat: add Entries FromEntries Transform 2023-02-17 15:24:46 +08:00
dudaodong
2eb08f3404 feat: add FilterByValues function 2023-02-17 11:59:44 +08:00
dudaodong
4c1496b648 feat: add FilterByKeys function 2023-02-17 11:57:09 +08:00
dudaodong
c2ae784f27 feat: add ValuesBy function 2023-02-17 11:48:11 +08:00
dudaodong
e71cecefea feat: add KeysBy function 2023-02-17 11:40:20 +08:00
dudaodong
572e53aa14 doc: add go playground demo for xerror package 2023-02-16 14:28:50 +08:00
dudaodong
ca9ecb9c8a update go playground demo 2023-02-16 14:10:24 +08:00
8 changed files with 301 additions and 18 deletions

View File

@@ -248,6 +248,8 @@ import "github.com/duke-git/lancet/v2/convertor"
[[play](https://go.dev/play/p/zI6xsmuQRbn)]
- **<big>DeepClone</big>** : creates a deep copy of passed item, can't clone unexported field of struct.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#DeepClone)]
[[play](https://go.dev/play/p/j4DP5dquxnk)]
### 5. Cryptor package is for data encryption and decryption.
@@ -836,13 +838,16 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/pJ-d6MUWcvK)]
- **<big>Drop</big>** : drop n elements from the start of a slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Drop)]
[[play](https://go.dev/play/p/pJ-d6MUWcvK)]
[[play](https://go.dev/play/p/jnPO2yQsT8H)]
- **<big>DropRight</big>** : drop n elements from the end of a slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DropRight)]
[[play](https://go.dev/play/p/8bcXvywZezG)]
- **<big>DropWhile</big>** : drop n elements from the start of a slice while predicate function returns true.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DropWhile)]
[[play](https://go.dev/play/p/4rt252UV_qs)]
- **<big>DropRightWhile</big>** : drop n elements from the end of a slice while predicate function returns true.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DropRightWhile)]
[[play](https://go.dev/play/p/6wyK3zMY56e)]
- **<big>Equal</big>** : checks if two slices are equal: the same length and all elements' order and value are equal.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Equal)]
[[play](https://go.dev/play/p/WcRQJ37ifPa)]
@@ -920,12 +925,16 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/YHvhnWGU3Ge)]
- **<big>IsAscending</big>** : Checks if a slice is ascending order.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IsAscending)]
[[play](https://go.dev/play/p/9CtsFjet4SH)]
- **<big>IsDescending</big>** : Checks if a slice is descending order.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IsDescending)]
[[play](https://go.dev/play/p/U_LljFXma14)]
- **<big>IsSorted</big>** : Checks if a slice is sorted (ascending or descending).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IsSorted)]
[[play](https://go.dev/play/p/nCE8wPLwSA-)]
- **<big>IsSortedByKey</big>** : Checks if a slice is sorted by iteratee function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IsSortedByKey)]
[[play](https://go.dev/play/p/tUoGB7DOHI4)]
- **<big>Sort</big>** : sorts a slice of any ordered type(number or string).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Sort)]
[[play](https://go.dev/play/p/V9AVjzf_4Fk)]
@@ -1178,31 +1187,43 @@ import "github.com/duke-git/lancet/v2/xerror"
#### Function list:
- **<big>New</big>** : creates a new XError pointer instance with message.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#New)]
[[play](https://go.dev/play/p/w4oWZts7q7f)]
- **<big>Wrap</big>** : creates a new XError pointer instance based on error object, and add message.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#Wrap)]
[[play](https://go.dev/play/p/5385qT2dCi4)]
- **<big>Unwrap</big>** : returns unwrapped XError from err by errors.As. If no XError, returns nil.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#Unwrap)]
[[play](https://go.dev/play/p/LKMLep723tu)]
- **<big>XError_Wrap</big>** : creates a new XError and copy message and id to new one.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#XError_Wrap)]
[[play](https://go.dev/play/p/5385qT2dCi4)]
- **<big>XError_Unwrap</big>** : Compatible with github.com/pkg/errors.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#XError_Unwrap)]
[[play](https://go.dev/play/p/VUXJ8BST4c6)]
- **<big>XError_With</big>** : adds key and value related to the XError object.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#XError_With)]
[[play](https://go.dev/play/p/ow8UISXX_Dp)]
- **<big>XError_Id</big>** : sets XError object id to check equality in XError.Is.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#XError_Id)]
[[play](https://go.dev/play/p/X6HBlsy58U9)]
- **<big>XError_Is</big>** : checks if target error is XError and Error.id of two errors are matched.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#XError_Is)]
[[play](https://go.dev/play/p/X6HBlsy58U9)]
- **<big>XError_Values</big>** : returns map of key and value that is set by XError.With function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#XError_Values)]
[[play](https://go.dev/play/p/ow8UISXX_Dp)]
- **<big>XError_StackTrace</big>** : returns stack trace which is compatible with pkg/errors.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#XError_StackTrace)]
[[play](https://go.dev/play/p/6FAvSQpa7pc)]
- **<big>XError_Info</big>** : returns information of xerror, which can be printed.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#XError_Info)]
[[play](https://go.dev/play/p/1ZX0ME1F-Jb)]
- **<big>XError_Error</big>** : implements standard error interface.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#XError_Error)]
[[play](https://go.dev/play/p/w4oWZts7q7f)]
- **<big>TryUnwrap</big>** : check if err is nil then it returns a valid value. If err is not nil, TryUnwrap panics with err.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#TryUnwrap)]
[[play](https://go.dev/play/p/w84d7Mb3Afk)]
[[play](https://go.dev/play/p/acyZVkNZEeW)]
## How to Contribute

View File

@@ -247,6 +247,8 @@ import "github.com/duke-git/lancet/v2/convertor"
[[play](https://go.dev/play/p/zI6xsmuQRbn)]
- **<big>DeepClone</big>** : 创建一个传入值的深拷贝, 无法克隆结构体的非导出字段。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#DeepClone)]
[[play](https://go.dev/play/p/j4DP5dquxnk)]
### 5. cryptor 加密包支持数据加密和解密,获取 md5hash 值。支持 base64, md5, hmac, aes, des, rsa。
@@ -844,13 +846,16 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/pJ-d6MUWcvK)]
- **<big>Drop</big>** : 从切片头部删除n个元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Drop)]
[[play](https://go.dev/play/p/pJ-d6MUWcvK)]
[[play](https://go.dev/play/p/jnPO2yQsT8H)]
- **<big>DropRight</big>** : 从切片尾部删除n个元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DropRight)]
[[play](https://go.dev/play/p/8bcXvywZezG)]
- **<big>DropWhile</big>** : 从切片的头部删除n个元素这个n个元素满足predicate函数返回true。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DropWhile)]
[[play](https://go.dev/play/p/4rt252UV_qs)]
- **<big>DropRightWhile</big>** : 从切片的尾部删除n个元素这个n个元素满足predicate函数返回true。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DropRightWhile)]
[[play](https://go.dev/play/p/6wyK3zMY56e)]
- **<big>Equal</big>** : 检查两个切片是否相等,相等条件:切片长度相同,元素顺序和值都相同。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Equal)]
[[play](https://go.dev/play/p/WcRQJ37ifPa)]
@@ -928,12 +933,16 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/YHvhnWGU3Ge)]
- **<big>IsAscending</big>** : 检查切片元素是否按升序排列。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IsAscending)]
[[play](https://go.dev/play/p/9CtsFjet4SH)]
- **<big>IsDescending</big>** : 检查切片元素是否按降序排列。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IsDescending)]
[[play](https://go.dev/play/p/U_LljFXma14)]
- **<big>IsSorted</big>** : 检查切片元素是否是有序的(升序或降序)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IsSorted)]
[[play](https://go.dev/play/p/nCE8wPLwSA-)]
- **<big>IsSortedByKey</big>** : 通过iteratee函数检查切片元素是否是有序的。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IsSortedByKey)]
[[play](https://go.dev/play/p/tUoGB7DOHI4)]
- **<big>Sort</big>** : 对任何有序类型(数字或字符串)的切片进行排序,使用快速排序算法。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Sort)]
[[play](https://go.dev/play/p/V9AVjzf_4Fk)]
@@ -1190,31 +1199,43 @@ import "github.com/duke-git/lancet/v2/xerror"
- **<big>New</big>** : 创建XError对象实例。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#New)]
[[play](https://go.dev/play/p/w4oWZts7q7f)]
- **<big>Wrap</big>** : 根据error对象创建XError对象实例可添加message。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#Wrap)]
[[play](https://go.dev/play/p/5385qT2dCi4)]
- **<big>Unwrap</big>** : 从error对象中解构出XError。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#Unwrap)]
[[play](https://go.dev/play/p/LKMLep723tu)]
- **<big>XError_Wrap</big>** : 创建新的XError对象并将消息和id复制到新的对象中。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#XError_Wrap)]
[[play](https://go.dev/play/p/5385qT2dCi4)]
- **<big>XError_Unwrap</big>** : 解构XEerror为error对象。适配github.com/pkg/errors。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#XError_Unwrap)]
[[play](https://go.dev/play/p/VUXJ8BST4c6)]
- **<big>XError_With</big>** : 添加与XError对象的键和值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#XError_With)]
[[play](https://go.dev/play/p/ow8UISXX_Dp)]
- **<big>XError_Id</big>** : 设置XError对象的id。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#XError_Id)]
[[play](https://go.dev/play/p/X6HBlsy58U9)]
- **<big>XError_Is</big>** : 检查目标error是否为XError两个错误中的error.id是否匹配。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#XError_Is)]
[[play](https://go.dev/play/p/X6HBlsy58U9)]
- **<big>XError_Values</big>** : 返回由With设置的键和值的映射。将合并所有XError键和值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#XError_Values)]
[[play](https://go.dev/play/p/ow8UISXX_Dp)]
- **<big>XError_StackTrace</big>** : 返回与pkg/error兼容的堆栈信息。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#XError_StackTrace)]
[[play](https://go.dev/play/p/6FAvSQpa7pc)]
- **<big>XError_Info</big>** : 返回可打印的XError对象信息。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#XError_Info)]
[[play](https://go.dev/play/p/1ZX0ME1F-Jb)]
- **<big>XError_Error</big>** : 实现标准库的error接口。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#XError_Error)]
[[play](https://go.dev/play/p/w4oWZts7q7f)]
- **<big>TryUnwrap</big>** : 检查error, 如果err为nil则展开则它返回一个有效值如果err不是nil则Unwrap使用err发生panic。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#TryUnwrap)]
[[play](https://go.dev/play/p/w84d7Mb3Afk)]
[[play](https://go.dev/play/p/acyZVkNZEeW)]
## 如何贡献代码

View File

@@ -327,7 +327,7 @@ func DecodeByte(data []byte, target any) error {
// DeepClone creates a deep copy of passed item.
// can't clone unexported field of struct
// Play: todo
// Play: https://go.dev/play/p/j4DP5dquxnk
func DeepClone[T any](src T) T {
c := cloner{
ptrs: map[reflect.Type]map[uintptr]reflect.Value{},

View File

@@ -133,7 +133,7 @@ func main() {
err1 := xerror.New("error").With("level", "high")
wrapErr := errors.Wrap(err1, "oops")
err := xerror.Unwrap(err1)
err := xerror.Unwrap(wrapErr)
values := err.Values()
fmt.Println(values["level"])
@@ -441,7 +441,7 @@ import (
)
func main() {
err := New("error")
err := xerror.New("error")
fmt.Println(err.Error())
// Output:

View File

@@ -133,7 +133,7 @@ func main() {
err1 := xerror.New("error").With("level", "high")
wrapErr := errors.Wrap(err1, "oops")
err := xerror.Unwrap(err1)
err := xerror.Unwrap(wrapErr)
values := err.Values()
fmt.Println(values["level"])
@@ -441,7 +441,7 @@ import (
)
func main() {
err := New("error")
err := xerror.New("error")
fmt.Println(err.Error())
// Output:

View File

@@ -4,7 +4,11 @@
// Package maputil includes some functions to manipulate map.
package maputil
import "reflect"
import (
"reflect"
"github.com/duke-git/lancet/v2/slice"
)
// Keys returns a slice of the map's keys.
// Play: https://go.dev/play/p/xNB5bTb97Wd
@@ -34,6 +38,30 @@ func Values[K comparable, V any](m map[K]V) []V {
return values
}
// KeysBy creates a slice whose element is the result of function mapper invoked by every map's key.
// Play: todo
func KeysBy[K comparable, V any, T any](m map[K]V, mapper func(item K) T) []T {
keys := make([]T, 0, len(m))
for k := range m {
keys = append(keys, mapper(k))
}
return keys
}
// ValuesBy creates a slice whose element is the result of function mapper invoked by every map's value.
// Play: todo
func ValuesBy[K comparable, V any, T any](m map[K]V, mapper func(item V) T) []T {
keys := make([]T, 0, len(m))
for _, v := range m {
keys = append(keys, mapper(v))
}
return keys
}
// Merge maps, next key will overwrite previous key.
// Play: https://go.dev/play/p/H95LENF1uB-
func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
@@ -69,6 +97,32 @@ func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool)
return result
}
// FilterByKeys iterates over map, return a new map whose keys are all given keys.
// Play: todo
func FilterByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V {
result := make(map[K]V)
for k, v := range m {
if slice.Contain(keys, k) {
result[k] = v
}
}
return result
}
// FilterByValues iterates over map, return a new map whose values are all given values.
// Play: todo
func FilterByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V {
result := make(map[K]V)
for k, v := range m {
if slice.Contain(values, v) {
result[k] = v
}
}
return result
}
// Intersect iterates over maps, return a new map of key and value pairs in all given maps.
// Play: https://go.dev/play/p/Zld0oj3sjcC
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V {
@@ -126,3 +180,49 @@ func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool {
}
return true
}
// Entry is a key/value pairs.
type Entry[K comparable, V any] struct {
Key K
Value V
}
// Entries transforms a map into array of key/value pairs.
// Play: todo
func Entries[K comparable, V any](m map[K]V) []Entry[K, V] {
entries := make([]Entry[K, V], 0, len(m))
for k, v := range m {
entries = append(entries, Entry[K, V]{
Key: k,
Value: v,
})
}
return entries
}
// FromEntries creates a map based on a slice of key/value pairs
// Play: todo
func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V {
result := make(map[K]V, len(entries))
for _, v := range entries {
result[v.Key] = v.Value
}
return result
}
// Transform a map to another type map.
// Play: todo
func Transform[K1 comparable, V1 any, K2 comparable, V2 any](m map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2 {
result := make(map[K2]V2, len(m))
for k1, v1 := range m {
k2, v2 := iteratee(k1, v1)
result[k2] = v2
}
return result
}

View File

@@ -2,6 +2,7 @@ package maputil
import (
"sort"
"strconv"
"testing"
"github.com/duke-git/lancet/v2/internal"
@@ -41,6 +42,51 @@ func TestValues(t *testing.T) {
assert.Equal([]string{"a", "a", "b", "c", "d"}, values)
}
func TestKeysBy(t *testing.T) {
assert := internal.NewAssert(t, "TestKeysBy")
m := map[int]string{
1: "a",
2: "a",
3: "b",
}
keys := KeysBy(m, func(n int) int {
return n + 1
})
sort.Ints(keys)
assert.Equal([]int{2, 3, 4}, keys)
}
func TestValuesBy(t *testing.T) {
assert := internal.NewAssert(t, "TestValuesBy")
m := map[int]string{
1: "a",
2: "b",
3: "c",
}
values := ValuesBy(m, func(v string) string {
switch v {
case "a":
return "a-1"
case "b":
return "b-2"
case "c":
return "c-3"
default:
return ""
}
})
sort.Strings(values)
assert.Equal([]string{"a-1", "b-2", "c-3"}, values)
}
func TestMerge(t *testing.T) {
assert := internal.NewAssert(t, "TestMerge")
@@ -104,6 +150,44 @@ func TestFilter(t *testing.T) {
}, acturl)
}
func TestFilterByKeys(t *testing.T) {
assert := internal.NewAssert(t, "TestFilterByKeys")
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
}
acturl := FilterByKeys(m, []string{"a", "b"})
assert.Equal(map[string]int{
"a": 1,
"b": 2,
}, acturl)
}
func TestFilterByValues(t *testing.T) {
assert := internal.NewAssert(t, "TestFilterByValues")
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
}
acturl := FilterByValues(m, []int{3, 4})
assert.Equal(map[string]int{
"c": 3,
"d": 4,
}, acturl)
}
func TestIntersect(t *testing.T) {
assert := internal.NewAssert(t, "TestIntersect")
@@ -170,3 +254,60 @@ func TestIsDisjoint(t *testing.T) {
assert.Equal(false, IsDisjoint(m1, m3))
}
func TestEntries(t *testing.T) {
assert := internal.NewAssert(t, "TestEntries")
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
result := Entries(m)
sort.Slice(result, func(i, j int) bool {
return result[i].Value < result[j].Value
})
expected := []Entry[string, int]{{Key: "a", Value: 1}, {Key: "b", Value: 2}, {Key: "c", Value: 3}}
assert.Equal(expected, result)
}
func TestFromEntries(t *testing.T) {
assert := internal.NewAssert(t, "TestFromEntries")
result := FromEntries([]Entry[string, int]{
{Key: "a", Value: 1},
{Key: "b", Value: 2},
{Key: "c", Value: 3},
})
assert.Equal(3, len(result))
assert.Equal(1, result["a"])
assert.Equal(2, result["b"])
assert.Equal(3, result["c"])
}
func TestTransform(t *testing.T) {
assert := internal.NewAssert(t, "TestTransform")
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
result := Transform(m, func(k string, v int) (string, string) {
return k, strconv.Itoa(v)
})
expected := map[string]string{
"a": "1",
"b": "2",
"c": "3",
}
assert.Equal(expected, result)
}

View File

@@ -559,7 +559,7 @@ func DeleteAt[T any](slice []T, start int, end ...int) []T {
}
// Drop drop n elements from the start of a slice.
// Play: https://go.dev/play/p/pJ-d6MUWcvK
// Play: https://go.dev/play/p/jnPO2yQsT8H
func Drop[T any](slice []T, n int) []T {
size := len(slice)
@@ -577,7 +577,7 @@ func Drop[T any](slice []T, n int) []T {
}
// DropRight drop n elements from the end of a slice.
// Play: todo
// Play: https://go.dev/play/p/8bcXvywZezG
func DropRight[T any](slice []T, n int) []T {
size := len(slice)
@@ -595,7 +595,7 @@ func DropRight[T any](slice []T, n int) []T {
}
// DropWhile drop n elements from the start of a slice while predicate function returns true.
// Play: todo
// Play: https://go.dev/play/p/4rt252UV_qs
func DropWhile[T any](slice []T, predicate func(item T) bool) []T {
i := 0
@@ -611,7 +611,7 @@ func DropWhile[T any](slice []T, predicate func(item T) bool) []T {
}
// DropRightWhile drop n elements from the end of a slice while predicate function returns true.
// Play: todo
// Play: https://go.dev/play/p/6wyK3zMY56e
func DropRightWhile[T any](slice []T, predicate func(item T) bool) []T {
i := len(slice) - 1
@@ -831,7 +831,7 @@ func Shuffle[T any](slice []T) []T {
}
// IsAscending checks if a slice is ascending order.
// Play: todo
// Play: https://go.dev/play/p/9CtsFjet4SH
func IsAscending[T constraints.Ordered](slice []T) bool {
for i := 1; i < len(slice); i++ {
if slice[i-1] > slice[i] {
@@ -843,7 +843,7 @@ func IsAscending[T constraints.Ordered](slice []T) bool {
}
// IsDescending checks if a slice is descending order.
// Play: todo
// Play: https://go.dev/play/p/U_LljFXma14
func IsDescending[T constraints.Ordered](slice []T) bool {
for i := 1; i < len(slice); i++ {
if slice[i-1] < slice[i] {
@@ -855,13 +855,13 @@ func IsDescending[T constraints.Ordered](slice []T) bool {
}
// IsSorted checks if a slice is sorted(ascending or descending).
// Play: todo
// Play: https://go.dev/play/p/nCE8wPLwSA-
func IsSorted[T constraints.Ordered](slice []T) bool {
return IsAscending(slice) || IsDescending(slice)
}
// IsSortedByKey checks if a slice is sorted by iteratee function.
// Play: todo
// Play: https://go.dev/play/p/tUoGB7DOHI4
func IsSortedByKey[T any, K constraints.Ordered](slice []T, iteratee func(item T) K) bool {
size := len(slice)