diff --git a/docs/slice.md b/docs/slice.md index 03cb09a..f9fa91d 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -1,16 +1,17 @@ # Slice + Package slice implements some functions to manipulate slice.
## Source: -- [https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go) - +- [https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go)
## Usage: + ```go import ( "github.com/duke-git/lancet/v2/slice" @@ -20,59 +21,63 @@ import (
## Index -- [AppendIfAbsent](#AppendIfAbsent) -- [Contain](#Contain) -- [ContainSubSlice](#ContainSubSlice) -- [Chunk](#Chunk) -- [Compact](#Compact) -- [Concat](#Concat) -- [Count](#Count) -- [Difference](#Difference) -- [DifferenceBy](#DifferenceBy) -- [DifferenceWith](#DifferenceWith) -- [DeleteAt](#DeleteAt) -- [Drop](#Drop) -- [Equal](#Equal) -- [EqualWith](#EqualWith) -- [Every](#Every) -- [Filter](#Filter) -- [Find](#Find) -- [FindLast](#FindLast) -- [Flatten](#Flatten) -- [FlattenDeep](#FlattenDeep) -- [ForEach](#ForEach) -- [GroupBy](#GroupBy) -- [GroupWith](#GroupWith) -- [IntSlice](#IntSlice) -- [InterfaceSlice](#InterfaceSlice) -- [Intersection](#Intersection) -- [InsertAt](#InsertAt) -- [IndexOf](#IndexOf) -- [LastIndexOf](#LastIndexOf) -- [Map](#Map) -- [Reverse](#Reverse) -- [Reduce](#Reduce) -- [Replace](#Replace) -- [ReplaceAll](#ReplaceAll) -- [Shuffle](#Shuffle) -- [SortByField](#SortByField) -- [Some](#Some) -- [StringSlice](#StringSlice) -- [SymmetricDifference](#SymmetricDifference) -- [ToSlice](#ToSlice) -- [ToSlicePointer](#ToSlicePointer) -- [Unique](#Unique) -- [UniqueBy](#UniqueBy) -- [Union](#Union) -- [UpdateAt](#UpdateAt) -- [Without](#Without) +- [AppendIfAbsent](#AppendIfAbsent) +- [Contain](#Contain) +- [ContainSubSlice](#ContainSubSlice) +- [Chunk](#Chunk) +- [Compact](#Compact) +- [Concat](#Concat) +- [Count](#Count) +- [Difference](#Difference) +- [DifferenceBy](#DifferenceBy) +- [DifferenceWith](#DifferenceWith) +- [DeleteAt](#DeleteAt) +- [Drop](#Drop) +- [Equal](#Equal) +- [EqualWith](#EqualWith) +- [Every](#Every) +- [Filter](#Filter) +- [Find](#Find) +- [FindLast](#FindLast) +- [Flatten](#Flatten) +- [FlattenDeep](#FlattenDeep) +- [ForEach](#ForEach) + +- [GroupBy](#GroupBy) +- [GroupWith](#GroupWith) +- [IntSlice](#IntSlice) +- [InterfaceSlice](#InterfaceSlice) +- [Intersection](#Intersection) +- [InsertAt](#InsertAt) +- [IndexOf](#IndexOf) +- [LastIndexOf](#LastIndexOf) +- [Map](#Map) +- [Reverse](#Reverse) +- [Reduce](#Reduce) +- [Replace](#Replace) +- [ReplaceAll](#ReplaceAll) +- [Shuffle](#Shuffle) +- [SortByField](#SortByField) +- [Some](#Some) +- [StringSlice](#StringSlice) +- [SymmetricDifference](#SymmetricDifference) +- [ToSlice](#ToSlice) +- [ToSlicePointer](#ToSlicePointer) +- [Unique](#Unique) +- [UniqueBy](#UniqueBy) +- [Union](#Union) +- [UnionBy](#UnionBy) +- [UpdateAt](#UpdateAt) +- [Without](#Without) +- [KeyBy](#KeyBy)
## Documentation ### AppendIfAbsent +

If slice doesn't contain the value, append it to the slice.

