From 2c7bcc9fbbadf1d7cb9e7bd204bdd696180888cd Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 29 Jul 2022 13:17:05 +0800 Subject: [PATCH] doc: update slice document --- docs/slice.md | 81 +++++++++++++++++++++++++++++++++++++++++++++ docs/slice_zh-CN.md | 80 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) diff --git a/docs/slice.md b/docs/slice.md index 94d3425..2d9463d 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -20,6 +20,7 @@ import (
## Index +- [AppendIfAbsent](#AppendIfAbsent) - [Contain](#Contain) - [ContainSubSlice](#ContainSubSlice) - [Chunk](#Chunk) @@ -53,6 +54,8 @@ import ( - [SortByField](#SortByField) - [Some](#Some) - [StringSlice](#StringSlice) +- [ToSlice](#ToSlice) +- [ToSlicePointer](#ToSlicePointer) - [Unique](#Unique) - [UniqueBy](#UniqueBy) - [Union](#Union) @@ -66,6 +69,35 @@ import ( ## Note: 1. param which type is interface{} in below functions should be slice. + + +### AppendIfAbsent +

If slice doesn't contain the value, append it to the slice.

+ +Signature: + +```go +func AppendIfAbsent[T comparable](slice []T, value T) []T +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + strs := []string{"a", "b"} + res1 := slice.AppendIfAbsent(strs, "a") + fmt.Println(res1) //[]string{"a", "b"} + + res2 := slice.AppendIfAbsent(strs, "cannot") + fmt.Println(res2"} +} +``` + + ### Contain

Check if the value is in the slice or not. iterableType param can be string, map or slice.

@@ -974,6 +1006,55 @@ func main() { +### ToSlice +

Returns a slices of a variable parameter transformation

+ +Signature: + +```go +func ToSlice[T any](value ...T) []T +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + res := slice.ToSlice("a", "b") + fmt.Println(res) //{"a", "b"} +} +``` + + + +### ToSlicePointer +

Returns a pointer to the slices of a variable parameter transformation

+ +Signature: + +```go +func ToSlicePointer[T any](value ...T) []*T +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + str1 := "a" + str2 := "b" + res := slice.ToSlicePointer(str1, str2) + fmt.Println(res) // res -> []*string{&str1, &str2} +} +``` + + ### Unique

Remove duplicate elements in slice.

diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index ca228b9..dbf0e16 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -20,6 +20,7 @@ import (
## 目录 +- [AppendIfAbsent](#AppendIfAbsent) - [Contain](#Contain) - [ContainSubSlice](#ContainSubSlice) - [Chunk](#Chunk) @@ -53,6 +54,8 @@ import ( - [SortByField](#SortByField) - [Some](#Some) - [StringSlice](#StringSlice) +- [ToSlice](#ToSlice) +- [ToSlicePointer](#ToSlicePointer) - [Unique](#Unique) - [UniqueBy](#UniqueBy) - [Union](#Union) @@ -63,6 +66,34 @@ import ( ## 文档 +### AppendIfAbsent +

当前切片中不包含值时,将该值追加到切片中

+ +函数签名: + +```go +func AppendIfAbsent[T comparable](slice []T, value T) []T +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/slice" +) + +func main() { + strs := []string{"a", "b"} + res1 := slice.AppendIfAbsent(strs, "a") + fmt.Println(res1) //[]string{"a", "b"} + + res2 := slice.AppendIfAbsent(strs, "cannot") + fmt.Println(res2"} +} +``` + + + ### Contain

判断slice是否包含value

@@ -974,6 +1005,55 @@ func main() { +### ToSlice +

将可变参数转为切片

+ +函数签名: + +```go +func ToSlice[T any](value ...T) []T +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + res := slice.ToSlice("a", "b") + fmt.Println(res) //{"a", "b"} +} +``` + + + +### ToSlicePointer +

将可变参数转为指针切片

+ +函数签名: + +```go +func ToSlicePointer[T any](value ...T) []*T +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + str1 := "a" + str2 := "b" + res := slice.ToSlicePointer(str1, str2) + fmt.Println(res) // res -> []*string{&str1, &str2} +} +``` + + ### Unique

删除切片中的重复元素