mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 21:02:27 +08:00
refactor: refact Filter, ForEach, Map func with generics
This commit is contained in:
@@ -400,15 +400,15 @@ func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error
|
||||
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 Every(slice, function interface{}) bool //return true if all of the values in the slice pass the predicate function, function signature should be func(index int, value interface{}) bool
|
||||
func None(slice, function interface{}) bool // return true if all the values in the slice mismatch the criteria
|
||||
func Filter(slice, function interface{}) interface{} //filter slice, function signature should be func(index int, value interface{}) 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(slice, function interface{}) (interface{}, 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 FlattenDeep(slice interface{}) interface{} //flattens slice recursive
|
||||
func ForEach(slice, function interface{}) //iterates over elements of slice and invokes function for each element, function signature should be func(index int, value interface{})
|
||||
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 Map(slice, function interface{}) interface{} //map lisce, function signature should be func(index int, value interface{}) interface{}
|
||||
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 ReverseSlice(slice interface{}) //revere slice
|
||||
func Reduce(slice, function, zero interface{}) interface{} //reduce slice, function signature should be func(index int, value1, value2 interface{}) interface{}
|
||||
func Shuffle(slice interface{}) interface{} //creates an slice of shuffled values
|
||||
|
||||
@@ -167,7 +167,7 @@ func Some(slice, function interface{}) bool {
|
||||
|
||||
// Filter iterates over elements of slice, returning an slice of all elements `signature` returns truthy for.
|
||||
// The fn 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 {
|
||||
res := make([]T, 0, 0)
|
||||
for i, v := range slice {
|
||||
if fn(i, v) {
|
||||
@@ -279,7 +279,7 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
|
||||
|
||||
// ForEach iterates over elements of slice and invokes function for each element
|
||||
// The fn 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)) {
|
||||
for i, v := range slice {
|
||||
fn(i, v)
|
||||
}
|
||||
@@ -287,7 +287,7 @@ 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 `function`.
|
||||
// The fn 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 {
|
||||
res := make([]U, len(slice), cap(slice))
|
||||
for i, v := range slice {
|
||||
res[i] = fn(i, v)
|
||||
|
||||
Reference in New Issue
Block a user