From 2e8834a2c178fd5e3839e1cbea65bfc266f77f7e Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 18 Feb 2022 11:05:14 +0800 Subject: [PATCH] docs: update slice.doc --- docs/slice.md | 107 +++++++++++++++++++++----------------------- docs/slice_zh-CN.md | 98 ++++++++++++++++++++-------------------- 2 files changed, 101 insertions(+), 104 deletions(-) diff --git a/docs/slice.md b/docs/slice.md index 27cac1a..0192883 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -28,7 +28,7 @@ import ( - [Count](#Count) - [Difference](#Difference) - [DifferenceBy](#DifferenceBy) -- [DeleteByIndex](#DeleteByIndex) +- [DeleteAt](#DeleteAt) - [Drop](#Drop) - [Every](#Every) - [Filter](#Filter) @@ -41,9 +41,9 @@ import ( - [IntSlice](#IntSlice) - [InterfaceSlice](#InterfaceSlice) - [Intersection](#Intersection) -- [InsertByIndex](#InsertByIndex) +- [InsertAt](#InsertAt) - [Map](#Map) -- [ReverseSlice](#ReverseSlice) +- [Reverse](#Reverse) - [Reduce](#Reduce) - [Shuffle](#Shuffle) - [SortByField](#SortByField) @@ -51,23 +51,20 @@ import ( - [StringSlice](#StringSlice) - [Unique](#Unique) - [Union](#Union) -- [UpdateByIndex](#UpdateByIndex) +- [UpdateAt](#UpdateAt) - [Without](#Without)
## Documentation -## Note: -1. param which type is interface{} in below functions should be slice. - ### Contain -

Check if the value is in the slice or not. iterableType param can be string, map or slice.

+

Check if the value is in the slice or not.

Signature: ```go -func Contain(iterableType interface{}, value interface{}) bool +func Contain[T any](slice []T, value T) bool ``` Example: @@ -90,7 +87,7 @@ func main() { Signature: ```go -func ContainSubSlice(slice interface{}, subslice interface{}) bool +func ContainSubSlice[T any](slice, subslice []T) bool ``` Example: @@ -115,7 +112,7 @@ func main() { Signature: ```go -func Chunk(slice []interface{}, size int) [][]interface{} +func Chunk[T any](slice []T, size int) [][]T ``` Example: @@ -140,7 +137,7 @@ func main() { Signature: ```go -func Compact(slice interface{}) interface{} +func Compact[T any](slice []T) []T ``` Example: @@ -163,7 +160,7 @@ func main() { Signature: ```go -func Concat(slice interface{}, values ...interface{}) interface{} +func Concat[T any](slice []T, values ...[]T) []T ``` Example: @@ -185,12 +182,12 @@ func main() { ### Count -

Count iterates over elements of slice, returns a count of all matched elements. The function signature should be func(index int, value interface{}) bool.

+

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

Signature: ```go -func Count(slice, function interface{}) int +func Count[T any](slice []T, predicate func(index int, t T) bool) int ``` Example: @@ -220,7 +217,7 @@ func main() { Signature: ```go -func Difference(slice1, slice2 interface{}) interface{} +func Difference[T comparable](slice, comparedSlice []T) []T ``` Example: @@ -248,7 +245,7 @@ func main() { Signature: ```go -func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{} +func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T ``` Example: @@ -273,13 +270,13 @@ func main() { -### DeleteByIndex +### DeleteAt

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

Signature: ```go -func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) +func DeleteAt[T any](slice []T, start int, end ...int) ``` Example: @@ -290,10 +287,10 @@ import ( ) func main() { - res1 := slice.DeleteByIndex([]string{"a", "b", "c", "d", "e"}, 3) + res1 := slice.DeleteAt([]string{"a", "b", "c", "d", "e"}, 3) fmt.Println(res1) //[]string{"a", "b", "c", "e"} - res2 := slice.DeleteByIndex([]string{"a", "b", "c", "d", "e"}, 0, 2) + res2 := slice.DeleteAt([]string{"a", "b", "c", "d", "e"}, 0, 2) fmt.Println(res2) //[]string{"c", "d", "e"} } @@ -308,7 +305,7 @@ func main() { Signature: ```go -func Drop(slice interface{}, n int) interface{} +func Drop[T any](slice []T, n int) []T ``` Example: @@ -334,12 +331,12 @@ func main() { ### Every -

Return true if all of the values in the slice pass the predicate function. The function signature should be func(index int, value interface{}) bool.

+

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

Signature: ```go -func Every(slice, function interface{}) bool +func Every[T any](slice []T, predicate func(index int, t T) bool) bool ``` Example: @@ -364,12 +361,12 @@ func main() { ### Filter -

Return all elements which match the function. Function signature should be func(index int, value interface{}) bool.

+

Return all elements which match the function.

Signature: ```go -func Filter(slice, function interface{}) interface{} +func Filter[T any](slice []T, predicate func(index int, t T) bool) []T ``` Example: @@ -393,12 +390,12 @@ func main() { ### Find -

Iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool.

+

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

Signature: ```go -func Find(slice, function interface{}) (interface{}, bool) +func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) ``` Example: @@ -424,12 +421,12 @@ func main() { ### FindLast -

iterates over elements of slice from end to begin, returning the last one that passes a truth test on function. The function signature should be func(index int, value interface{}) bool.

+

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

Signature: ```go -func FindLast(slice, function interface{}) (interface{}, bool) +func FindLast[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) ``` Example: @@ -481,12 +478,12 @@ func main() { ### ForEach -

Iterates over elements of slice and invokes function for each element, function signature should be func(index int, value interface{}).

+

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

Signature: ```go -func ForEach(slice, function interface{}) +func ForEach[T any](slice []T, iteratee func(index int, t T)) ``` Example: @@ -510,12 +507,12 @@ func main() { ### GroupBy -

Iterates over elements of the slice, each element will be group by criteria, returns two slices. The function signature should be func(index int, value interface{}) bool.

+

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

Signature: ```go -func GroupBy(slice, function interface{}) (interface{}, interface{}) +func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) ``` Example: @@ -598,7 +595,7 @@ func main() { Signature: ```go -func Intersection(slices ...interface{}) interface{} +func Intersection[T any](slices ...[]T) []T ``` Example: @@ -620,13 +617,13 @@ func main() { -### InsertByIndex +### InsertAt

insert the element into slice at index.

Signature: ```go -func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error) +func InsertAt[T any](slice []T, index int, value interface{}) []T ``` Example: @@ -639,10 +636,10 @@ import ( func main() { s := []string{"a", "b", "c"} - res1, _ := slice.InsertByIndex(s, 0, "1") + res1, _ := slice.InsertAt(s, 0, "1") fmt.Println(res1) //[]string{"1", "a", "b", "c"} - res2, _ := slice.InsertByIndex(s, 3, []string{"1", "2", "3"}) + res2, _ := slice.InsertAt(s, 3, []string{"1", "2", "3"}) fmt.Println(res2) //[]string{"a", "b", "c", "1", "2", "3"} } ``` @@ -651,12 +648,12 @@ func main() { ### Map -

Creates an slice of values by running each element in slice thru function, function signature should be func(index int, value interface{}) interface{}.

+

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

Signature: ```go -func Map(slice, function interface{}) interface{} +func Map[T any, U any](slice []T, iteratee func(index int, t T) U) []U ``` Example: @@ -679,13 +676,13 @@ func main() { -### ReverseSlice +### Reverse

Reverse the elements order in slice.

Signature: ```go -func ReverseSlice(slice interface{}) +func Reverse[T any](slice []T) ``` Example: @@ -697,7 +694,7 @@ import ( func main() { nums := []int{1, 2, 3, 4} - slice.ReverseSlice(nums) + slice.Reverse(nums) fmt.Println(res) //[]int{4, 3, 2, 1} } ``` @@ -705,12 +702,12 @@ func main() { ### Reduce -

Reduce slice, function signature should be func(index int, value1, value2 interface{}) interface{}.

+

Reduce slice.

Signature: ```go -func Reduce(slice, function, zero interface{}) interface{} +func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T ``` Example: @@ -739,7 +736,7 @@ func main() { Signature: ```go -func Shuffle(slice interface{}) interface{} +func Shuffle[T any](slice []T) []T ``` Example: @@ -802,12 +799,12 @@ func main() { ### Some -

Return true if any of the values in the list pass the predicate function, function signature should be func(index int, value interface{}) bool.

+

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

Signature: ```go -func Some(slice, function interface{}) bool +func Some[T any](slice []T, predicate func(index int, t T) bool) bool ``` Example: @@ -862,7 +859,7 @@ func main() { Signature: ```go -func Unique(slice interface{}) interface{} +func Unique[T any](slice []T) []T ``` Example: @@ -886,7 +883,7 @@ func main() { Signature: ```go -func Union(slices ...interface{}) interface{} +func Union[T any](slices ...[]T) []T ``` Example: @@ -907,13 +904,13 @@ func main() { -### UpdateByIndex +### UpdateAt

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

Signature: ```go -func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) +func UpdateAt[T any](slice []T, index int, value T) []T ``` Example: @@ -926,7 +923,7 @@ import ( func main() { s := []string{"a", "b", "c"} - res1, _ := slice.UpdateByIndex(s, 0, "1") + res1, _ := slice.UpdateAt(s, 0, "1") fmt.Println(res1) //[]string{"1", "b", "c"} } ``` @@ -940,7 +937,7 @@ func main() { Signature: ```go -func Without(slice interface{}, values ...interface{}) interface{} +func Without[T any](slice []T, values ...T) []T ``` Example: diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 18bab8d..062859d 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -28,7 +28,7 @@ import ( - [Count](#Count) - [Difference](#Difference) - [DifferenceBy](#DifferenceBy) -- [DeleteByIndex](#DeleteByIndex) +- [DeleteAt](#DeleteAt) - [Drop](#Drop) - [Every](#Every) - [Filter](#Filter) @@ -41,9 +41,9 @@ import ( - [IntSlice](#IntSlice) - [InterfaceSlice](#InterfaceSlice) - [Intersection](#Intersection) -- [InsertByIndex](#InsertByIndex) +- [InsertAt](#InsertAt) - [Map](#Map) -- [ReverseSlice](#ReverseSlice) +- [Reverse](#Reverse) - [Reduce](#Reduce) - [Shuffle](#Shuffle) - [SortByField](#SortByField) @@ -51,7 +51,7 @@ import ( - [StringSlice](#StringSlice) - [Unique](#Unique) - [Union](#Union) -- [UpdateByIndex](#UpdateByIndex) +- [UpdateAt](#UpdateAt) - [Without](#Without)
@@ -64,7 +64,7 @@ import ( 函数签名: ```go -func Contain(iterableType interface{}, value interface{}) bool +func Contain[T any](slice []T, value T) bool ``` 例子: @@ -87,7 +87,7 @@ func main() { 函数签名: ```go -func ContainSubSlice(slice interface{}, subslice interface{}) bool +func ContainSubSlice[T any](slice, subslice []T) bool ``` 例子: @@ -112,7 +112,7 @@ func main() { 函数签名: ```go -func Chunk(slice []interface{}, size int) [][]interface{} +func Chunk[T any](slice []T, size int) [][]T ``` 例子: @@ -137,7 +137,7 @@ func main() { 函数签名: ```go -func Compact(slice interface{}) interface{} +func Compact[T any](slice []T) []T ``` 例子: @@ -160,7 +160,7 @@ func main() { 函数签名: ```go -func Concat(slice interface{}, values ...interface{}) interface{} +func Concat[T any](slice []T, values ...[]T) []T ``` 例子: @@ -182,12 +182,12 @@ func main() { ### Count -

遍历切片,对每个元素执行函数function. 返回符合函数返回值为true的元素的个数,函数签名必须是func(index int, value interface{}) bool

+

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

函数签名: ```go -func Count(slice, function interface{}) int +func Count[T any](slice []T, predicate func(index int, t T) bool) int ``` 例子: @@ -217,7 +217,7 @@ func main() { 函数签名: ```go -func Difference(slice1, slice2 interface{}) interface{} +func Difference[T comparable](slice, comparedSlice []T) []T ``` 例子: @@ -245,7 +245,7 @@ func main() { 函数签名: ```go -func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{} +func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T ``` 例子: @@ -270,13 +270,13 @@ func main() { -### DeleteByIndex +### DeleteAt

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

函数签名: ```go -func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) +func DeleteAt[T any](slice []T, start int, end ...int) ``` 例子: @@ -287,10 +287,10 @@ import ( ) func main() { - res1 := slice.DeleteByIndex([]string{"a", "b", "c", "d", "e"}, 3) + res1 := slice.DeleteAt([]string{"a", "b", "c", "d", "e"}, 3) fmt.Println(res1) //[]string{"a", "b", "c", "e"} - res2 := slice.DeleteByIndex([]string{"a", "b", "c", "d", "e"}, 0, 2) + res2 := slice.DeleteAt([]string{"a", "b", "c", "d", "e"}, 0, 2) fmt.Println(res2) //[]string{"c", "d", "e"} } @@ -305,7 +305,7 @@ func main() { 函数签名: ```go -func Drop(slice interface{}, n int) interface{} +func Drop[T any](slice []T, n int) []T ``` 例子: @@ -336,7 +336,7 @@ func main() { 函数签名: ```go -func Every(slice, function interface{}) bool +func Every[T any](slice []T, predicate func(index int, t T) bool) bool ``` 例子: @@ -366,7 +366,7 @@ func main() { 函数签名: ```go -func Filter(slice, function interface{}) interface{} +func Filter[T any](slice []T, predicate func(index int, t T) bool) []T ``` 例子: @@ -390,12 +390,12 @@ func main() { ### Find -

遍历slice的元素,返回第一个通过function真值测试的元素。函数签名应该是 func(index int, value interface{}) bool

+

遍历slice的元素,返回第一个通过function真值测试的元素

函数签名: ```go -func Find(slice, function interface{}) (interface{}, bool) +func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) ``` 例子: @@ -421,12 +421,12 @@ func main() { ### FindLast -

从头到尾遍历 slice 的元素,返回最后一个通过函数真值测试的元素。 函数签名应该是 func(index int, value interface{}) bool。

+

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

函数签名: ```go -func FindLast(slice, function interface{}) (interface{}, bool) +func FindLast[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) ``` 例子: @@ -478,12 +478,12 @@ func main() { ### ForEach -

遍历slice的元素并为每个元素调用函数,函数签名应该是func(index int, value interface{})

+

遍历slice的元素并为每个元素调用函数

函数签名: ```go -func ForEach(slice, function interface{}) +func ForEach[T any](slice []T, iteratee func(index int, t T)) ``` 例子: @@ -507,12 +507,12 @@ func main() { ### GroupBy -

迭代切片的元素,每个元素将按条件分组,返回两个切片。 函数签名应该是func(index int, value interface{}) bool

+

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

函数签名: ```go -func GroupBy(slice, function interface{}) (interface{}, interface{}) +func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) ``` 例子: @@ -595,7 +595,7 @@ func main() { 函数签名: ```go -func Intersection(slices ...interface{}) interface{} +func Intersection[T any](slices ...[]T) []T ``` 例子: @@ -617,13 +617,13 @@ func main() { -### InsertByIndex +### InsertAt

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

函数签名: ```go -func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error) +func InsertAt[T any](slice []T, index int, value interface{}) []T ``` 例子: @@ -636,10 +636,10 @@ import ( func main() { s := []string{"a", "b", "c"} - res1, _ := slice.InsertByIndex(s, 0, "1") + res1, _ := slice.InsertAt(s, 0, "1") fmt.Println(res1) //[]string{"1", "a", "b", "c"} - res2, _ := slice.InsertByIndex(s, 3, []string{"1", "2", "3"}) + res2, _ := slice.InsertAt(s, 3, []string{"1", "2", "3"}) fmt.Println(res2) //[]string{"a", "b", "c", "1", "2", "3"} } ``` @@ -648,12 +648,12 @@ func main() { ### Map -

通过运行函数slice中的每个元素来创建一个值切片,函数签名应该是func(index int, value interface{}) interface{}。

+

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

函数签名: ```go -func Map(slice, function interface{}) interface{} +func Map[T any, U any](slice []T, iteratee func(index int, t T) U) []U ``` 例子: @@ -676,13 +676,13 @@ func main() { -### ReverseSlice +### Reverse

反转切片中的元素顺序

函数签名: ```go -func ReverseSlice(slice interface{}) +func Reverse[T any](slice []T) ``` 例子: @@ -694,7 +694,7 @@ import ( func main() { nums := []int{1, 2, 3, 4} - slice.ReverseSlice(nums) + slice.Reverse(nums) fmt.Println(res) //[]int{4, 3, 2, 1} } ``` @@ -702,12 +702,12 @@ func main() { ### Reduce -

将slice中的元素运行函数,返回运行结果。函数签名应该是func(index int, value1, value2 interface{}) interface{}。

+

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

函数签名: ```go -func Reduce(slice, function, zero interface{}) interface{} +func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T ``` 例子: @@ -736,7 +736,7 @@ func main() { 函数签名: ```go -func Shuffle(slice interface{}) interface{} +func Shuffle[T any](slice []T) []T ``` 例子: @@ -799,12 +799,12 @@ func main() { ### Some -

如果列表中的任何值通过谓词函数,则返回true,函数签名应该是func(index int, value interface{}) bool .

+

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

函数签名: ```go -func Some(slice, function interface{}) bool +func Some[T any](slice []T, predicate func(index int, t T) bool) bool ``` 例子: @@ -859,7 +859,7 @@ func main() { 函数签名: ```go -func Unique(slice interface{}) interface{} +func Unique[T any](slice []T) []T ``` 例子: @@ -883,7 +883,7 @@ func main() { 函数签名: ```go -func Union(slices ...interface{}) interface{} +func Union[T any](slices ...[]T) []T ``` 例子: @@ -904,13 +904,13 @@ func main() { -### UpdateByIndex +### UpdateAt

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

函数签名: ```go -func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) +func UpdateAt[T any](slice []T, index int, value T) []T ``` 例子: @@ -923,7 +923,7 @@ import ( func main() { s := []string{"a", "b", "c"} - res1, _ := slice.UpdateByIndex(s, 0, "1") + res1, _ := slice.UpdateAt(s, 0, "1") fmt.Println(res1) //[]string{"1", "b", "c"} } ``` @@ -937,7 +937,7 @@ func main() { 函数签名: ```go -func Without(slice interface{}, values ...interface{}) interface{} +func Without[T any](slice []T, values ...T) []T ``` 例子: