From b3a29ce82d79a8c63474e1fc0d5efdf644d30b9b Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 13 Jun 2022 15:14:37 +0800 Subject: [PATCH] docs: add doc for function Equal and EqualWithFunc --- docs/slice.md | 64 ++++++++++++++++++++++++++++++++++++++++++++ docs/slice_zh-CN.md | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) diff --git a/docs/slice.md b/docs/slice.md index 1ed157d..e966395 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -31,6 +31,8 @@ import ( - [DifferenceWith](#DifferenceWith) - [DeleteAt](#DeleteAt) - [Drop](#Drop) +- [Equal](#Equal) +- [EqualWithFunc](#EqualWithFunc) - [Every](#Every) - [Filter](#Filter) - [Find](#Find) @@ -360,6 +362,68 @@ func main() { +### Equal +

Check if two slices are equal: the same length and all elements' order and value are equal.

+ +Signature: + +```go +func Equal[T comparable](slice1, slice2 []T) bool +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + slice1 := []int{1, 2, 3} + slice2 := []int{1, 2, 3} + slice3 := []int{3, 2, 1} + + res1 := slice.Equal(slice1, slice2) + res2 := slice.Equal(slice1, slice3) + + fmt.Println(res1) //true + fmt.Println(res2) //false +} +``` + + + +### EqualWithFunc +

Check if two slices are equal with comparator func.

+ +Signature: + +```go +func EqualWithFunc[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + slice1 := []int{1, 2, 3} + slice2 := []int{2, 4, 6} + + isDouble := func(a, b int) bool { + return b == a*2 + } + + res := slice.EqualWithFunc(slice1, slice2, isDouble) + + fmt.Println(res) //true +} +``` + + ### Every

Return true if all of the values in the slice pass the predicate function.

diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 3c38c90..723fa0b 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -32,6 +32,8 @@ import ( - [DeleteAt](#DeleteAt) - [Drop](#Drop) - [Every](#Every) +- [Equal](#Equal) +- [EqualWithFunc](#EqualWithFunc) - [Filter](#Filter) - [Find](#Find) - [FindLast](#FindLast) @@ -393,6 +395,69 @@ func main() { +### Equal +

检查两个切片是否相等,相等条件:切片长度相同,元素顺序和值都相同

+ +函数签名: + +```go +func Equal[T comparable](slice1, slice2 []T) bool +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + slice1 := []int{1, 2, 3} + slice2 := []int{1, 2, 3} + slice3 := []int{3, 2, 1} + + res1 := slice.Equal(slice1, slice2) + res2 := slice.Equal(slice1, slice3) + + fmt.Println(res1) //true + fmt.Println(res2) //false +} +``` + + + +### EqualWithFunc +

检查两个切片是否相等,相等条件:对两个切片的元素调用比较函数comparator,返回true

+ +函数签名: + +```go +func EqualWithFunc[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + slice1 := []int{1, 2, 3} + slice2 := []int{2, 4, 6} + + isDouble := func(a, b int) bool { + return b == a*2 + } + + res := slice.EqualWithFunc(slice1, slice2, isDouble) + + fmt.Println(res) //true +} +``` + + + ### Filter

返回切片中通过predicate函数真值测试的所有元素