From 02fa7bc8be743ae762d65898ce6976d3f1bc869c Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 17 Jun 2022 16:57:42 +0800 Subject: [PATCH] docs: add doc for function Equal and EqualWithFunc --- docs/slice.md | 64 +++++++++++++++++++++++++++++++++++++++++++++ docs/slice_zh-CN.md | 64 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) diff --git a/docs/slice.md b/docs/slice.md index 70909ba..d390bd7 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -30,6 +30,8 @@ import ( - [DifferenceBy](#DifferenceBy) - [DeleteByIndex](#DeleteByIndex) - [Drop](#Drop) +- [Equal](#Equal) +- [EqualWith](#EqualWith) - [Every](#Every) - [Filter](#Filter) - [Find](#Find) @@ -332,6 +334,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(slice1, slice2 interface{}) 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 +} +``` + + + +### EqualWith +

Check if two slices are equal with comparator funcation.comparator signature: func(a interface{}, b interface{}) bool

+ +Signature: + +```go +func EqualWith(slice1, slice2 interface{}, comparator interface{}) 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.EqualWith(slice1, slice2, isDouble) + + fmt.Println(res) //true +} +``` + + ### Every

Return true if all of the values in the slice pass the predicate function. The function signature should be func(index int, value interface{}) bool.

diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 45dfb13..f4ac625 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -30,6 +30,8 @@ import ( - [DifferenceBy](#DifferenceBy) - [DeleteByIndex](#DeleteByIndex) - [Drop](#Drop) +- [Equal](#Equal) +- [EqualWith](#EqualWith) - [Every](#Every) - [Filter](#Filter) - [Find](#Find) @@ -329,6 +331,68 @@ func main() { +### Equal +

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

+ +函数签名: + +```go +func Equal(slice1, slice2 interface{}) 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 +} +``` + + + +### EqualWith +

检查两个切片是否相等,相等条件:对两个切片的元素调用比较函数comparator,返回true。 comparator函数签名: func(a interface{}, b interface{}) bool

+ +函数签名: + +```go +func EqualWith(slice1, slice2 interface{}, comparator interface{}) 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.EqualWith(slice1, slice2, isDouble) + + fmt.Println(res) //true +} +``` + + ### Every

如果切片中的所有值都通过谓词函数,则返回true。 函数签名应该是func(index int, value interface{}) bool