1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-06 21:52:28 +08:00

doc: add doc for FilterMap and FlatMap

This commit is contained in:
dudaodong
2023-02-24 11:23:02 +08:00
parent f3801bcd8f
commit e523d4af6e
2 changed files with 146 additions and 2 deletions

View File

@@ -56,6 +56,8 @@ import (
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [Map](#Map)
- [FilterMap](#FilterMap)
- [FlatMap](#FlatMap)
- [Merge](#Merge)
- [Reverse](#Reverse)
- [Reduce](#Reduce)
@@ -1252,6 +1254,76 @@ func main() {
}
```
### <span id="FilterMap">FilterMap</span>
<p>Returns a slice which apply both filtering and mapping to the given slice. iteratee callback function should returntwo values: 1, mapping result. 2, whether the result element should be included or not.</p>
<b>Signature:</b>
```go
func FilterMap[T any, U any](slice []T, iteratee func(index int, item T) (U, bool)) []U
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5}
getEvenNumStr := func(i, num int) (string, bool) {
if num%2 == 0 {
return strconv.FormatInt(int64(num), 10), true
}
return "", false
}
result := slice.FilterMap(nums, getEvenNumStr)
fmt.Printf("%#v", result)
// Output:
// []string{"2", "4"}
}
```
### <span id="FlatMap">FlatMap</span>
<p>Manipulates a slice and transforms and flattens it to a slice of another type.</p>
<b>Signature:</b>
```go
func FlatMap[T any, U any](slice []T, iteratee func(index int, item T) []U) []U
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4}
result := slice.FlatMap(nums, func(i int, num int) []string {
s := "hi-" + strconv.FormatInt(int64(num), 10)
return []string{s}
})
fmt.Printf("%#v", result)
// Output:
// []string{"hi-1", "hi-2", "hi-3", "hi-4"}
}
```
### <span id="Merge">Merge</span>
<p>Merge all given slices into one slice.</p>
@@ -1467,8 +1539,8 @@ func main() {
nums := []int{1, 2, 3, 4, 5}
result := slice.Shuffle(nums)
fmt.Println(res)
fmt.Println(res)
// Output:
// [3 1 5 4 2] (random order)
}

View File

@@ -56,6 +56,8 @@ import (
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [Map](#Map)
- [FilterMap](#FilterMap)
- [FlatMap](#FlatMap)
- [Merge](#Merge)
- [Reverse](#Reverse)
- [Reduce](#Reduce)
@@ -1254,6 +1256,76 @@ func main() {
}
```
### <span id="FilterMap">FilterMap</span>
<p>返回一个将filter和map操作应用于给定切片的切片。 iteratee回调函数应该返回两个值1结果值。2结果值是否应该被包含在返回的切片中。</p>
<b>函数签名:</b>
```go
func FilterMap[T any, U any](slice []T, iteratee func(index int, item T) (U, bool)) []U
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5}
getEvenNumStr := func(i, num int) (string, bool) {
if num%2 == 0 {
return strconv.FormatInt(int64(num), 10), true
}
return "", false
}
result := slice.FilterMap(nums, getEvenNumStr)
fmt.Printf("%#v", result)
// Output:
// []string{"2", "4"}
}
```
### <span id="FlatMap">FlatMap</span>
<p>将切片转换为其它类型切片。</p>
<b>函数签名:</b>
```go
func FlatMap[T any, U any](slice []T, iteratee func(index int, item T) []U) []U
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4}
result := slice.FlatMap(nums, func(i int, num int) []string {
s := "hi-" + strconv.FormatInt(int64(num), 10)
return []string{s}
})
fmt.Printf("%#v", result)
// Output:
// []string{"hi-1", "hi-2", "hi-3", "hi-4"}
}
```
### <span id="Merge">Merge</span>
<p>合并多个切片(不会消除重复元素).</p>