From f7e9d5dc475910c77f9eb7d24652bc8919532496 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 13 Aug 2024 11:00:47 +0800 Subject: [PATCH] doc: add doc for UniqueByComparator and UniqueByParallel --- README.md | 4 ++ README_zh-CN.md | 4 ++ docs/api/packages/slice.md | 69 +++++++++++++++++++++++ docs/en/api/packages/slice.md | 101 ++++++++++++++++++++++++++++------ 4 files changed, 162 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 66ec955..ef7a7be 100644 --- a/README.md +++ b/README.md @@ -1419,9 +1419,13 @@ import "github.com/duke-git/lancet/v2/slice" - **UniqueBy** : remove duplicate elements from the input slice based on the values returned by the iteratee function. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#UniqueBy)] [[play](https://go.dev/play/p/UR323iZLDpv)] +- **UniqueByComparator** : remove duplicate elements from the input slice using the provided comparator function. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#UniqueByComparator)] - **UniqueByField** : remove duplicate elements in struct slice by struct field. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#UniqueByField)] [[play](https://go.dev/play/p/6cifcZSPIGu)] +- **UniqueByParallel** : remove duplicate elements from the slice by parallel. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#UniqueByParallel)] - **Union** : creates a slice of unique elements, in order, from all given slices. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Union)] [[play](https://go.dev/play/p/hfXV1iRIZOf)] diff --git a/README_zh-CN.md b/README_zh-CN.md index 07550d7..0decb24 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -1420,9 +1420,13 @@ import "github.com/duke-git/lancet/v2/slice" - **UniqueBy** : 根据迭代函数返回的值,从输入切片中移除重复元素。此函数保持元素的顺序。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueBy)] [[play](https://go.dev/play/p/UR323iZLDpv)] +- **UniqueByComparator** : 使用提供的比较器函数从输入切片中移除重复元素。此函数保持元素的顺序。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueByComparator)] - **UniqueByField** : 根据struct字段对struct切片去重复。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueByField)] [[play](https://go.dev/play/p/6cifcZSPIGu)] +- **UniqueByParallel** : 并发的从输入切片中移除重复元素,结果保持元素的顺序。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueByParallel)] - **Union** : 合并多个切片。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Union)] [[play](https://go.dev/play/p/hfXV1iRIZOf)] diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index fcb59e6..3e618ea 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -86,7 +86,9 @@ import ( - [ToSlicePointer](#ToSlicePointer) - [Unique](#Unique) - [UniqueBy](#UniqueBy) +- [UniqueByComparator](#UniqueByComparator) - [UniqueByField](#UniqueByField) +- [UniqueByParallel](#UniqueByParallel) - [Union](#Union) - [UnionBy](#UnionBy) - [UpdateAt](#UpdateAt) @@ -2327,6 +2329,73 @@ func main() { } ``` +### UniqueByComparator + +

使用提供的比较器函数从输入切片中移除重复元素。此函数保持元素的顺序。

+ +函数签名: + +```go +func UniqueByComparator[T comparable](slice []T, comparator func(item T, other T) bool) []T +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + uniqueNums := slice.UniqueByComparator([]int{1, 2, 3, 1, 2, 4, 5, 6, 4}, func(item int, other int) bool { + return item == other + }) + + caseInsensitiveStrings := slice.UniqueByComparator([]string{"apple", "banana", "Apple", "cherry", "Banana", "date"}, func(item string, other string) bool { + return strings.ToLower(item) == strings.ToLower(other) + }) + + fmt.Println(uniqueNums) + fmt.Println(caseInsensitiveStrings) + + // Output: + // [1 2 3 4 5 6] + // [apple banana cherry date] +} +``` + +### UniqueByParallel + +

并发的从输入切片中移除重复元素,结果保持元素的顺序。

+ +函数签名: + +```go +func UniqueByParallel[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + nums := []int{1, 2, 3, 1, 2, 4, 5, 6, 4, 7} + numOfThreads := 4 + comparator := func(item int, other int) bool { return item == other } + + result := slice.UniqueByParallel(nums, numOfThreads, comparator) + + fmt.Println(result) + // Output: + // [1 2 3 4 5 6 7] +} +``` + ### UniqueByField

根据struct字段对struct切片去重复。

diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index 5ad9820..2f9722f 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -86,7 +86,9 @@ import ( - [ToSlicePointer](#ToSlicePointer) - [Unique](#Unique) - [UniqueBy](#UniqueBy) +- [UniqueByComparator](#UniqueByComparator) - [UniqueByField](#UniqueByField) +- [UniqueByParallel](#UniqueByParallel) - [Union](#Union) - [UnionBy](#UnionBy) - [UpdateAt](#UpdateAt) @@ -2325,6 +2327,73 @@ func main() { } ``` +### UniqueByComparator + +

Removes duplicate elements from the input slice using the provided comparator function. The function maintains the order of the elements.

+ +Signature: + +```go +func UniqueByComparator[T comparable](slice []T, comparator func(item T, other T) bool) []T +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + uniqueNums := slice.UniqueByComparator([]int{1, 2, 3, 1, 2, 4, 5, 6, 4}, func(item int, other int) bool { + return item == other + }) + + caseInsensitiveStrings := slice.UniqueByComparator([]string{"apple", "banana", "Apple", "cherry", "Banana", "date"}, func(item string, other string) bool { + return strings.ToLower(item) == strings.ToLower(other) + }) + + fmt.Println(uniqueNums) + fmt.Println(caseInsensitiveStrings) + + // Output: + // [1 2 3 4 5 6] + // [apple banana cherry date] +} +``` + +### UniqueByParallel + +

Removes duplicate elements from the slice by parallel.

+ +Signature: + +```go +func UniqueByParallel[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + nums := []int{1, 2, 3, 1, 2, 4, 5, 6, 4, 7} + numOfThreads := 4 + comparator := func(item int, other int) bool { return item == other } + + result := slice.UniqueByParallel(nums, numOfThreads, comparator) + + fmt.Println(result) + // Output: + // [1 2 3 4 5 6 7] +} +``` + ### UniqueByField

Remove duplicate elements in struct slice by struct field.

@@ -2647,14 +2716,14 @@ import ( func main() { strs := []string{"a", "b", "a", "c", "d", "a"} - modifiedStrs, count := slice.SetToDefaultIf(strs, func(s string) bool { return "a" == s }) - + modifiedStrs, count := slice.SetToDefaultIf(strs, func(s string) bool { return "a" == s }) + fmt.Println(modifiedStrs) - fmt.Println(count) - + fmt.Println(count) + // Output: - // [ b c d ] - // 3 + // [ b c d ] + // 3 } ``` @@ -2710,11 +2779,11 @@ import ( ) func main() { - nums := []int{1, 2, 3, 4, 5} - padded := slice.RightPadding(nums, 0, 3) - fmt.Println(padded) - // Output: - // [1 2 3 4 5 0 0 0] + nums := []int{1, 2, 3, 4, 5} + padded := slice.RightPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [1 2 3 4 5 0 0 0] } ``` @@ -2737,10 +2806,10 @@ import ( ) func main() { - nums := []int{1, 2, 3, 4, 5} - padded := slice.LeftPadding(nums, 0, 3) - fmt.Println(padded) - // Output: - // [0 0 0 1 2 3 4 5] + nums := []int{1, 2, 3, 4, 5} + padded := slice.LeftPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [0 0 0 1 2 3 4 5] } ``` \ No newline at end of file