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

refactor: rewrite DeleteByIndex, InsertByIndex, UpdateByIndex and Without func with generics

This commit is contained in:
dudaodong
2022-01-11 18:08:22 +08:00
parent 24c0d95112
commit 7ed173849a
4 changed files with 105 additions and 168 deletions

View File

@@ -396,8 +396,8 @@ func Contain[T comparable](slice []T, value T) bool //check if the value is in t
func Chunk[T any](slice []T, size int) [][]T //creates an slice of elements split into groups the length of size.
func ConvertSlice(originalSlice interface{}, newSliceType reflect.Type) interface{} //convert originalSlice to newSliceType
func Difference[T comparable](slice1, slice2 []T) []T //creates an slice of whose element not included in the other given slice
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) //delete the element of slice from start index to end index - 1
func Drop(slice interface{}, n int) interface{} //creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0
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.
@@ -406,8 +406,8 @@ 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
func InterfaceSlice(slice interface{}) []interface{} //convert value to interface{} slice
func Intersection(slices ...interface{}) interface{} //creates a slice of unique values that included by all slices.
func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //insert the element into slice at index.
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 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
@@ -415,10 +415,10 @@ 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 StringSlice(slice interface{}) []string //convert value to string slice
func Unique(slice interface{}) interface{} //remove duplicate elements in slice
func Union(slices ...interface{}) interface{} //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //update the slice element at index.
func Without(slice interface{}, values ...interface{}) interface{} //creates a slice excluding all given values
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(slice, function interface{}) (interface{}, interface{}) // 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
```