diff --git a/README.md b/README.md index 8f43f51..52d7144 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ - [FindIndex](#findindex) - [FindLastIndex](#findlastindex) - [Filter](#filter) + - [Flat](#flat) - [排序](#排序) - [Sort 原生排序](#sort--原生排序) - [BubbleSort 冒泡排序](#bubblesort-冒泡排序) @@ -568,6 +569,40 @@ func main() { fmt.Printf("----- Filter Test End -----\n\n") } ``` +###### Flat + +> * 根据指定深度递归地将所有子数组元素拼接到新的数组中 +> +> * 返回新的数组 +> +> * depth 默认值为1 + +``` +fmt.Printf("----- Flat Test Start -----\n") +type AnySlice = []any +originArr := AnySlice{ + 1, + AnySlice{ + 2, + AnySlice{ + 3, + AnySlice{4}, + }, + 5, + }, +} +fmt.Printf("originArr: %v\n", originArr) // [1 [2 [3 [4]] 5]] +newArr := array.Flat(originArr) +fmt.Printf("newArr : %v\n", newArr) // [1 2 [3 [4]] 5] +newArr = array.Flat(originArr, 1) +fmt.Printf("newArr : %v\n", newArr) // [1 2 [3 [4]] 5] +newArr = array.Flat(originArr, 2) +fmt.Printf("newArr : %v\n", newArr) // [1 2 3 [4] 5] +newArr = array.Flat(originArr, 3) +fmt.Printf("newArr : %v\n", newArr) // [1 2 3 4 5] + +fmt.Printf("----- Flat Test End -----\n") +``` #### 排序 diff --git a/array.go b/array.go index 3a84aa8..a0ad99f 100644 --- a/array.go +++ b/array.go @@ -16,6 +16,42 @@ func As[T any](source []any) (newArr []T) { return } +// Flat 方法 根据指定深度递归地将所有子数组元素拼接到新的数组中 +// 返回新的数组 +// depth 默认值为1 + +func Flat(arr []any, depth ...int) []any { + finalDepth := 1 + if len(depth) > 0 { + finalDepth = depth[0] + if finalDepth < 1 { + panic("depth must be a positive integer greater than or equal to 1") + } + } + result := make([]any, 0) + + var flatten func([]any, int) + flatten = func(arr []any, depth int) { + length := len(arr) + for i := 0; i < length; i++ { + item := arr[i] + switch v := item.(type) { + case []any: + if depth > 0 { + flatten(v, depth-1) + } else { + result = append(result, v) + } + default: + result = append(result, item) + } + } + } + + flatten(arr, finalDepth) + return result +} + // Reverse 方法用于反转数组 // 注意:此方法会改变原数组,要在不改变原始数组的情况下反转数组中的元素,使用 toReversed()。 func Reverse[T any](arr *[]T, args ...T) { @@ -26,7 +62,7 @@ func Reverse[T any](arr *[]T, args ...T) { } } -//反转并返回新的数组 +// 反转并返回新的数组 func ToReversed[T any](arr []T, args ...T) (newArr []T) { length := len(arr) newArr = make([]T, length) diff --git a/array_test.go b/array_test.go index 03f6a47..1167d99 100644 --- a/array_test.go +++ b/array_test.go @@ -22,6 +22,34 @@ type TestIII struct { haha string } +func TestFlat(t *testing.T) { + fmt.Printf("----- Flat Test Start -----\n") + type AnySlice = []any + originArr := AnySlice{ + 1, + AnySlice{ + 2, + AnySlice{ + 3, + AnySlice{4}, + }, + 5, + }, + } + fmt.Printf("originArr: %v\n", originArr) // [1 [2 [3 [4]] 5]] + newArr := Flat(originArr) + fmt.Printf("newArr : %v\n", newArr) // [1 2 [3 [4]] 5] + newArr = Flat(originArr, 1) + fmt.Printf("newArr : %v\n", newArr) // [1 2 [3 [4]] 5] + newArr = Flat(originArr, 2) + fmt.Printf("newArr : %v\n", newArr) // [1 2 3 [4] 5] + newArr = Flat(originArr, 3) + fmt.Printf("newArr : %v\n", newArr) // [1 2 3 4 5] + + fmt.Printf("----- Flat Test End -----\n") + +} + func TestReverse(t *testing.T) { fmt.Printf("----- Reverse Test Start -----\n") originArr := []Test{ @@ -60,11 +88,11 @@ func TestToReversed(t *testing.T) { func TestMap(t *testing.T) { fmt.Printf("----- Map Test Start -----\n") originArr := []Test{ - Test{ + { id: 1, name: "A", }, - Test{ + { id: 2, name: "C", },