1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

refactor: clean code for slice/slice.go

This commit is contained in:
dudaodong
2022-01-24 11:46:09 +08:00
parent 2612569500
commit 89aef977a2
3 changed files with 56 additions and 67 deletions

View File

@@ -441,29 +441,29 @@ func Difference[T comparable](slice1, slice2 []T) []T //creates an slice of whos
func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T //it accepts iteratee which is invoked for each element of slice and values to generate the criterion by which they're compared.
func DeleteByIndex[T any](slice []T, start int, end ...int) []T //delete the element of slice from start index to end index - 1
func Drop[T any](slice []T, n int) []T //creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0
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(int, T) bool .
func Every[T any](slice []T, predicate 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, predicate 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, predicate func(index int, t T) bool) []T //iterates over elements of slice, returning an slice of all elements pass the predicate function
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) //iterates over elements of slice, returning the first one that passes a truth test on iteratee function. If return T is nil then no items matched the predicate func
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, iteratee func(index int, t T)) //iterates over elements of slice and invokes function for each element
func IntSlice(slice interface{}) ([]int, error) //convert value to int slice
func InterfaceSlice(slice interface{}) []interface{} //convert value to interface{} slice
func Intersection[T comparable](slices ...[]T) []T //creates a slice of unique values that included by all slices.
func InsertByIndex[T any](slice []T, index int, value interface{}) []T //insert the value or other slice into slice at index.
func Map [T any, U any] (slice []T, fn func(index int, t T) U) []U //map lisce, fn signature should be func(int, T).
func Map [T any, U any] (slice []T, iteratee func(index int, t T) U) []U //creates an slice of values by running each element of slice thru iteratee function.
func Reverse[T any](slice []T)//revere slice
func Reduce[T any](slice []T, fn func(index int, t1, t2 T) T, initial T) T //reduce slice
func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T //creates an slice of values by running each element of slice thru iteratee function
func Shuffle[T any](slice []T) []T //creates an slice of shuffled values
func SortByField(slice interface{}, field string, sortType ...string) error //sort struct slice by field
func Some[T any](slice []T, fn func(index int, t T) bool) bool //return true if any of the values in the list pass the predicate fn function
func Some[T any](slice []T, predicate func(index int, t T) bool) bool //return true if any of the values in the list pass the predicate function
func StringSlice(slice interface{}) []string //convert value to string slice
func Unique[T comparable](slice []T) []T //remove duplicate elements in slice
func Union[T comparable](slices ...[]T) []T //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
func UpdateByIndex[T any](slice []T, index int, value T) []T //update the slice element at index.
func Without[T comparable](slice []T, values ...T) []T //creates a slice excluding all given values
func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) // groups slice into two categories
func Count[T any](slice []T, fn func(index int, t T) bool) int // Count iterates over elements of slice, returns a count of all matched elements
func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) //iterate over elements of the slice, each element will be group by criteria, returns two slices
func Count[T any](slice []T, predicate func(index int, t T) bool) int // iterates over elements of slice, returns a count of all matched elements
```
### 11. strutil is for processing string

View File

@@ -441,29 +441,29 @@ func Difference[T comparable](slice1, slice2 []T) []T //返回切片,其元素
func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T //将slice 和comparedSlice中每个元素调用iterateeFn后作比较如果不相等返回slice中的元素。
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(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 Every[T any](slice []T, predicate func(index int, t T) bool) bool//slice中所有元素都符合函数条件时返回true, 否则返回false
func None[T any](slice []T, predicate func(index int, t T) bool) bool //slice中所有元素都不符合函数条件时返回true, 否则返回false
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) //查找slice中第一个符合条件的元素没有找到时返回nil
func Filter [T any] (slice []T, predicate func(index int, t T) bool) []T//过滤slice中元素符合predicate函数
func FlattenDeep(slice interface{}) interface{} //将slice递归为一维切片
func ForEach [T any] (slice []T, fn func(index int, t T)) //遍历切片,在每个元素上执行函数
func ForEach [T any] (slice []T, iteratee func(index int, t T)) //遍历切片,在每个元素上执行iteratee函数
func IntSlice(slice interface{}) ([]int, error) //转成int切片
func InterfaceSlice(slice interface{}) []interface{} //转成interface{}切片
func Intersection[T comparable](slices ...[]T) []T //slice交集去重
func InsertByIndex[T any](slice []T, index int, value interface{}) []T //在切片中index位置插入value
func Map [T any, U any] (slice []T, fn func(index int, t T) U) []U //遍历切片
func Map [T any, U any] (slice []T, iteratee func(index int, t T) U) []U //遍历切片,返回遍历结果
func Reverse[T any](slice []T) //反转切片
func Reduce[T any](slice []T, fn func(index int, t1, t2 T) T, initial T) T //切片reduce操作
func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T //切片reduce操作
func Shuffle[T any](slice []T) []T //创建一个被打乱值的切片
func Some[T any](slice []T, fn func(index int, t T) bool) bool //slice中任意一个元素都符合函数条件时返回true, 否则返回false.
func Some[T any](slice []T, predicate func(index int, t T) bool) bool //slice中任意一个元素都符合函数条件时返回true, 否则返回false.
func SortByField(slice interface{}, field string, sortType ...string) error //对struct切片进行排序
func StringSlice(slice interface{}) []string //转为string切片
func Unique[T comparable](slice []T) []T //去重切片
func Union[T comparable](slices ...[]T) []T //slice并集, 去重
func UpdateByIndex[T any](slice []T, index int, value T) []T //在切片中index位置更新value
func Without[T comparable](slice []T, values ...T) []T //slice去除values
func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) //根据函数function的逻辑分slice为两组slice
func Count[T any](slice []T, fn func(index int, t T) bool) int //遍历slice的元素返回所有匹配元素的
func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) //根据函数groupFn的逻辑分slice为两组slice
func Count[T any](slice []T, predicate func(index int, t T) bool) int //遍历slice的元素返回所有匹配元素的数
```
### 11. strutil字符串处理包

