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

refactor: clean code

This commit is contained in:
dudaodong
2022-12-03 12:58:25 +08:00
parent f8b785c4cb
commit a044da7d2f

View File

@@ -25,9 +25,9 @@ func Contain[T comparable](slice []T, target T) bool {
return false return false
} }
// ContainSubSlice check if the slice contain subslice or not // ContainSubSlice check if the slice contain a given subslice or not
func ContainSubSlice[T comparable](slice, subslice []T) bool { func ContainSubSlice[T comparable](slice, subSlice []T) bool {
for _, v := range subslice { for _, v := range subSlice {
if !Contain(slice, v) { if !Contain(slice, v) {
return false return false
} }
@@ -36,7 +36,7 @@ func ContainSubSlice[T comparable](slice, subslice []T) bool {
return true return true
} }
// Chunk creates an slice of elements split into groups the length of size. // Chunk creates a slice of elements split into groups the length of size
func Chunk[T any](slice []T, size int) [][]T { func Chunk[T any](slice []T, size int) [][]T {
result := [][]T{} result := [][]T{}
@@ -63,20 +63,20 @@ func Compact[T comparable](slice []T) []T {
var zero T var zero T
result := []T{} result := []T{}
for _, item := range slice { for _, v := range slice {
if item != zero { if v != zero {
result = append(result, item) result = append(result, v)
} }
} }
return result return result
} }
// Concat creates a new slice concatenating slice with any additional slices and/or values. // Concat creates a new slice concatenating slice with any additional slices.
func Concat[T any](slice []T, values ...[]T) []T { func Concat[T any](slice []T, slices ...[]T) []T {
result := append([]T{}, slice...) result := append([]T{}, slice...)
for _, v := range values { for _, v := range slices {
result = append(result, v...) result = append(result, v...)
} }
@@ -159,9 +159,8 @@ func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) boo
return false return false
} }
for i, v1 := range slice1 { for i, v := range slice1 {
v2 := slice2[i] if !comparator(v, slice2[i]) {
if !comparator(v1, v2) {
return false return false
} }
} }
@@ -206,6 +205,7 @@ func Some[T any](slice []T, predicate func(index int, item T) bool) bool {
// Filter iterates over elements of slice, returning an slice of all elements pass the predicate function // 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, item T) bool) []T { func Filter[T any](slice []T, predicate func(index int, item T) bool) []T {
result := make([]T, 0) result := make([]T, 0)
for i, v := range slice { for i, v := range slice {
if predicate(i, v) { if predicate(i, v) {
result = append(result, v) result = append(result, v)
@@ -217,11 +217,8 @@ func Filter[T any](slice []T, predicate func(index int, item 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
func Count[T any](slice []T, predicate func(index int, item T) bool) int { func Count[T any](slice []T, predicate func(index int, item T) bool) int {
if len(slice) == 0 { count := 0
return 0
}
var count int
for i, v := range slice { for i, v := range slice {
if predicate(i, v) { if predicate(i, v) {
count++ count++
@@ -270,11 +267,8 @@ func GroupWith[T any, U comparable](slice []T, iteratee func(item T) U) map[U][]
// Find iterates over elements of slice, returning the first one that passes a truth test on predicate function. // 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 // If return T is nil then no items matched the predicate func
func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) { func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
if len(slice) == 0 {
return nil, false
}
index := -1 index := -1
for i, v := range slice { for i, v := range slice {
if predicate(i, v) { if predicate(i, v) {
index = i index = i
@@ -292,11 +286,8 @@ func Find[T any](slice []T, predicate func(index int, item 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. // 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 // If return T is nil then no items matched the predicate func
func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) { func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
if len(slice) == 0 {
return nil, false
}
index := -1 index := -1
for i := len(slice) - 1; i >= 0; i-- { for i := len(slice) - 1; i >= 0; i-- {
if predicate(i, slice[i]) { if predicate(i, slice[i]) {
index = i index = i
@@ -342,8 +333,11 @@ func Flatten(slice any) any {
func FlattenDeep(slice any) any { func FlattenDeep(slice any) any {
sv := sliceValue(slice) sv := sliceValue(slice)
st := sliceElemType(sv.Type()) st := sliceElemType(sv.Type())
tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0) tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0)
result := flattenRecursive(sv, tmp) result := flattenRecursive(sv, tmp)
return result.Interface() return result.Interface()
} }
@@ -372,6 +366,7 @@ func ForEach[T any](slice []T, iteratee func(index int, item T)) {
// Map creates an slice of values by running each element of slice thru iteratee function. // 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, item T) U) []U { func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U {
result := make([]U, len(slice), cap(slice)) result := make([]U, len(slice), cap(slice))
for i, v := range slice { for i, v := range slice {
result[i] = iteratee(i, v) result[i] = iteratee(i, v)
} }
@@ -381,7 +376,6 @@ func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U {
// Reduce creates an slice of values by running each element of slice thru iteratee function. // 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, item1, item2 T) T, initial T) T { func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T {
if len(slice) == 0 { if len(slice) == 0 {
return initial return initial
} }
@@ -421,9 +415,11 @@ func ReplaceAll[T comparable](slice []T, old T, new T) []T {
// Repeat creates a slice with length n whose elements are param `item`. // Repeat creates a slice with length n whose elements are param `item`.
func Repeat[T any](item T, n int) []T { func Repeat[T any](item T, n int) []T {
result := make([]T, n) result := make([]T, n)
for i := range result { for i := range result {
result[i] = item result[i] = item
} }
return result return result
} }
@@ -446,32 +442,32 @@ func InterfaceSlice(slice any) []any {
func StringSlice(slice any) []string { func StringSlice(slice any) []string {
v := sliceValue(slice) v := sliceValue(slice)
out := make([]string, v.Len()) result := make([]string, v.Len())
for i := 0; i < v.Len(); i++ { for i := 0; i < v.Len(); i++ {
v, ok := v.Index(i).Interface().(string) v, ok := v.Index(i).Interface().(string)
if !ok { if !ok {
panic("invalid element type") panic("invalid element type")
} }
out[i] = v result[i] = v
} }
return out return result
} }
// IntSlice convert param to slice of int. // IntSlice convert param to slice of int.
func IntSlice(slice any) []int { func IntSlice(slice any) []int {
sv := sliceValue(slice) sv := sliceValue(slice)
out := make([]int, sv.Len()) result := make([]int, sv.Len())
for i := 0; i < sv.Len(); i++ { for i := 0; i < sv.Len(); i++ {
v, ok := sv.Index(i).Interface().(int) v, ok := sv.Index(i).Interface().(int)
if !ok { if !ok {
panic("invalid element type") panic("invalid element type")
} }
out[i] = v result[i] = v
} }
return out return result
} }
// DeleteAt delete the element of slice from start index to end index - 1. // DeleteAt delete the element of slice from start index to end index - 1.
@@ -628,8 +624,8 @@ func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T {
func Merge[T any](slices ...[]T) []T { func Merge[T any](slices ...[]T) []T {
result := make([]T, 0) result := make([]T, 0)
for _, item := range slices { for _, v := range slices {
result = append(result, item...) result = append(result, v...)
} }
return result return result
@@ -646,8 +642,8 @@ func Intersection[T comparable](slices ...[]T) []T {
reducer := func(sliceA, sliceB []T) []T { reducer := func(sliceA, sliceB []T) []T {
hashMap := make(map[T]int) hashMap := make(map[T]int)
for _, val := range sliceA { for _, v := range sliceA {
hashMap[val] = 1 hashMap[v] = 1
} }
out := make([]T, 0) out := make([]T, 0)
@@ -832,11 +828,10 @@ func IndexOf[T comparable](slice []T, value T) int {
return -1 return -1
} }
// LastIndexOf returns the index at which the last occurrence of a value is found in a slice or return -1 // LastIndexOf returns the index at which the last occurrence of the item is found in a slice or return -1 if the then cannot be found.
// if the value cannot be found. func LastIndexOf[T comparable](slice []T, item T) int {
func LastIndexOf[T comparable](slice []T, value T) int {
for i := len(slice) - 1; i > 0; i-- { for i := len(slice) - 1; i > 0; i-- {
if value == slice[i] { if item == slice[i] {
return i return i
} }
} }
@@ -845,27 +840,27 @@ func LastIndexOf[T comparable](slice []T, value T) int {
} }
// ToSlicePointer returns a pointer to the slices of a variable parameter transformation // ToSlicePointer returns a pointer to the slices of a variable parameter transformation
func ToSlicePointer[T any](value ...T) []*T { func ToSlicePointer[T any](items ...T) []*T {
result := make([]*T, len(value)) result := make([]*T, len(items))
for i := range value { for i := range items {
result[i] = &value[i] result[i] = &items[i]
} }
return result return result
} }
// ToSlice returns a slices of a variable parameter transformation // ToSlice returns a slices of a variable parameter transformation
func ToSlice[T any](value ...T) []T { func ToSlice[T any](items ...T) []T {
result := make([]T, len(value)) result := make([]T, len(items))
copy(result, value) copy(result, items)
return result return result
} }
// AppendIfAbsent only absent append the value // AppendIfAbsent only absent append the item
func AppendIfAbsent[T comparable](slice []T, value T) []T { func AppendIfAbsent[T comparable](slice []T, item T) []T {
if !Contain(slice, value) { if !Contain(slice, item) {
slice = append(slice, value) slice = append(slice, item)
} }
return slice return slice
} }