Skip to content

Commit

Permalink
feat: add Flat method
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoWu committed Jun 25, 2024
1 parent a38b562 commit eb1fcc4
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [FindIndex](#findindex)
- [FindLastIndex](#findlastindex)
- [Filter](#filter)
- [Flat](#flat)
- [排序](#排序)
- [Sort 原生排序](#sort--原生排序)
- [BubbleSort 冒泡排序](#bubblesort-冒泡排序)
Expand Down Expand Up @@ -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")
```


#### 排序
Expand Down
38 changes: 37 additions & 1 deletion array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
32 changes: 30 additions & 2 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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",
},
Expand Down

0 comments on commit eb1fcc4

Please sign in to comment.