Skip to content

Commit

Permalink
doc: add doc for UniqueByComparator and UniqueByParallel
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Aug 13, 2024
1 parent 5c580ed commit f7e9d5d
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 16 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1419,9 +1419,13 @@ import "github.com/duke-git/lancet/v2/slice"
- **<big>UniqueBy</big>** : 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)]
- **<big>UniqueByComparator</big>** : 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)]
- **<big>UniqueByField</big>** : 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)]
- **<big>UniqueByParallel</big>** : remove duplicate elements from the slice by parallel.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#UniqueByParallel)]
- **<big>Union</big>** : 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)]
Expand Down
4 changes: 4 additions & 0 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1420,9 +1420,13 @@ import "github.com/duke-git/lancet/v2/slice"
- **<big>UniqueBy</big>** : 根据迭代函数返回的值,从输入切片中移除重复元素。此函数保持元素的顺序。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueBy)]
[[play](https://go.dev/play/p/UR323iZLDpv)]
- **<big>UniqueByComparator</big>** : 使用提供的比较器函数从输入切片中移除重复元素。此函数保持元素的顺序。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueByComparator)]
- **<big>UniqueByField</big>** : 根据struct字段对struct切片去重复。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueByField)]
[[play](https://go.dev/play/p/6cifcZSPIGu)]
- **<big>UniqueByParallel</big>** : 并发的从输入切片中移除重复元素,结果保持元素的顺序。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueByParallel)]
- **<big>Union</big>** : 合并多个切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Union)]
[[play](https://go.dev/play/p/hfXV1iRIZOf)]
Expand Down
69 changes: 69 additions & 0 deletions docs/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ import (
- [ToSlicePointer](#ToSlicePointer)
- [Unique](#Unique)
- [UniqueBy](#UniqueBy)
- [UniqueByComparator](#UniqueByComparator)
- [UniqueByField](#UniqueByField)
- [UniqueByParallel](#UniqueByParallel)
- [Union](#Union)
- [UnionBy](#UnionBy)
- [UpdateAt](#UpdateAt)
Expand Down Expand Up @@ -2327,6 +2329,73 @@ func main() {
}
```

### <span id="UniqueByComparator">UniqueByComparator</span>

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

<b>函数签名:</b>

```go
func UniqueByComparator[T comparable](slice []T, comparator func(item T, other T) bool) []T
```

<b>示例:</b>

```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]
}
```

### <span id="UniqueByParallel">UniqueByParallel</span>

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

<b>函数签名:</b>

```go
func UniqueByParallel[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T
```

<b>示例:</b>

```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]
}
```

### <span id="UniqueByField">UniqueByField</span>

<p>根据struct字段对struct切片去重复。</p>
Expand Down
101 changes: 85 additions & 16 deletions docs/en/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ import (
- [ToSlicePointer](#ToSlicePointer)
- [Unique](#Unique)
- [UniqueBy](#UniqueBy)
- [UniqueByComparator](#UniqueByComparator)
- [UniqueByField](#UniqueByField)
- [UniqueByParallel](#UniqueByParallel)
- [Union](#Union)
- [UnionBy](#UnionBy)
- [UpdateAt](#UpdateAt)
Expand Down Expand Up @@ -2325,6 +2327,73 @@ func main() {
}
```

### <span id="UniqueByComparator">UniqueByComparator</span>

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

<b>Signature:</b>

```go
func UniqueByComparator[T comparable](slice []T, comparator func(item T, other T) bool) []T
```

<b>Example:</b>

```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]
}
```

### <span id="UniqueByParallel">UniqueByParallel</span>

<p>Removes duplicate elements from the slice by parallel.</p>

<b>Signature:</b>

```go
func UniqueByParallel[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T
```

<b>Example:</b>

```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]
}
```

### <span id="UniqueByField">UniqueByField</span>

<p>Remove duplicate elements in struct slice by struct field.</p>
Expand Down Expand Up @@ -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
}
```

Expand Down Expand Up @@ -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]
}
```

Expand All @@ -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]
}
```

0 comments on commit f7e9d5d

Please sign in to comment.