1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

Compare commits

...

4 Commits

Author SHA1 Message Date
dudaodong
b309044981 doc: add doc for Partition 2023-09-04 16:23:23 +08:00
dudaodong
541e6d4ea3 feat: add Partition for slice 2023-09-04 11:30:50 +08:00
dudaodong
4037b96cc4 refactor: update SortByField 2023-09-04 11:03:22 +08:00
dudaodong
8e484c4a6f doc: update doc site 2023-09-04 10:31:02 +08:00
6 changed files with 148 additions and 18 deletions

View File

@@ -46,7 +46,7 @@ import (
func RandBytes(length int) []byte
```
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/EkiLESeXf8d)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/EkiLESeXf8d)</span></b>
```go
package main
@@ -72,7 +72,7 @@ func main() {
func RandInt(min, max int) int
```
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/pXyyAAI5YxD)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/pXyyAAI5YxD)</span></b>
```go
package main
@@ -98,7 +98,7 @@ func main() {
func RandString(length int) string
```
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/W2xvRUXA7Mi)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/W2xvRUXA7Mi)</span></b>
```go
package main
@@ -124,7 +124,7 @@ func main() {
func RandUpper(length int) string
```
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/29QfOh0DVuh)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/29QfOh0DVuh)</span></b>
```go
package main
@@ -150,7 +150,7 @@ func main() {
func RandLower(length int) string
```
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/XJtZ471cmtI)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/XJtZ471cmtI)</span></b>
```go
package main
@@ -176,7 +176,7 @@ func main() {
func RandNumeral(length int) string
```
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/g4JWVpHsJcf)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/g4JWVpHsJcf)</span></b>
```go
package main
@@ -202,7 +202,7 @@ func main() {
func RandNumeralOrLetter(length int) string
```
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/19CEQvpx2jD)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/19CEQvpx2jD)</span></b>
```go
package main
@@ -228,7 +228,7 @@ func main() {
func UUIdV4() (string, error)
```
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/_Z9SFmr28ft)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/_Z9SFmr28ft)</span></b>
```go
package main
@@ -257,7 +257,7 @@ func main() {
func RandUniqueIntSlice(n, min, max int) []int
```
<b>示例:<span class="run-container">[运行](https://go.dev/play/p/uBkRSOz73Ec)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/uBkRSOz73Ec)</span></b>
```go
package main

View File

@@ -77,7 +77,7 @@ import (
- [IsSortedByKey](#IsSortedByKey)
- [Sort](#Sort)
- [SortBy](#SortBy)
- [SortByField<sup>deprecated</sup>](#SortByField)
- [SortByField](#SortByField)
- [Some](#Some)
- [StringSlice<sup>deprecated</sup>](#StringSlice)
- [SymmetricDifference](#SymmetricDifference)
@@ -91,6 +91,7 @@ import (
- [Without](#Without)
- [KeyBy](#KeyBy)
- [Join](#Join)
- [Partition](#Partition)
<div STYLE="page-break-after: always;"></div>
@@ -2002,9 +2003,9 @@ func main() {
}
```
### <span id="SortByField">SortByField (已弃用: 请使用 Sort 或 SortBy 代替该方法)</span>
### <span id="SortByField">SortByField</span>
<p>按字段对结构切片进行排序。slice元素应为struct字段类型应为int、uint、string或bool。 默认排序类型是升序asc如果是降序设置 sortType 为 desc</p>
<p>按字段对结构切片进行排序。slice元素应为struct排序字段field类型应为int、uint、string或bool。 默认排序类型是升序asc如果是降序设置 sortType 为 desc</p>
<b>函数签名:</b>
@@ -2452,3 +2453,39 @@ func main() {
// 1-2-3-4-5
}
```
### <span id="Partition">Partition</span>
<p>根据给定的predicate判断函数分组切片元素。</p>
<b>函数签名:</b>
```go
func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T
```
<b>示例:</b>
```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]]
}
```

View File

@@ -77,7 +77,7 @@ import (
- [IsSortedByKey](#IsSortedByKey)
- [Sort](#Sort)
- [SortBy](#SortBy)
- [SortByField<sup>deprecated</sup>](#SortByField)
- [SortByField](#SortByField)
- [Some](#Some)
- [StringSlice<sup>deprecated</sup>](#StringSlice)
- [SymmetricDifference](#SymmetricDifference)
@@ -2000,9 +2000,9 @@ func main() {
}
```
### <span id="SortByField">SortByField (Deprecated: use Sort and SortBy for replacement)</span>
### <span id="SortByField">SortByField</span>
<p>Sort struct slice by field. Slice element should be struct, field type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set sortType to desc.</p>
<p>Sort struct slice by field. Slice element should be struct, `field` param type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set `sortType` param to desc.</p>
<b>Signature:</b>
@@ -2450,3 +2450,39 @@ func main() {
// 1-2-3-4-5
}
```
### <span id="Partition">Partition</span>
<p>Partition all slice elements with the evaluation of the given predicate functions. </p>
<b>Signature:</b>
```go
func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T
```
<b>Example:</b>
```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]]
}
```

View File

@@ -1008,7 +1008,7 @@ func SortBy[T any](slice []T, less func(a, b T) bool) {
// default sortType is ascending (asc), if descending order, set sortType to desc
// This function is deprecated, use Sort and SortBy for replacement.
// Play: https://go.dev/play/p/fU1prOBP9p1
func SortByField(slice any, field string, sortType ...string) error {
func SortByField[T any](slice []T, field string, sortType ...string) error {
sv := sliceValue(slice)
t := sv.Type().Elem()
@@ -1192,10 +1192,40 @@ func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T {
// Join the slice item with specify separator.
// Play: https://go.dev/play/p/huKzqwNDD7V
func Join[T any](s []T, separator string) string {
str := Map(s, func(_ int, item T) string {
func Join[T any](slice []T, separator string) string {
str := Map(slice, func(_ int, item T) string {
return fmt.Sprint(item)
})
return strings.Join(str, separator)
}
// Partition all slice elements with the evaluation of the given predicate functions.
// todo
func Partition[T any](slice []T, predicates ...func(item T) bool) [][]T {
l := len(predicates)
result := make([][]T, l+1)
for _, item := range slice {
processed := false
for i, f := range predicates {
if f == nil {
panic("predicate function must not be nill")
}
if f(item) {
result[i] = append(result[i], item)
processed = true
break
}
}
if !processed {
result[l] = append(result[l], item)
}
}
return result
}

View File

@@ -1046,3 +1046,20 @@ func ExampleJoin() {
// 1,2,3,4,5
// 1-2-3-4-5
}
func ExamplePartition() {
nums := []int{1, 2, 3, 4, 5}
result1 := Partition(nums)
result2 := Partition(nums, func(n int) bool { return n%2 == 0 })
result3 := 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]]
}

View File

@@ -1175,3 +1175,13 @@ func TestJoin(t *testing.T) {
assert.Equal("1,2,3,4,5", result1)
assert.Equal("1-2-3-4-5", result2)
}
func TestPartition(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestPartition")
assert.Equal([][]int{{1, 2, 3, 4, 5}}, Partition([]int{1, 2, 3, 4, 5}))
assert.Equal([][]int{{2, 4}, {1, 3, 5}}, Partition([]int{1, 2, 3, 4, 5}, func(n int) bool { return n%2 == 0 }))
assert.Equal([][]int{{1, 2}, {3, 4}, {5}}, Partition([]int{1, 2, 3, 4, 5}, func(n int) bool { return n == 1 || n == 2 }, func(n int) bool { return n == 2 || n == 3 || n == 4 }))
}