diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index 0f31936..9731825 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -91,6 +91,7 @@ import ( - [Without](#Without) - [KeyBy](#KeyBy) - [Join](#Join) +- [Partition](#Partition)
@@ -2452,3 +2453,39 @@ func main() { // 1-2-3-4-5 } ``` + +### Partition + +根据给定的predicate判断函数分组切片元素。
+ +函数签名: + +```go +func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + + result1 := slice.Partition(nums) + result2 := slice.Partition(nums, func(n int) bool { return n%2 == 0 }) + result3 := slice.Partition(nums, func(n int) bool { return n == 1 || n == 2 }, func(n int) bool { return n == 2 || n == 3 || n == 4 }) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // [[1 2 3 4 5]] + // [[2 4] [1 3 5]] + // [[1 2] [3 4] [5]] +} +``` \ No newline at end of file diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index b62479c..200e047 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -2450,3 +2450,39 @@ func main() { // 1-2-3-4-5 } ``` + +### Partition + +Partition all slice elements with the evaluation of the given predicate functions.
+ +Signature: + +```go +func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + + result1 := slice.Partition(nums) + result2 := slice.Partition(nums, func(n int) bool { return n%2 == 0 }) + result3 := slice.Partition(nums, func(n int) bool { return n == 1 || n == 2 }, func(n int) bool { return n == 2 || n == 3 || n == 4 }) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // [[1 2 3 4 5]] + // [[2 4] [1 3 5]] + // [[1 2] [3 4] [5]] +} +``` \ No newline at end of file