diff --git a/docs/slice.md b/docs/slice.md index f82ff79..acdd7e3 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -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() { } ``` +### FilterMap + +

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.

+ +Signature: + +```go +func FilterMap[T any, U any](slice []T, iteratee func(index int, item T) (U, bool)) []U +``` + +Example: + +```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"} +} +``` + +### FlatMap + +

Manipulates a slice and transforms and flattens it to a slice of another type.

+ +Signature: + +```go +func FlatMap[T any, U any](slice []T, iteratee func(index int, item T) []U) []U +``` + +Example: + +```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"} +} +``` + ### Merge

Merge all given slices into one slice.

@@ -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) } diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 1e234c5..d9838f9 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -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() { } ``` +### FilterMap + +

返回一个将filter和map操作应用于给定切片的切片。 iteratee回调函数应该返回两个值:1,结果值。2,结果值是否应该被包含在返回的切片中。

+ +函数签名: + +```go +func FilterMap[T any, U any](slice []T, iteratee func(index int, item T) (U, bool)) []U +``` + +示例: + +```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"} +} +``` + +### FlatMap + +

将切片转换为其它类型切片。

+ +函数签名: + +```go +func FlatMap[T any, U any](slice []T, iteratee func(index int, item T) []U) []U +``` + +示例: + +```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"} +} +``` + ### Merge

合并多个切片(不会消除重复元素).