mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
docs: add GroupWith doc
This commit is contained in:
@@ -37,8 +37,9 @@ import (
|
|||||||
- [FindLast](#FindLast)
|
- [FindLast](#FindLast)
|
||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
|
|
||||||
- [GroupBy](#GroupBy)
|
- [GroupBy](#GroupBy)
|
||||||
|
- [GroupWith](#GroupWith)
|
||||||
- [IntSlice](#IntSlice)
|
- [IntSlice](#IntSlice)
|
||||||
- [InterfaceSlice](#InterfaceSlice)
|
- [InterfaceSlice](#InterfaceSlice)
|
||||||
- [Intersection](#Intersection)
|
- [Intersection](#Intersection)
|
||||||
@@ -564,6 +565,34 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GroupWith">GroupWith</span>
|
||||||
|
<p>Return a map composed of keys generated from the results of running each element of slice thru iteratee.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nums := []float64{6.1, 4.2, 6.3}
|
||||||
|
floor := func(num float64) float64 {
|
||||||
|
return math.Floor(num)
|
||||||
|
}
|
||||||
|
res := slice.GroupWith(nums, floor)
|
||||||
|
fmt.Println(res) //map[float64][]float64{ 4: {4.2}, 6: {6.1, 6.3},}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IntSlice">IntSlice</span>
|
### <span id="IntSlice">IntSlice</span>
|
||||||
<p>Convert interface slice to int slice.</p>
|
<p>Convert interface slice to int slice.</p>
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import (
|
|||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
|
|
||||||
- [GroupBy](#GroupBy)
|
- [GroupBy](#GroupBy)
|
||||||
|
- [GroupWith](#GroupWith)
|
||||||
- [IntSlice](#IntSlice)
|
- [IntSlice](#IntSlice)
|
||||||
- [InterfaceSlice](#InterfaceSlice)
|
- [InterfaceSlice](#InterfaceSlice)
|
||||||
- [Intersection](#Intersection)
|
- [Intersection](#Intersection)
|
||||||
@@ -565,6 +566,32 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GroupWith">GroupWith</span>
|
||||||
|
<p>创建一个map,key是iteratee遍历slice中的每个元素返回的结果。 分组值的顺序是由他们出现在slice中的顺序确定的。每个键对应的值负责生成key的元素组成的数组。iteratee调用1个参数: (value)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nums := []float64{6.1, 4.2, 6.3}
|
||||||
|
floor := func(num float64) float64 {
|
||||||
|
return math.Floor(num)
|
||||||
|
}
|
||||||
|
res := slice.GroupWith(nums, floor)
|
||||||
|
fmt.Println(res) //map[float64][]float64{ 4: {4.2}, 6: {6.1, 6.3},}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="IntSlice">IntSlice</span>
|
### <span id="IntSlice">IntSlice</span>
|
||||||
<p>将接口切片转换为int切片</p>
|
<p>将接口切片转换为int切片</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user