View File

@@ -12,7 +12,7 @@ import (
"sort"
)
// Contain check if the value is in the iterable type or not
// Contain check if the value is in the slice or not
func Contain[T any](slice []T, value T) bool {
for _, v := range slice {
if reflect.DeepEqual(v, value) {
@@ -127,15 +127,14 @@ func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int,
}
// 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 {
if fn == nil {
func Every[T any](slice []T, predicate func(index int, t T) bool) bool {
if predicate == nil {
panic("fn is missing")
}
var currentLength int
for i, v := range slice {
if fn(i, v) {
if predicate(i, v) {
currentLength++
}
}
@@ -144,15 +143,14 @@ 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 {
if fn == nil {
func None[T any](slice []T, predicate func(index int, t T) bool) bool {
if predicate == nil {
panic("fn is missing")
}
var currentLength int
for i, v := range slice {
if !fn(i, v) {
if !predicate(i, v) {
currentLength++
}
}
@@ -161,30 +159,28 @@ 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 {
func Some[T any](slice []T, predicate func(index int, t T) bool) bool {
if predicate == nil {
panic("fn is missing")
}
for i, v := range slice {
if fn(i, v) {
if predicate(i, v) {
return true
}
}
return false
}
// 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 {
// 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 {
if predicate == nil {
panic("fn is missing")
}
res := make([]T, 0, 0)
for i, v := range slice {
if fn(i, v) {
if predicate(i, v) {
res = append(res, v)
}
}
@@ -192,9 +188,8 @@ 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(int, T) bool .
func Count[T any](slice []T, fn func(index int, t T) bool) int {
if fn == nil {
func Count[T any](slice []T, predicate func(index int, t T) bool) int {
if predicate == nil {
panic("fn is missing")
}
@@ -204,7 +199,7 @@ func Count[T any](slice []T, fn func(index int, t T) bool) int {
var count int
for i, v := range slice {
if fn(i, v) {
if predicate(i, v) {
count++
}
}
@@ -213,10 +208,8 @@ 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, T) bool .
func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) {
if fn == nil {
func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) {
if groupFn == nil {
panic("fn is missing")
}
@@ -228,7 +221,7 @@ func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) {
groupA := make([]T, 0)
for i, v := range slice {
ok := fn(i, v)
ok := groupFn(i, v)
if ok {
groupA = append(groupA, v)
} else {
@@ -239,11 +232,10 @@ func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) {
return groupA, groupB
}
// Find iterates over elements of slice, returning the first one that passes a truth test on function.
// The fn function signature should be func(int, T) bool .
// Find iterates over elements of slice, returning the first one that passes a truth test on iteratee function.
// 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 {
func Find[T any](slice []T, iteratee func(index int, t T) bool) (*T, bool) {
if iteratee == nil {
panic("fn is missing")
}
@@ -253,7 +245,7 @@ func Find[T any](slice []T, fn func(index int, t T) bool) (*T, bool) {
index := -1
for i, v := range slice {
if fn(i, v) {
if iteratee(i, v) {
index = i
break
}
@@ -291,36 +283,33 @@ 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 {
func ForEach[T any](slice []T, iteratee func(index int, t T)) {
if iteratee == nil {
panic("fn is missing")
}
for i, v := range slice {
fn(i, v)
iteratee(i, v)
}
}
// 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 {
// 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 {
if iteratee == nil {
panic("fn is missing")
}
res := make([]U, len(slice), cap(slice))
for i, v := range slice {
res[i] = fn(i, v)
res[i] = iteratee(i, v)
}
return res
}
// 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 {
// 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 {
if iteratee == nil {
panic("fn is missing")
}
@@ -328,13 +317,13 @@ func Reduce[T any](slice []T, fn func(index int, t1, t2 T) T, initial T) T {
return initial
}
res := fn(0, initial, slice[0])
res := iteratee(0, initial, slice[0])
tmp := make([]T, 2, 2)
for i := 1; i < len(slice); i++ {
tmp[0] = res
tmp[1] = slice[i]
res = fn(i, tmp[0], tmp[1])
res = iteratee(i, tmp[0], tmp[1])
}
return res