mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 02:02:27 +08:00
update: make func comment consistent
This commit is contained in:
@@ -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 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 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 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 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 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
|
func IntSlice(slice interface{}) ([]int, error) //convert value to int slice
|
||||||
|
|||||||
@@ -399,9 +399,9 @@ func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interfac
|
|||||||
func Difference[T comparable](slice1, slice2 []T) []T //返回切片,其元素在slice1中,不在slice2中
|
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 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 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 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(index int, value interface{}) 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(index int, value interface{}) 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 Filter [T any] (slice []T, fn func(index int, t T) bool) []T //过滤slice
|
||||||
func FlattenDeep(slice interface{}) interface{} //将slice递归为一维切片
|
func FlattenDeep(slice interface{}) interface{} //将slice递归为一维切片
|
||||||
func ForEach [T any] (slice []T, fn func(index int, t T)) //遍历切片,在每个元素上执行函数
|
func ForEach [T any] (slice []T, fn func(index int, t T)) //遍历切片,在每个元素上执行函数
|
||||||
|
|||||||
@@ -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.
|
// 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 .
|
// The fn function signature should be func(int, T) bool .
|
||||||
func Every[T any](slice []T, fn func(index int, t T) bool) 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 {
|
for i, v := range slice {
|
||||||
if fn(i, v) {
|
if fn(i, v) {
|
||||||
currentLength++
|
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
|
// None return true if all the values in the slice mismatch the criteria
|
||||||
// The fn function signature should be func(int, T) bool .
|
// The fn function signature should be func(int, T) bool .
|
||||||
func None[T any](slice []T, fn func(index int, t T) bool) 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 {
|
for i, v := range slice {
|
||||||
if !fn(i, v) {
|
if !fn(i, v) {
|
||||||
currentLength++
|
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.
|
// 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.
|
// The fn function signature should be func(int, T) bool.
|
||||||
func Some[T any](slice []T, fn func(index int, t T) bool) 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 {
|
for i, v := range slice {
|
||||||
if fn(i, v) {
|
if fn(i, v) {
|
||||||
return true
|
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.
|
// 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.
|
// The fn function signature should be func(int, T) bool.
|
||||||
func Filter[T any](slice []T, fn func(index int, t T) bool) []T {
|
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)
|
res := make([]T, 0, 0)
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if fn(i, v) {
|
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
|
// 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 {
|
func Count[T any](slice []T, fn func(index int, t T) bool) int {
|
||||||
if fn == nil {
|
if fn == nil {
|
||||||
panic("fn is missing")
|
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
|
// 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) {
|
func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) {
|
||||||
|
|
||||||
if fn == nil {
|
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 .
|
// The fn function signature should be func(int, T) bool .
|
||||||
// If return T is nil then no items matched the predicate func
|
// 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) {
|
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 {
|
if len(slice) == 0 {
|
||||||
return nil, false
|
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
|
// ForEach iterates over elements of slice and invokes function for each element
|
||||||
// The fn function signature should be func(int, T).
|
// The fn function signature should be func(int, T).
|
||||||
func ForEach[T any](slice []T, fn func(index int, t 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 {
|
for i, v := range slice {
|
||||||
fn(i, v)
|
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.
|
// Map creates an slice of values by running each element of slice thru fn function.
|
||||||
// The fn function signature should be func(int, T).
|
// 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 {
|
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))
|
res := make([]U, len(slice), cap(slice))
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
res[i] = fn(i, v)
|
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.
|
// 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 .
|
// 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 {
|
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 {
|
if len(slice) == 0 {
|
||||||
return initial
|
return initial
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user