From 6248293c49a1cc192a737a79ab88aff34db8a413 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 17 Oct 2022 10:40:35 +0800 Subject: [PATCH] doc: add docment for Replace and ReplaceAll --- docs/slice.md | 56 ++++++++++++++++++++++++++++++++++++++++++++ docs/slice_zh-CN.md | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/docs/slice.md b/docs/slice.md index b5b69ab..03cb09a 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -53,6 +53,8 @@ import ( - [Map](#Map) - [Reverse](#Reverse) - [Reduce](#Reduce) +- [Replace](#Replace) +- [ReplaceAll](#ReplaceAll) - [Shuffle](#Shuffle) - [SortByField](#SortByField) - [Some](#Some) @@ -966,6 +968,60 @@ func main() { +### Replace +

Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.

+ +Signature: + +```go +func Replace[T comparable](slice []T, old T, new T, n int) []T +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + strs := []string{"a", "b", "a", "c", "d", "a"} + + fmt.Println(slice.Replace(strs, "a", "x", 0)) //{"a", "b", "a", "c", "d", "a"} + + fmt.Println(slice.Replace(strs, "a", "x", 1)) //{"x", "b", "a", "c", "d", "a"} + + fmt.Println(slice.Replace(strs, "a", "x", -1)) //{"x", "b", "x", "c", "d", "x"} +} +``` + + + +### ReplaceAll +

Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.

+ +Signature: + +```go +func ReplaceAll[T comparable](slice []T, old T, new T) []T +``` +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + strs := []string{"a", "b", "a", "c", "d", "a"} + + fmt.Println(slice.ReplaceAll(strs, "a", "x")) //{"x", "b", "x", "c", "d", "x"} + + fmt.Println(slice.Replace(strs, "e", "x")) //{"a", "b", "a", "c", "d", "a"} +} +``` + ### Shuffle

Creates an slice of shuffled values.

diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 68c9737..be544a3 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -53,6 +53,8 @@ import ( - [Map](#Map) - [Reverse](#Reverse) - [Reduce](#Reduce) +- [Replace](#Replace) +- [ReplaceAll](#ReplaceAll) - [Shuffle](#Shuffle) - [SortByField](#SortByField) - [Some](#Some) @@ -966,6 +968,61 @@ func main() { +### Replace +

返回切片的副本,其中前n个不重叠的old替换为new

+ +函数签名: + +```go +func Replace[T comparable](slice []T, old T, new T, n int) []T +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + strs := []string{"a", "b", "a", "c", "d", "a"} + + fmt.Println(slice.Replace(strs, "a", "x", 0)) //{"a", "b", "a", "c", "d", "a"} + + fmt.Println(slice.Replace(strs, "a", "x", 1)) //{"x", "b", "a", "c", "d", "a"} + + fmt.Println(slice.Replace(strs, "a", "x", -1)) //{"x", "b", "x", "c", "d", "x"} +} +``` + + + +### ReplaceAll +

返回切片的副本,将其中old全部替换为new

+ +函数签名: + +```go +func ReplaceAll[T comparable](slice []T, old T, new T) []T +``` +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + strs := []string{"a", "b", "a", "c", "d", "a"} + + fmt.Println(slice.ReplaceAll(strs, "a", "x")) //{"x", "b", "x", "c", "d", "x"} + + fmt.Println(slice.Replace(strs, "e", "x")) //{"a", "b", "a", "c", "d", "a"} +} +``` + + ### Shuffle

随机打乱切片中的元素顺序