From 885c08847d9bd42813a4bd52146568653c4ed69b Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 17 Jun 2022 17:11:09 +0800 Subject: [PATCH] docs: add doc for function IndexOf and LastIndexOf --- docs/slice.md | 62 ++++++++++++++++++++++++++++++++++++++++++--- docs/slice_zh-CN.md | 61 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 117 insertions(+), 6 deletions(-) diff --git a/docs/slice.md b/docs/slice.md index d390bd7..d425791 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -44,6 +44,8 @@ import ( - [InterfaceSlice](#InterfaceSlice) - [Intersection](#Intersection) - [InsertByIndex](#InsertByIndex) +- [IndexOf](#IndexOf) +- [LastIndexOf](#LastIndexOf) - [Map](#Map) - [ReverseSlice](#ReverseSlice) - [Reduce](#Reduce) @@ -347,7 +349,7 @@ func Equal(slice1, slice2 interface{}) bool ```go import ( "fmt" - "github.com/duke-git/lancet/v2/slice" + "github.com/duke-git/lancet/slice" ) func main() { @@ -378,7 +380,7 @@ func EqualWith(slice1, slice2 interface{}, comparator interface{}) bool ```go import ( "fmt" - "github.com/duke-git/lancet/v2/slice" + "github.com/duke-git/lancet/slice" ) func main() { @@ -542,8 +544,6 @@ func main() { - - ### ForEach

Iterates over elements of slice and invokes function for each element, function signature should be func(index int, value interface{}).

@@ -683,6 +683,60 @@ func main() { +### IndexOf +

Returns the index at which the first occurrence of a value is found in a slice or return -1 if the value cannot be found.

+ +Signature: + +```go +func IndexOf(slice, value interface{}) int +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + arr := []string{"a", "a", "b", "c"} + res1 := slice.IndexOf(arr, "a") + fmt.Println(res1) //0 + + res2 := slice.IndexOf(arr, "d") + fmt.Println(res2) //-1 +} +``` + + + +### LastIndexOf +

Returns the index at which the last occurrence of a value is found in a slice or return -1 if the value cannot be found.

+ +Signature: + +```go +func LastIndexOf(slice, value interface{}) int +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + arr := []string{"a", "a", "b", "c"} + res1 := slice.LastIndexOf(arr, "a") + fmt.Println(res1) //1 + + res2 := slice.LastIndexOf(arr, "d") + fmt.Println(res2) //-1 +} +``` + ### InsertByIndex

insert the element into slice at index.

diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index f4ac625..0a81a5f 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -44,6 +44,8 @@ import ( - [InterfaceSlice](#InterfaceSlice) - [Intersection](#Intersection) - [InsertByIndex](#InsertByIndex) +- [IndexOf](#IndexOf) +- [LastIndexOf](#LastIndexOf) - [Map](#Map) - [ReverseSlice](#ReverseSlice) - [Reduce](#Reduce) @@ -344,7 +346,7 @@ func Equal(slice1, slice2 interface{}) bool ```go import ( "fmt" - "github.com/duke-git/lancet/v2/slice" + "github.com/duke-git/lancet/slice" ) func main() { @@ -375,7 +377,7 @@ func EqualWith(slice1, slice2 interface{}, comparator interface{}) bool ```go import ( "fmt" - "github.com/duke-git/lancet/v2/slice" + "github.com/duke-git/lancet/slice" ) func main() { @@ -680,6 +682,61 @@ func main() { +### IndexOf +

返回在切片中找到值的第一个匹配项的索引,如果找不到值,则返回-1

+ +函数签名: + +```go +func IndexOf(slice, value interface{}) int +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + arr := []string{"a", "a", "b", "c"} + res1 := slice.IndexOf(arr, "a") + fmt.Println(res1) //0 + + res2 := slice.IndexOf(arr, "d") + fmt.Println(res2) //-1 +} +``` + + + +### LastIndexOf +

返回在切片中找到最后一个值的索引,如果找不到该值,则返回-1

+ +函数签名: + +```go +func LastIndexOf(slice, value interface{}) int +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + arr := []string{"a", "a", "b", "c"} + res1 := slice.LastIndexOf(arr, "a") + fmt.Println(res1) //1 + + res2 := slice.LastIndexOf(arr, "d") + fmt.Println(res2) //-1 +} +``` + + ### InsertByIndex

将元素插入到索引处的切片中