Signature: @@ -80,6 +85,7 @@ import ( ```go func AppendIfAbsent[T comparable](slice []T, value T) []T ``` + Example: ```go @@ -98,8 +104,8 @@ func main() { } ``` - ### Contain +

Check if the value is in the slice or not.

Signature: @@ -107,6 +113,7 @@ func main() { ```go func Contain[T comparable](slice []T, value T) bool ``` + Example: ```go @@ -121,8 +128,8 @@ func main() { } ``` - ### ContainSubSlice +

Check if the slice contain subslice or not.

Signature: @@ -130,6 +137,7 @@ func main() { ```go func ContainSubSlice[T comparable](slice, subslice []T) bool ``` + Example: ```go @@ -144,10 +152,8 @@ func main() { } ``` - - - ### Chunk +

Creates an slice of elements split into groups the length of `size`.

Signature: @@ -155,6 +161,7 @@ func main() { ```go func Chunk[T any](slice []T, size int) [][]T ``` + Example: ```go @@ -170,9 +177,8 @@ func main() { } ``` - - ### Compact +

Creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey.

Signature: @@ -180,6 +186,7 @@ func main() { ```go func Compact[T any](slice []T) []T ``` + Example: ```go @@ -194,8 +201,8 @@ func main() { } ``` - ### Concat +

Creates a new slice concatenating slice with any additional slices and/or values.

Signature: @@ -203,6 +210,7 @@ func main() { ```go func Concat[T any](slice []T, values ...[]T) []T ``` + Example: ```go @@ -220,9 +228,8 @@ func main() { } ``` - - ### Count +

Count iterates over elements of slice, returns a count of all matched elements.

Signature: @@ -230,6 +237,7 @@ func main() { ```go func Count[T any](slice []T, predicate func(index int, t T) bool) int ``` + Example: ```go @@ -249,10 +257,8 @@ func main() { } ``` - - - ### Difference +

Creates an slice of whose element not included in the other given slice.

Signature: @@ -260,6 +266,7 @@ func main() { ```go func Difference[T comparable](slice, comparedSlice []T) []T ``` + Example: ```go @@ -277,10 +284,8 @@ func main() { } ``` - - - ### DifferenceBy +

DifferenceBy accepts iteratee func which is invoked for each element of slice and values to generate the criterion by which they're compared.

Signature: @@ -288,6 +293,7 @@ func main() { ```go func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T ``` + Example: ```go @@ -308,8 +314,8 @@ func main() { } ``` - ### DifferenceWith +

DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice.

Signature: @@ -317,6 +323,7 @@ func main() { ```go func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T ``` + Example: ```go @@ -338,6 +345,7 @@ func main() { ``` ### DeleteAt +

Delete the element of slice from start index to end index - 1.

Signature: @@ -345,6 +353,7 @@ func main() { ```go func DeleteAt[T any](slice []T, start int, end ...int) ``` + Example: ```go @@ -363,10 +372,8 @@ func main() { } ``` - - - ### Drop +

Creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0.

Signature: @@ -374,6 +381,7 @@ func main() { ```go func Drop[T any](slice []T, n int) []T ``` + Example: ```go @@ -394,9 +402,8 @@ func main() { } ``` - - ### Equal +

Check if two slices are equal: the same length and all elements' order and value are equal.

Signature: @@ -404,6 +411,7 @@ func main() { ```go func Equal[T comparable](slice1, slice2 []T) bool ``` + Example: ```go @@ -425,9 +433,8 @@ func main() { } ``` - - ### EqualWith +

Check if two slices are equal with comparator func.

Signature: @@ -435,6 +442,7 @@ func main() { ```go func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool ``` + Example: ```go @@ -457,9 +465,8 @@ func main() { } ``` - - ### Every +

Return true if all of the values in the slice pass the predicate function.

Signature: @@ -467,6 +474,7 @@ func main() { ```go func Every[T any](slice []T, predicate func(index int, item T) bool) bool ``` + Example: ```go @@ -486,10 +494,8 @@ func main() { } ``` - - - ### Filter +

Return all elements which match the function.

Signature: @@ -497,6 +503,7 @@ func main() { ```go func Filter[T any](slice []T, predicate func(index int, item T) bool) []T ``` + Example: ```go @@ -516,9 +523,8 @@ func main() { } ``` - - ### Find +

Iterates over elements of slice, returning the first one that passes a truth test on function.

Signature: @@ -526,6 +532,7 @@ func main() { ```go func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) ``` + Example: ```go @@ -546,10 +553,8 @@ func main() { } ``` - - - ### FindLast +

iterates over elements of slice from end to begin, returning the last one that passes a truth test on function.

Signature: @@ -557,6 +562,7 @@ func main() { ```go func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) ``` + Example: ```go @@ -577,9 +583,8 @@ func main() { } ``` - - ### Flatten +

Flatten slice with one level.

Signature: @@ -587,6 +592,7 @@ func main() { ```go func Flatten(slice any) any ``` + Example: ```go @@ -602,9 +608,8 @@ func main() { } ``` - - ### FlattenDeep +

flattens slice recursive.

Signature: @@ -612,6 +617,7 @@ func main() { ```go func FlattenDeep(slice any) any ``` + Example: ```go @@ -627,11 +633,8 @@ func main() { } ``` - - - - ### ForEach +

Iterates over elements of slice and invokes function for each element.

Signature: @@ -639,6 +642,7 @@ func main() { ```go func ForEach[T any](slice []T, iteratee func(index int, item T)) ``` + Example: ```go @@ -657,10 +661,8 @@ func main() { } ``` - - - ### GroupBy +

Iterates over elements of the slice, each element will be group by criteria, returns two slices.

Signature: @@ -668,6 +670,7 @@ func main() { ```go func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T) ``` + Example: ```go @@ -688,17 +691,16 @@ func main() { } ``` - - - ### GroupWith +

Return a map composed of keys generated from the results of running each element of slice thru iteratee.

Signature: ```go -func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T +func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T ``` + Example: ```go @@ -717,9 +719,8 @@ func main() { } ``` - - ### IntSlice +

Convert interface slice to int slice.

Signature: @@ -727,6 +728,7 @@ func main() { ```go func IntSlice(slice any) []int ``` + Example: ```go @@ -742,10 +744,8 @@ func main() { } ``` - - - ### InterfaceSlice +

Convert value to interface slice.

Signature: @@ -753,6 +753,7 @@ func main() { ```go func InterfaceSlice(slice any) []any ``` + Example: ```go @@ -768,10 +769,8 @@ func main() { } ``` - - - ### Intersection +

Creates a slice of unique values that included by all slices.

Signature: @@ -779,6 +778,7 @@ func main() { ```go func Intersection[T comparable](slices ...[]T) []T ``` + Example: ```go @@ -796,10 +796,8 @@ func main() { } ``` - - - ### InsertAt +

insert the element into slice at index.

Signature: @@ -807,6 +805,7 @@ func main() { ```go func InsertAt[T any](slice []T, index int, value any) []T ``` + Example: ```go @@ -817,7 +816,7 @@ import ( func main() { s := []string{"a", "b", "c"} - + res1, _ := slice.InsertAt(s, 0, "1") fmt.Println(res1) //[]string{"1", "a", "b", "c"} @@ -826,10 +825,8 @@ func main() { } ``` - - - ### IndexOf +

Returns the index at which the first occurrence of a value is found in a slice or return -1 if the value cannot be found.

Signature: @@ -837,6 +834,7 @@ func main() { ```go func IndexOf[T comparable](slice []T, value T) int ``` + Example: ```go @@ -855,9 +853,8 @@ func main() { } ``` - - ### LastIndexOf +

Returns the index at which the last occurrence of a value is found in a slice or return -1 if the value cannot be found.

Signature: @@ -865,6 +862,7 @@ func main() { ```go func LastIndexOf[T comparable](slice []T, value T) int ``` + Example: ```go @@ -883,10 +881,8 @@ func main() { } ``` - - - ### Map +

Creates an slice of values by running each element in slice thru function.

Signature: @@ -894,6 +890,7 @@ func main() { ```go func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U ``` + Example: ```go @@ -912,10 +909,8 @@ func main() { } ``` - - - ### Reverse +

Reverse the elements order in slice.

Signature: @@ -923,6 +918,7 @@ func main() { ```go func Reverse[T any](slice []T) ``` + Example: ```go @@ -938,9 +934,8 @@ func main() { } ``` - - ### Reduce +

Reduce slice.

Signature: @@ -948,6 +943,7 @@ func main() { ```go func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T ``` + Example: ```go @@ -966,9 +962,8 @@ func main() { } ``` - - ### Replace +

Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.

Signature: @@ -976,6 +971,7 @@ func main() { ```go func Replace[T comparable](slice []T, old T, new T, n int) []T ``` + Example: ```go @@ -995,9 +991,8 @@ func main() { } ``` - - ### ReplaceAll +

Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.

Signature: @@ -1005,6 +1000,7 @@ func main() { ```go func ReplaceAll[T comparable](slice []T, old T, new T) []T ``` + Example: ```go @@ -1022,8 +1018,8 @@ func main() { } ``` - ### Shuffle +

Creates an slice of shuffled values.

Signature: @@ -1031,6 +1027,7 @@ func main() { ```go func Shuffle[T any](slice []T) []T ``` + Example: ```go @@ -1046,9 +1043,8 @@ func main() { } ``` - - ### SortByField +

Sort struct slice by field. Slice element should be struct, field type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set sortType to desc

Signature: @@ -1056,6 +1052,7 @@ func main() { ```go func SortByField(slice any, field string, sortType ...string) error ``` + Example: ```go @@ -1079,7 +1076,7 @@ func main() { if err != nil { fmt.Println(err) } - fmt.Println(students) + fmt.Println(students) // []students{ // {"b", 15}, // {"a", 10}, @@ -1089,9 +1086,8 @@ func main() { } ``` - - ### Some +

Return true if any of the values in the list pass the predicate function.

Signature: @@ -1099,6 +1095,7 @@ func main() { ```go func Some[T any](slice []T, predicate func(index int, item T) bool) bool ``` + Example: ```go @@ -1118,9 +1115,8 @@ func main() { } ``` - - ### StringSlice +

Convert interface slice to string slice.

Signature: @@ -1128,6 +1124,7 @@ func main() { ```go func StringSlice(slice any) []string ``` + Example: ```go @@ -1143,10 +1140,8 @@ func main() { } ``` - - - ### SymmetricDifference +

Create a slice whose element is in given slices, but not in both slices.

Signature: @@ -1154,6 +1149,7 @@ func main() { ```go func SymmetricDifference[T comparable](slices ...[]T) []T ``` + Example: ```go @@ -1173,9 +1169,8 @@ func main() { } ``` - - ### ToSlice +

Returns a slices of a variable parameter transformation

Signature: @@ -1183,6 +1178,7 @@ func main() { ```go func ToSlice[T any](value ...T) []T ``` + Example: ```go @@ -1197,9 +1193,8 @@ func main() { } ``` - - ### ToSlicePointer +

Returns a pointer to the slices of a variable parameter transformation

Signature: @@ -1207,6 +1202,7 @@ func main() { ```go func ToSlicePointer[T any](value ...T) []*T ``` + Example: ```go @@ -1223,8 +1219,8 @@ func main() { } ``` - ### Unique +

Remove duplicate elements in slice.

Signature: @@ -1232,6 +1228,7 @@ func main() { ```go func Unique[T comparable](slice []T) []T ``` + Example: ```go @@ -1246,9 +1243,8 @@ func main() { } ``` - - ### UniqueBy +

Call iteratee func with every item of slice, then remove duplicated.

Signature: @@ -1256,6 +1252,7 @@ func main() { ```go func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T ``` + Example: ```go @@ -1272,9 +1269,8 @@ func main() { } ``` - - ### Union +

Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.

Signature: @@ -1282,6 +1278,7 @@ func main() { ```go func Union[T comparable](slices ...[]T) []T ``` + Example: ```go @@ -1299,9 +1296,35 @@ func main() { } ``` +### UnionBy +

UnionBy is like Union, what's more it accepts iteratee which is invoked for each element of each slice.

+ +Signature: + +```go +func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + testFunc := func(i int) int { + return i / 2 + } + result := slice.UnionBy(testFunc, []int{0, 1, 2, 3, 4, 5}, []int{0, 2, 10}) + fmt.Println(result) //[]int{0, 2, 4, 10} +} +``` ### UpdateAt +

Update the slice element at index. if param index < 0 or index >= len(slice), will return error.

Signature: @@ -1309,6 +1332,7 @@ func main() { ```go func UpdateAt[T any](slice []T, index int, value T) []T ``` + Example: ```go @@ -1319,16 +1343,14 @@ import ( func main() { s := []string{"a", "b", "c"} - + res1, _ := slice.UpdateAt(s, 0, "1") fmt.Println(res1) //[]string{"1", "b", "c"} } ``` - - - ### Without +

Creates a slice excluding all given values.

Signature: @@ -1336,6 +1358,7 @@ func main() { ```go func Without[T comparable](slice []T, values ...T) []T ``` + Example: ```go @@ -1350,13 +1373,29 @@ func main() { } ``` +### KeyBy +

Converts a slice to a map based on a callback function.

+Signature: +```go +func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T +``` +Example: +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) +func main() { + res := slice.KeyBy([]string{"a", "ab", "abc"}, func(str string) int { + return len(str) + }) - - - + fmt.Println(res) //map[int]string{1: "a", 2: "ab", 3: "abc"} +} +``` diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index be544a3..873cdfb 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -1,16 +1,17 @@ # Slice -slice包包含操作切片的方法集合。 + +slice 包包含操作切片的方法集合。
## 源码: -- [https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go) - +- [https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go)
## 用法: + ```go import ( "github.com/duke-git/lancet/v2/slice" @@ -20,59 +21,63 @@ import (
## 目录 -- [AppendIfAbsent](#AppendIfAbsent) -- [Contain](#Contain) -- [ContainSubSlice](#ContainSubSlice) -- [Chunk](#Chunk) -- [Compact](#Compact) -- [Concat](#Concat) -- [Count](#Count) -- [Difference](#Difference) -- [DifferenceBy](#DifferenceBy) -- [DifferenceWith](#DifferenceWith) -- [DeleteAt](#DeleteAt) -- [Drop](#Drop) -- [Every](#Every) -- [Equal](#Equal) -- [EqualWith](#EqualWith) -- [Filter](#Filter) -- [Find](#Find) -- [FindLast](#FindLast) -- [Flatten](#Flatten) -- [FlattenDeep](#FlattenDeep) -- [ForEach](#ForEach) - -- [GroupBy](#GroupBy) -- [GroupWith](#GroupWith) -- [IntSlice](#IntSlice) -- [InterfaceSlice](#InterfaceSlice) -- [Intersection](#Intersection) -- [InsertAt](#InsertAt) -- [IndexOf](#IndexOf) -- [LastIndexOf](#LastIndexOf) -- [Map](#Map) -- [Reverse](#Reverse) -- [Reduce](#Reduce) -- [Replace](#Replace) -- [ReplaceAll](#ReplaceAll) -- [Shuffle](#Shuffle) -- [SortByField](#SortByField) -- [Some](#Some) -- [StringSlice](#StringSlice) -- [SymmetricDifference](#SymmetricDifference) -- [ToSlice](#ToSlice) -- [ToSlicePointer](#ToSlicePointer) -- [Unique](#Unique) -- [UniqueBy](#UniqueBy) -- [Union](#Union) -- [UpdateAt](#UpdateAt) -- [Without](#Without) + +- [AppendIfAbsent](#AppendIfAbsent) +- [Contain](#Contain) +- [ContainSubSlice](#ContainSubSlice) +- [Chunk](#Chunk) +- [Compact](#Compact) +- [Concat](#Concat) +- [Count](#Count) +- [Difference](#Difference) +- [DifferenceBy](#DifferenceBy) +- [DifferenceWith](#DifferenceWith) +- [DeleteAt](#DeleteAt) +- [Drop](#Drop) +- [Every](#Every) +- [Equal](#Equal) +- [EqualWith](#EqualWith) +- [Filter](#Filter) +- [Find](#Find) +- [FindLast](#FindLast) +- [Flatten](#Flatten) +- [FlattenDeep](#FlattenDeep) +- [ForEach](#ForEach) +- [GroupBy](#GroupBy) +- [GroupWith](#GroupWith) +- [IntSlice](#IntSlice) + +- [InterfaceSlice](#InterfaceSlice) +- [Intersection](#Intersection) +- [InsertAt](#InsertAt) +- [IndexOf](#IndexOf) +- [LastIndexOf](#LastIndexOf) +- [Map](#Map) +- [Reverse](#Reverse) +- [Reduce](#Reduce) +- [Replace](#Replace) +- [ReplaceAll](#ReplaceAll) +- [Shuffle](#Shuffle) +- [SortByField](#SortByField) +- [Some](#Some) +- [StringSlice](#StringSlice) +- [SymmetricDifference](#SymmetricDifference) +- [ToSlice](#ToSlice) +- [ToSlicePointer](#ToSlicePointer) +- [Unique](#Unique) +- [UniqueBy](#UniqueBy) +- [Union](#Union) +- [UnionBy](#UnionBy) +- [UpdateAt](#UpdateAt) +- [Without](#Without) +- [KeyBy](#KeyBy)
## 文档 ### AppendIfAbsent +

当前切片中不包含值时,将该值追加到切片中

函数签名: @@ -80,6 +85,7 @@ import ( ```go func AppendIfAbsent[T comparable](slice []T, value T) []T ``` + 例子: ```go @@ -98,9 +104,8 @@ func main() { } ``` - - ### Contain +

判断slice是否包含value

函数签名: @@ -108,6 +113,7 @@ func main() { ```go func Contain[T comparable](slice []T, value T) bool ``` + 例子: ```go @@ -122,8 +128,8 @@ func main() { } ``` - ### ContainSubSlice +

判断slice是否包含subslice

函数签名: @@ -131,6 +137,7 @@ func main() { ```go func ContainSubSlice[T comparable](slice, subslice []T) bool ``` + 例子: ```go @@ -145,10 +152,8 @@ func main() { } ``` - - - ### Chunk +

按照size参数均分slice

函数签名: @@ -156,6 +161,7 @@ func main() { ```go func Chunk[T any](slice []T, size int) [][]T ``` + 例子: ```go @@ -171,9 +177,8 @@ func main() { } ``` - - ### Compact +

去除slice中的假值(false values are false, nil, 0, "")

函数签名: @@ -181,6 +186,7 @@ func main() { ```go func Compact[T any](slice []T) []T ``` + 例子: ```go @@ -195,8 +201,8 @@ func main() { } ``` - ### Concat +

连接values到slice中,values类型可以是切片或多个值

函数签名: @@ -204,6 +210,7 @@ func main() { ```go func Concat[T any](slice []T, values ...[]T) []T ``` + 例子: ```go @@ -221,9 +228,8 @@ func main() { } ``` - - ### Count +

遍历切片,对每个元素执行函数function. 返回符合函数返回值为true的元素的个数

函数签名: @@ -231,6 +237,7 @@ func main() { ```go func Count[T any](slice []T, predicate func(index int, t T) bool) int ``` + 例子: ```go @@ -250,10 +257,8 @@ func main() { } ``` - - - ### Difference +

创建一个切片,其元素不包含在另一个给定切片中

函数签名: @@ -261,6 +266,7 @@ func main() { ```go func Difference[T comparable](slice, comparedSlice []T) []T ``` + 例子: ```go @@ -278,10 +284,8 @@ func main() { } ``` - - - ### DifferenceBy +

在slice和comparedSlice中的每个元素调用iteratee函数,并比较它们的返回值,如果不想等返回在slice中对应的值

函数签名: @@ -289,6 +293,7 @@ func main() { ```go func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T ``` + 例子: ```go @@ -309,9 +314,8 @@ func main() { } ``` - - ### DifferenceWith +

DifferenceWith 接受比较器,该比较器被调用以将切片的元素与值进行比较。 结果值的顺序和引用由第一个切片确定

函数签名: @@ -319,6 +323,7 @@ func main() { ```go func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T ``` + 例子: ```go @@ -339,8 +344,8 @@ func main() { } ``` - ### DeleteAt +

删除切片中从开始索引到结束索引-1的元素

函数签名: @@ -348,6 +353,7 @@ func main() { ```go func DeleteAt[T any](slice []T, start int, end ...int) ``` + 例子: ```go @@ -366,10 +372,8 @@ func main() { } ``` - - - ### Drop +

创建一个切片,当 n > 0 时从开头删除 n 个元素,或者当 n < 0 时从结尾删除 n 个元素

函数签名: @@ -377,6 +381,7 @@ func main() { ```go func Drop[T any](slice []T, n int) []T ``` + 例子: ```go @@ -397,10 +402,8 @@ func main() { } ``` - - - ### Every +

如果切片中的所有值都通过谓词函数,则返回true。 函数签名应该是func(index int, value any) bool

函数签名: @@ -408,6 +411,7 @@ func main() { ```go func Every[T any](slice []T, predicate func(index int, item T) bool) bool ``` + 例子: ```go @@ -427,10 +431,8 @@ func main() { } ``` - - - ### Equal +

检查两个切片是否相等,相等条件:切片长度相同,元素顺序和值都相同

函数签名: @@ -438,6 +440,7 @@ func main() { ```go func Equal[T comparable](slice1, slice2 []T) bool ``` + 例子: ```go @@ -459,9 +462,8 @@ func main() { } ``` - - ### EqualWith +

检查两个切片是否相等,相等条件:对两个切片的元素调用比较函数comparator,返回true

函数签名: @@ -469,6 +471,7 @@ func main() { ```go func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool ``` + 例子: ```go @@ -491,9 +494,8 @@ func main() { } ``` - - ### Filter +

返回切片中通过predicate函数真值测试的所有元素

函数签名: @@ -501,6 +503,7 @@ func main() { ```go func Filter[T any](slice []T, predicate func(index int, item T) bool) []T ``` + 例子: ```go @@ -520,9 +523,8 @@ func main() { } ``` - - ### Find +

遍历切片的元素,返回第一个通过predicate函数真值测试的元素

函数签名: @@ -530,6 +532,7 @@ func main() { ```go func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) ``` + 例子: ```go @@ -550,10 +553,8 @@ func main() { } ``` - - - ### FindLast +

从头到尾遍历slice的元素,返回最后一个通过predicate函数真值测试的元素。

函数签名: @@ -561,6 +562,7 @@ func main() { ```go func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) ``` + 例子: ```go @@ -581,8 +583,8 @@ func main() { } ``` - ### Flatten +

将切片压平一层

函数签名: @@ -590,6 +592,7 @@ func main() { ```go func Flatten(slice any) any ``` + 例子: ```go @@ -605,9 +608,8 @@ func main() { } ``` - - ### FlattenDeep +

flattens slice recursive.

函数签名: @@ -615,6 +617,7 @@ func main() { ```go func FlattenDeep(slice any) any ``` + 例子: ```go @@ -630,11 +633,8 @@ func main() { } ``` - - - - ### ForEach +

遍历切片的元素并为每个元素调用iteratee函数

函数签名: @@ -642,6 +642,7 @@ func main() { ```go func ForEach[T any](slice []T, iteratee func(index int, item T)) ``` + 例子: ```go @@ -660,10 +661,8 @@ func main() { } ``` - - - ### GroupBy +

迭代切片的元素,每个元素将按条件分组,返回两个切片

函数签名: @@ -671,6 +670,7 @@ func main() { ```go func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T) ``` + 例子: ```go @@ -691,16 +691,16 @@ func main() { } ``` - - ### GroupWith +

创建一个map,key是iteratee遍历slice中的每个元素返回的结果。 分组值的顺序是由他们出现在slice中的顺序确定的。每个键对应的值负责生成key的元素组成的数组。iteratee调用1个参数: (value)

函数签名: ```go -func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T +func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T ``` + 例子: ```go @@ -719,8 +719,8 @@ func main() { } ``` - ### IntSlice +

将接口切片转换为int切片

函数签名: @@ -728,6 +728,7 @@ func main() { ```go func IntSlice(slice any) []int ``` + 例子: ```go @@ -743,10 +744,8 @@ func main() { } ``` - - - ### InterfaceSlice +

将值转换为接口切片

函数签名: @@ -754,6 +753,7 @@ func main() { ```go func InterfaceSlice(slice any) []any ``` + 例子: ```go @@ -769,10 +769,8 @@ func main() { } ``` - - - ### Intersection +

多个切片的交集

函数签名: @@ -780,6 +778,7 @@ func main() { ```go func Intersection[T comparable](slices ...[]T) []T ``` + 例子: ```go @@ -797,10 +796,8 @@ func main() { } ``` - - - ### InsertAt +

将元素插入到索引处的切片中

函数签名: @@ -808,6 +805,7 @@ func main() { ```go func InsertAt[T any](slice []T, index int, value any) []T ``` + 例子: ```go @@ -818,7 +816,7 @@ import ( func main() { s := []string{"a", "b", "c"} - + res1, _ := slice.InsertAt(s, 0, "1") fmt.Println(res1) //[]string{"1", "a", "b", "c"} @@ -827,10 +825,8 @@ func main() { } ``` - - - ### IndexOf +

返回在切片中找到值的第一个匹配项的索引,如果找不到值,则返回-1

函数签名: @@ -838,6 +834,7 @@ func main() { ```go func IndexOf[T comparable](slice []T, value T) int ``` + 例子: ```go @@ -856,9 +853,8 @@ func main() { } ``` - - ### LastIndexOf +

返回在切片中找到最后一个值的索引,如果找不到该值,则返回-1

函数签名: @@ -866,6 +862,7 @@ func main() { ```go func LastIndexOf[T comparable](slice []T, value T) int ``` + 例子: ```go @@ -884,9 +881,8 @@ func main() { } ``` - - ### Map +

通过运行函数slice中的每个元素来创建一个新切片

函数签名: @@ -894,6 +890,7 @@ func main() { ```go func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U ``` + 例子: ```go @@ -912,10 +909,8 @@ func main() { } ``` - - - ### Reverse +

反转切片中的元素顺序

函数签名: @@ -923,6 +918,7 @@ func main() { ```go func Reverse[T any](slice []T) ``` + 例子: ```go @@ -938,9 +934,8 @@ func main() { } ``` - - ### Reduce +

将切片中的元素依次运行iteratee函数,返回运行结果

函数签名: @@ -948,6 +943,7 @@ func main() { ```go func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T ``` + 例子: ```go @@ -966,9 +962,8 @@ func main() { } ``` - - ### Replace +

返回切片的副本,其中前n个不重叠的old替换为new

函数签名: @@ -976,6 +971,7 @@ func main() { ```go func Replace[T comparable](slice []T, old T, new T, n int) []T ``` + 例子: ```go @@ -995,9 +991,8 @@ func main() { } ``` - - ### ReplaceAll +

返回切片的副本,将其中old全部替换为new

函数签名: @@ -1005,6 +1000,7 @@ func main() { ```go func ReplaceAll[T comparable](slice []T, old T, new T) []T ``` + 例子: ```go @@ -1022,9 +1018,8 @@ func main() { } ``` - - ### Shuffle +

随机打乱切片中的元素顺序

函数签名: @@ -1032,6 +1027,7 @@ func main() { ```go func Shuffle[T any](slice []T) []T ``` + 例子: ```go @@ -1047,9 +1043,8 @@ func main() { } ``` - - ### SortByField +

按字段对结构切片进行排序。slice元素应为struct,字段类型应为int、uint、string或bool。 默认排序类型是升序(asc),如果是降序,设置 sortType 为 desc

函数签名: @@ -1057,6 +1052,7 @@ func main() { ```go func SortByField(slice any, field string, sortType ...string) error ``` + 例子: ```go @@ -1080,7 +1076,7 @@ func main() { if err != nil { fmt.Println(err) } - fmt.Println(students) + fmt.Println(students) // []students{ // {"b", 15}, // {"a", 10}, @@ -1090,9 +1086,8 @@ func main() { } ``` - - ### Some +

如果列表中的任何值通过谓词函数,则返回true

函数签名: @@ -1100,6 +1095,7 @@ func main() { ```go func Some[T any](slice []T, predicate func(index int, item T) bool) bool ``` + 例子: ```go @@ -1119,9 +1115,8 @@ func main() { } ``` - - ### StringSlice +

将接口切片转换为字符串切片

函数签名: @@ -1129,6 +1124,7 @@ func main() { ```go func StringSlice(slice any) []string ``` + 例子: ```go @@ -1144,10 +1140,8 @@ func main() { } ``` - - - ### SymmetricDifference +

返回一个切片,其中的元素存在于参数切片中,但不同时存储在于参数切片中(交集取反)

函数签名: @@ -1155,6 +1149,7 @@ func main() { ```go func SymmetricDifference[T comparable](slices ...[]T) []T ``` + 例子: ```go @@ -1174,8 +1169,8 @@ func main() { } ``` - ### ToSlice +

将可变参数转为切片

函数签名: @@ -1183,6 +1178,7 @@ func main() { ```go func ToSlice[T any](value ...T) []T ``` + 例子: ```go @@ -1197,9 +1193,8 @@ func main() { } ``` - - ### ToSlicePointer +

将可变参数转为指针切片

函数签名: @@ -1207,6 +1202,7 @@ func main() { ```go func ToSlicePointer[T any](value ...T) []*T ``` + 例子: ```go @@ -1223,9 +1219,8 @@ func main() { } ``` - - ### Unique +

删除切片中的重复元素

函数签名: @@ -1233,6 +1228,7 @@ func main() { ```go func Unique[T comparable](slice []T) []T ``` + 例子: ```go @@ -1247,9 +1243,8 @@ func main() { } ``` - - ### UniqueBy +

对切片的每个元素调用iteratee函数,然后删除重复元素

函数签名: @@ -1257,6 +1252,7 @@ func main() { ```go func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T ``` + 例子: ```go @@ -1273,16 +1269,16 @@ func main() { } ``` - - ### Union -

从所有给定的切片按顺序创建一个唯一值切片,使用==进行相等比较

+ +

合并多个切片.

函数签名: ```go func Union[T comparable](slices ...[]T) []T ``` + 例子: ```go @@ -1300,9 +1296,35 @@ func main() { } ``` +### UnionBy +

对切片的每个元素调用函数后,合并多个切片

+ +函数签名: + +```go +func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T +``` + +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + testFunc := func(i int) int { + return i / 2 + } + result := slice.UnionBy(testFunc, []int{0, 1, 2, 3, 4, 5}, []int{0, 2, 10}) + fmt.Println(result) //[]int{0, 2, 4, 10} +} +``` ### UpdateAt +

更新索引处的切片元素。 如果index < 0或 index >= len(slice),将返回错误

函数签名: @@ -1310,6 +1332,7 @@ func main() { ```go func UpdateAt[T any](slice []T, index int, value T) []T ``` + 例子: ```go @@ -1320,16 +1343,14 @@ import ( func main() { s := []string{"a", "b", "c"} - + res1, _ := slice.UpdateAt(s, 0, "1") fmt.Println(res1) //[]string{"1", "b", "c"} } ``` - - - ### Without +

创建一个不包括所有给定值的切片

函数签名: @@ -1337,6 +1358,7 @@ func main() { ```go func Without[T comparable](slice []T, values ...T) []T ``` + 例子: ```go @@ -1352,12 +1374,29 @@ func main() { ``` +### KeyBy +

将切片每个元素调用函数后转为map

+函数签名: +```go +func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T +``` +例子: +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) +func main() { + res := slice.KeyBy([]string{"a", "ab", "abc"}, func(str string) int { + return len(str) + }) - - + fmt.Println(res) //map[int]string{1: "a", 2: "ab", 3: "abc"} +} +```