diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index 1e173958..6ac27981 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -95,6 +95,8 @@ import ( - [Partition](#Partition) - [SetToDefaultIf](#SetToDefaultIf) - [Break](#Break) +- [RightPadding](#RightPadding) +- [LeftPadding](#LeftPadding)
@@ -2607,13 +2609,13 @@ func main() {

根据判断函数将切片分成两部分。它开始附加到与函数匹配的第一个元素之后的第二个切片。第一个匹配之后的所有元素都包含在第二个切片中,无论它们是否与函数匹配。

-示例: +函数签名: ```go func Break[T any](values []T, predicate func(T) bool) ([]T, []T) ``` -Example: +示例: ```go import ( @@ -2634,4 +2636,58 @@ func main() { // [1] // [2 3 4 5] } +``` + +RightPadding + +

TBD

+ +函数签名: + +```go +func RightPadding[T any](slice []T, paddingValue T, paddingLength int) []T +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + padded := slice.RightPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [1 2 3 4 5 0 0 0] +} +``` + +LeftPadding + +

TBD

+ +函数签名: + +```go +func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + padded := slice.LeftPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [0 0 0 1 2 3 4 5] +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index cf11d122..381152a1 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -95,6 +95,8 @@ import ( - [Partition](#Partition) - [SetToDefaultIf](#SetToDefaultIf) - [Break](#Break) +- [RightPadding](#RightPadding) +- [LeftPadding](#LeftPadding)
@@ -2631,4 +2633,58 @@ func main() { // [1] // [2 3 4 5] } +``` + +RightPadding + +

RightPadding adds padding to the right end of a slice.

+ +Signature: + +```go +func RightPadding[T any](slice []T, paddingValue T, paddingLength int) []T +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + padded := RightPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [1 2 3 4 5 0 0 0] +} +``` + +LeftPadding + +

LeftPadding adds padding to the left begin of a slice.

+ +Signature: + +```go +func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 4, 5} + padded := LeftPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [0 0 0 1 2 3 4 5] +} ``` \ No newline at end of file diff --git a/slice/slice.go b/slice/slice.go index bf0b2b5c..d832a369 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -1273,3 +1273,35 @@ func Random[T any](slice []T) (val T, idx int) { idx = random.RandInt(0, len(slice)) return slice[idx], idx } + +// RightPadding adds padding to the right end of a slice. +// Play: Todo +func RightPadding[T any](slice []T, paddingValue T, paddingLength int) []T { + if paddingLength == 0 { + return slice + } + for i := 0; i < paddingLength; i++ { + slice = append(slice, paddingValue) + } + return slice +} + +// LeftPadding adds padding to the left begin of a slice. +// Play: Todo +func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T { + if paddingLength == 0 { + return slice + } + + paddedSlice := make([]T, len(slice)+paddingLength) + i := 0 + for ; i < paddingLength; i++ { + paddedSlice[i] = paddingValue + } + for j := 0; j < len(slice); j++ { + paddedSlice[i] = slice[j] + i++ + } + + return paddedSlice +} diff --git a/slice/slice_example_test.go b/slice/slice_example_test.go index 39361a2b..f949e75f 100644 --- a/slice/slice_example_test.go +++ b/slice/slice_example_test.go @@ -1125,3 +1125,19 @@ func ExampleBreak() { // [1] // [2 3 4 5] } + +func ExampleRightPadding() { + nums := []int{1, 2, 3, 4, 5} + padded := RightPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [1 2 3 4 5 0 0 0] +} + +func ExampleLeftPadding() { + nums := []int{1, 2, 3, 4, 5} + padded := LeftPadding(nums, 0, 3) + fmt.Println(padded) + // Output: + // [0 0 0 1 2 3 4 5] +} diff --git a/slice/slice_test.go b/slice/slice_test.go index aa413f3e..5bb43e27 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -1396,3 +1396,15 @@ func TestBreak(t *testing.T) { assert.Equal([]int{1, 3, 5, 7, 9}, resultAllOdd) assert.Equal([]int{}, emptyResult) } + +func TestRightPaddingAndLeftPadding(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "RightPaddingAndLeftPadding") + + // Test with integers + nums := []int{1, 2, 3, 4, 5} + + padded := LeftPadding(RightPadding(nums, 0, 3), 0, 3) + assert.Equal([]int{0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0}, padded) +}