From c3372e18b1c6480b83cbe3a0342e5e2d37d745c6 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 6 Sep 2024 15:06:35 +0800 Subject: [PATCH] feat: add Frequency in slice package --- docs/api/packages/slice.md | 38 ++++++++++++++++++++++--- docs/en/api/packages/slice.md | 38 ++++++++++++++++++++++--- slice/slice.go | 12 ++++++++ slice/slice_example_test.go | 10 +++++++ slice/slice_test.go | 52 ++++++++++++++++++++++++++++++++++- 5 files changed, 141 insertions(+), 9 deletions(-) diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index 714c42f..7b87d05 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -105,6 +105,7 @@ import ( - [Break](#Break) - [RightPadding](#RightPadding) - [LeftPadding](#LeftPadding) +- [Frequency](#Frequency)
@@ -2902,7 +2903,7 @@ func main() { } ``` -RightPadding +### RightPadding

在切片的右部添加元素。

@@ -2921,7 +2922,7 @@ import ( ) func main() { - nums := []int{1, 2, 3, 4, 5} + nums := []int{1, 2, 3, 4, 5} padded := slice.RightPadding(nums, 0, 3) fmt.Println(padded) // Output: @@ -2929,7 +2930,7 @@ func main() { } ``` -LeftPadding +### LeftPadding

在切片的左部添加元素。

@@ -2948,10 +2949,39 @@ import ( ) func main() { - nums := []int{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] } +``` + +### Frequency + +

计算切片中每个元素出现的频率。

+ +函数签名: + +```go +func Frequency[T comparable](slice []T) map[T]int +``` + +示例:[运行]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + strs := []string{"a", "b", "b", "c", "c", "c"} + result := slice.Frequency(strs) + + fmt.Println(result) + + // Output: + // map[a:1 b:2 c:3] +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index 9fdda7a..1e6e7a6 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -105,6 +105,7 @@ import ( - [Break](#Break) - [RightPadding](#RightPadding) - [LeftPadding](#LeftPadding) +- [Frequency](#Frequency)
@@ -2897,7 +2898,7 @@ func main() { } ``` -RightPadding +### RightPadding

RightPadding adds padding to the right end of a slice.

@@ -2916,7 +2917,7 @@ import ( ) func main() { - nums := []int{1, 2, 3, 4, 5} + nums := []int{1, 2, 3, 4, 5} padded := slice.RightPadding(nums, 0, 3) fmt.Println(padded) // Output: @@ -2924,7 +2925,7 @@ func main() { } ``` -LeftPadding +### LeftPadding

LeftPadding adds padding to the left begin of a slice.

@@ -2943,10 +2944,39 @@ import ( ) func main() { - nums := []int{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] } +``` + +### Frequency + +

Counts the frequency of each element in the slice.

+ +Signature: + +```go +func Frequency[T comparable](slice []T) map[T]int +``` + +Example:[Run]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + strs := []string{"a", "b", "b", "c", "c", "c"} + result := slice.Frequency(strs) + + fmt.Println(result) + + // Output: + // map[a:1 b:2 c:3] +} ``` \ No newline at end of file diff --git a/slice/slice.go b/slice/slice.go index 4f2d7f3..7847b26 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -1386,3 +1386,15 @@ func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T { return paddedSlice } + +// Frequency counts the frequency of each element in the slice. +// Play: todo +func Frequency[T comparable](slice []T) map[T]int { + result := make(map[T]int) + + for _, v := range slice { + result[v]++ + } + + return result +} diff --git a/slice/slice_example_test.go b/slice/slice_example_test.go index b20a4b4..132c4d6 100644 --- a/slice/slice_example_test.go +++ b/slice/slice_example_test.go @@ -1251,3 +1251,13 @@ func ExampleMapConcurrent() { // Output: // [1 4 9 16 25 36] } + +func ExampleFrequency() { + strs := []string{"a", "b", "b", "c", "c", "c"} + result := Frequency(strs) + + fmt.Println(result) + + // Output: + // map[a:1 b:2 c:3] +} diff --git a/slice/slice_test.go b/slice/slice_test.go index 5a59080..74e55b4 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -1764,5 +1764,55 @@ func TestFilterConcurrent(t *testing.T) { actual := FilterConcurrent(nums, func(_, n int) bool { return n > 3 }, 4) assert.Equal(expected, actual) }) - +} + +func TestFrequency(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestFrequency") + + t.Run("empty slice", func(t *testing.T) { + result := Frequency([]int{}) + assert.Equal(map[int]int{}, result) + }) + + t.Run("int slice", func(t *testing.T) { + nums := []int{1, 2, 2, 3, 3, 3} + expected := map[int]int{1: 1, 2: 2, 3: 3} + result := Frequency(nums) + + assert.Equal(expected, result) + }) + + t.Run("string slice", func(t *testing.T) { + strs := []string{"a", "b", "b", "c", "c", "c"} + expected := map[string]int{"a": 1, "b": 2, "c": 3} + result := Frequency(strs) + + assert.Equal(expected, result) + }) + + t.Run("struct slice", func(t *testing.T) { + type student struct { + Name string + Age int + } + + students := []student{ + {Name: "a", Age: 11}, + {Name: "b", Age: 12}, + {Name: "a", Age: 13}, + {Name: "c", Age: 14}, + } + + expected := map[student]int{ + {Name: "a", Age: 11}: 1, + {Name: "a", Age: 13}: 1, + {Name: "b", Age: 12}: 1, + {Name: "c", Age: 14}: 1, + } + result := Frequency(students) + + assert.Equal(expected, result) + }) }