From bdbc06b095faa959bf893c3e1c027cb4d91d595d Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 18 May 2022 15:00:09 +0800 Subject: [PATCH] docs: add doc for funcs IndexOf and LastIndexOf --- docs/slice.md | 59 +++++++++++++++++++++++++++++++++++++++++++++ docs/slice_zh-CN.md | 58 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/docs/slice.md b/docs/slice.md index 98ee5a2..145294a 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -44,6 +44,8 @@ import ( - [InterfaceSlice](#InterfaceSlice) - [Intersection](#Intersection) - [InsertAt](#InsertAt) +- [IndexOf](#IndexOf) +- [LastIndexOf](#LastIndexOf) - [Map](#Map) - [Reverse](#Reverse) - [Reduce](#Reduce) @@ -704,6 +706,63 @@ 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[T any](slice []T, value T) int +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/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[T any](slice []T, value T) int +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/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 +} +``` + + + + ### Map

Creates an slice of values by running each element in slice thru function.

diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 3c1ba8f..0521bdd 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -44,6 +44,8 @@ import ( - [InterfaceSlice](#InterfaceSlice) - [Intersection](#Intersection) - [InsertAt](#InsertAt) +- [IndexOf](#IndexOf) +- [LastIndexOf](#LastIndexOf) - [Map](#Map) - [Reverse](#Reverse) - [Reduce](#Reduce) @@ -704,6 +706,62 @@ func main() { +### IndexOf +

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

+ +函数签名: + +```go +func IndexOf[T any](slice []T, value T) int +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/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[T any](slice []T, value T) int +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/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 +} +``` + + + ### Map

通过运行函数slice中的每个元素来创建一个新切片