diff --git a/README.md b/README.md index 20bc200..29fa3d3 100644 --- a/README.md +++ b/README.md @@ -401,7 +401,7 @@ func Drop[T any](slice []T, n int) []T //creates a slice with `n` elements dropp func Every[T any](slice []T, fn func(index int, t T) bool) bool //return true if all of the values in the slice pass the predicate function func None[T any](slice []T, fn func(index int, t T) bool) bool // return true if all the values in the slice mismatch the criteria func Filter [T any] (slice []T, fn func(index int, t T) bool) []T //filter slice, fn signature should be func(int, T) bool. -func Find[T any](slice []T, fn func(index int, t T) bool) (*T, bool) //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 . +func Find[T any](slice []T, fn func(index int, t T) bool) (*T, bool) //iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(int, T) bool . func FlattenDeep(slice interface{}) interface{} //flattens slice recursive func ForEach [T any] (slice []T, fn func(index int, t T)) //iterates over elements of slice and invokes function for each element, fn signature should be func(int, T ). func IntSlice(slice interface{}) ([]int, error) //convert value to int slice diff --git a/README_zh-CN.md b/README_zh-CN.md index e57739a..49a8914 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -399,9 +399,9 @@ func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interfac func Difference[T comparable](slice1, slice2 []T) []T //返回切片,其元素在slice1中,不在slice2中 func DeleteByIndex[T any](slice []T, start int, end ...int) []T //删除切片中start到end位置的值(不包含end) func Drop[T any](slice []T, n int) []T //创建一个新切片,当n大于0时删除原切片前n个元素,当n小于0时删除原切片后n个元素 -func Every[T any](slice []T, fn func(index int, t T) bool) bool //slice中所有元素都符合函数条件时返回true, 否则返回false. 函数签名:func(index int, t T) bool -func None[T any](slice []T, fn func(index int, t T) bool) bool //slice中所有元素都不符合函数条件时返回true, 否则返回false. 函数签名:func(index int, value interface{}) bool -func Find[T any](slice []T, fn func(index int, t T) bool) (*T, bool)//查找slice中第一个符合条件的元素,函数签名:func(index int, value interface{}) bool +func Every[T any](slice []T, fn func(index int, t T) bool) bool //slice中所有元素都符合函数条件时返回true, 否则返回false. 函数签名:func(int, t T) bool +func None[T any](slice []T, fn func(index int, t T) bool) bool //slice中所有元素都不符合函数条件时返回true, 否则返回false. 函数签名:func(int, T) bool +func Find[T any](slice []T, fn func(index int, t T) bool) (*T, bool)//查找slice中第一个符合条件的元素,函数签名:func(int, T) bool func Filter [T any] (slice []T, fn func(index int, t T) bool) []T //过滤slice func FlattenDeep(slice interface{}) interface{} //将slice递归为一维切片 func ForEach [T any] (slice []T, fn func(index int, t T)) //遍历切片,在每个元素上执行函数 diff --git a/slice/slice.go b/slice/slice.go index 1b9ad02..41c7dcd 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -72,8 +72,11 @@ func Difference[T comparable](slice1, slice2 []T) []T { // Every return true if all of the values in the slice pass the predicate function. // The fn function signature should be func(int, T) bool . func Every[T any](slice []T, fn func(index int, t T) bool) bool { - var currentLength int + if fn == nil { + panic("fn is missing") + } + var currentLength int for i, v := range slice { if fn(i, v) { currentLength++ @@ -86,8 +89,11 @@ func Every[T any](slice []T, fn func(index int, t T) bool) bool { // None return true if all the values in the slice mismatch the criteria // The fn function signature should be func(int, T) bool . func None[T any](slice []T, fn func(index int, t T) bool) bool { - var currentLength int + if fn == nil { + panic("fn is missing") + } + var currentLength int for i, v := range slice { if !fn(i, v) { currentLength++ @@ -100,6 +106,10 @@ func None[T any](slice []T, fn func(index int, t T) bool) bool { // Some return true if any of the values in the list pass the predicate function. // The fn function signature should be func(int, T) bool. func Some[T any](slice []T, fn func(index int, t T) bool) bool { + if fn == nil { + panic("fn is missing") + } + for i, v := range slice { if fn(i, v) { return true @@ -111,6 +121,10 @@ func Some[T any](slice []T, fn func(index int, t T) bool) bool { // Filter iterates over elements of slice, returning an slice of all elements `signature` returns truthy for. // The fn function signature should be func(int, T) bool. func Filter[T any](slice []T, fn func(index int, t T) bool) []T { + if fn == nil { + panic("fn is missing") + } + res := make([]T, 0, 0) for i, v := range slice { if fn(i, v) { @@ -121,7 +135,7 @@ func Filter[T any](slice []T, fn func(index int, t T) bool) []T { } // Count iterates over elements of slice, returns a count of all matched elements -// The function signature should be func(index int, value interface{}) bool . +// The function signature should be func(int, T) bool . func Count[T any](slice []T, fn func(index int, t T) bool) int { if fn == nil { panic("fn is missing") @@ -142,7 +156,7 @@ func Count[T any](slice []T, fn func(index int, t T) bool) int { } // GroupBy iterate 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 . +// The function signature should be func(int, value T) bool . func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) { if fn == nil { @@ -172,6 +186,10 @@ func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) { // The fn function signature should be func(int, T) bool . // If return T is nil then no items matched the predicate func func Find[T any](slice []T, fn func(index int, t T) bool) (*T, bool) { + if fn == nil { + panic("fn is missing") + } + if len(slice) == 0 { return nil, false } @@ -218,6 +236,10 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value { // ForEach iterates over elements of slice and invokes function for each element // The fn function signature should be func(int, T). func ForEach[T any](slice []T, fn func(index int, t T)) { + if fn == nil { + panic("fn is missing") + } + for i, v := range slice { fn(i, v) } @@ -226,6 +248,10 @@ func ForEach[T any](slice []T, fn func(index int, t T)) { // Map creates an slice of values by running each element of slice thru fn function. // The fn function signature should be func(int, T). func Map[T any, U any](slice []T, fn func(index int, t T) U) []U { + if fn == nil { + panic("fn is missing") + } + res := make([]U, len(slice), cap(slice)) for i, v := range slice { res[i] = fn(i, v) @@ -237,6 +263,10 @@ func Map[T any, U any](slice []T, fn func(index int, t T) U) []U { // Reduce creates an slice of values by running each element of slice thru fn function. // The fn function signature should be fn func(int, T) T . func Reduce[T any](slice []T, fn func(index int, t1, t2 T) T, initial T) T { + if fn == nil { + panic("fn is missing") + } + if len(slice) == 0 { return initial }