From bc9bacaa55f7889a7f609765a1b999d93997cc67 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sat, 21 May 2022 16:50:57 +0800 Subject: [PATCH 1/2] refactor: change param to item --- slice/slice.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/slice/slice.go b/slice/slice.go index 55f13c1..fcad15d 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -108,7 +108,7 @@ func Difference[T comparable](slice, comparedSlice []T) []T { // DifferenceBy it accepts iteratee which is invoked for each element of slice // and values to generate the criterion by which they're compared. // like lodash.js differenceBy: https://lodash.com/docs/4.17.15#differenceBy, -func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T { +func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T { orginSliceAfterMap := Map(slice, iteratee) comparedSliceAfterMap := Map(comparedSlice, iteratee) @@ -148,7 +148,7 @@ func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, } // Every return true if all of the values in the slice pass the predicate function. -func Every[T any](slice []T, predicate func(index int, t T) bool) bool { +func Every[T any](slice []T, predicate func(index int, item T) bool) bool { if predicate == nil { panic("predicate func is missing") } @@ -164,7 +164,7 @@ func Every[T any](slice []T, predicate func(index int, t T) bool) bool { } // None return true if all the values in the slice mismatch the criteria -func None[T any](slice []T, predicate func(index int, t T) bool) bool { +func None[T any](slice []T, predicate func(index int, item T) bool) bool { if predicate == nil { panic("predicate func is missing") } @@ -180,7 +180,7 @@ func None[T any](slice []T, predicate func(index int, t T) bool) bool { } // Some return true if any of the values in the list pass the predicate function. -func Some[T any](slice []T, predicate func(index int, t T) bool) bool { +func Some[T any](slice []T, predicate func(index int, item T) bool) bool { if predicate == nil { panic("predicate func is missing") } @@ -194,7 +194,7 @@ func Some[T any](slice []T, predicate func(index int, t T) bool) bool { } // Filter iterates over elements of slice, returning an slice of all elements pass the predicate function -func Filter[T any](slice []T, predicate func(index int, t T) bool) []T { +func Filter[T any](slice []T, predicate func(index int, item T) bool) []T { if predicate == nil { panic("predicate func is missing") } @@ -209,7 +209,7 @@ func Filter[T any](slice []T, predicate func(index int, t T) bool) []T { } // Count iterates over elements of slice, returns a count of all matched elements -func Count[T any](slice []T, predicate func(index int, t T) bool) int { +func Count[T any](slice []T, predicate func(index int, item T) bool) int { if predicate == nil { panic("predicate func is missing") } @@ -229,7 +229,7 @@ func Count[T any](slice []T, predicate func(index int, t T) bool) int { } // GroupBy iterate over elements of the slice, each element will be group by criteria, returns two slices -func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) { +func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T) { if groupFn == nil { panic("groupFn func is missing") } @@ -274,7 +274,7 @@ func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T { // Find iterates over elements of slice, returning the first one that passes a truth test on predicate function. // If return T is nil then no items matched the predicate func -func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) { +func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) { if predicate == nil { panic("predicate func is missing") } @@ -300,7 +300,7 @@ func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) { // FindLast iterates over elements of slice from end to begin, returning the first one that passes a truth test on predicate function. // If return T is nil then no items matched the predicate func -func FindLast[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) { +func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) { if predicate == nil { panic("predicate func is missing") } @@ -349,7 +349,7 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value { } // ForEach iterates over elements of slice and invokes function for each element -func ForEach[T any](slice []T, iteratee func(index int, t T)) { +func ForEach[T any](slice []T, iteratee func(index int, item T)) { if iteratee == nil { panic("iteratee func is missing") } @@ -360,7 +360,7 @@ func ForEach[T any](slice []T, iteratee func(index int, t T)) { } // Map creates an slice of values by running each element of slice thru iteratee function. -func Map[T any, U any](slice []T, iteratee func(index int, t T) U) []U { +func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U { if iteratee == nil { panic("iteratee func is missing") } @@ -374,7 +374,7 @@ func Map[T any, U any](slice []T, iteratee func(index int, t T) U) []U { } // Reduce creates an slice of values by running each element of slice thru iteratee function. -func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T { +func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T { if iteratee == nil { panic("iteratee func is missing") } From d097f356fd5e72252a6b670e515898bf29e2cbfb Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sat, 21 May 2022 17:00:28 +0800 Subject: [PATCH 2/2] docs: change some function comments for slice doc --- docs/slice.md | 20 ++++++++++---------- docs/slice_zh-CN.md | 34 +++++++++++++++++----------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/docs/slice.md b/docs/slice.md index 145294a..1ed157d 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -250,7 +250,7 @@ func main() { Signature: ```go -func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T +func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T ``` Example: @@ -367,7 +367,7 @@ func main() { Signature: ```go -func Every[T any](slice []T, predicate func(index int, t T) bool) bool +func Every[T any](slice []T, predicate func(index int, item T) bool) bool ``` Example: @@ -397,7 +397,7 @@ func main() { Signature: ```go -func Filter[T any](slice []T, predicate func(index int, t T) bool) []T +func Filter[T any](slice []T, predicate func(index int, item T) bool) []T ``` Example: @@ -426,7 +426,7 @@ func main() { Signature: ```go -func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) +func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) ``` Example: @@ -457,7 +457,7 @@ func main() { Signature: ```go -func FindLast[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) +func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) ``` Example: @@ -514,7 +514,7 @@ func main() { Signature: ```go -func ForEach[T any](slice []T, iteratee func(index int, t T)) +func ForEach[T any](slice []T, iteratee func(index int, item T)) ``` Example: @@ -543,7 +543,7 @@ func main() { Signature: ```go -func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) +func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T) ``` Example: @@ -769,7 +769,7 @@ func main() { Signature: ```go -func Map[T any, U any](slice []T, iteratee func(index int, t T) U) []U +func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U ``` Example: @@ -823,7 +823,7 @@ func main() { Signature: ```go -func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T +func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T ``` Example: @@ -920,7 +920,7 @@ func main() { Signature: ```go -func Some[T any](slice []T, predicate func(index int, t T) bool) bool +func Some[T any](slice []T, predicate func(index int, item T) bool) bool ``` Example: diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 0521bdd..3c38c90 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -250,7 +250,7 @@ func main() { 函数签名: ```go -func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T +func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T ``` 例子: @@ -369,7 +369,7 @@ func main() { 函数签名: ```go -func Every[T any](slice []T, predicate func(index int, t T) bool) bool +func Every[T any](slice []T, predicate func(index int, item T) bool) bool ``` 例子: @@ -394,12 +394,12 @@ func main() { ### Filter -

返回与函数匹配的所有元素。 函数签名应该是 func(index int, value any) bool

+

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

函数签名: ```go -func Filter[T any](slice []T, predicate func(index int, t T) bool) []T +func Filter[T any](slice []T, predicate func(index int, item T) bool) []T ``` 例子: @@ -423,12 +423,12 @@ func main() { ### Find -

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

+

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

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

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

+

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

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

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

+

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

函数签名: ```go -func ForEach[T any](slice []T, iteratee func(index int, t T)) +func ForEach[T any](slice []T, iteratee func(index int, item T)) ``` 例子: @@ -545,7 +545,7 @@ func main() { 函数签名: ```go -func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) +func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T) ``` 例子: @@ -768,7 +768,7 @@ func main() { 函数签名: ```go -func Map[T any, U any](slice []T, iteratee func(index int, t T) U) []U +func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U ``` 例子: @@ -817,12 +817,12 @@ func main() { ### Reduce -

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

+

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

函数签名: ```go -func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T +func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T ``` 例子: @@ -919,7 +919,7 @@ func main() { 函数签名: ```go -func Some[T any](slice []T, predicate func(index int, t T) bool) bool +func Some[T any](slice []T, predicate func(index int, item T) bool) bool ``` 例子: @@ -1021,7 +1021,7 @@ func main() { ### Unique -

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

+

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

函数签名: @@ -1048,7 +1048,7 @@ func main() { ### UpdateAt -

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

+

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

函数签名: