Skip to content

Commit

Permalink
Slice: padding (#201)
Browse files Browse the repository at this point in the history
* LeftPadding adds padding to the left begin of a slice.
* RightPadding adds padding to the right end of a slice.
  • Loading branch information
donutloop authored Mar 10, 2024
1 parent 73c97af commit ab50e81
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 2 deletions.
60 changes: 58 additions & 2 deletions docs/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ import (
- [Partition](#Partition)
- [SetToDefaultIf](#SetToDefaultIf)
- [Break](#Break)
- [RightPadding](#RightPadding)
- [LeftPadding](#LeftPadding)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -2607,13 +2609,13 @@ func main() {

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

<b>示例:</b>
<b>函数签名:</b>

```go
func Break[T any](values []T, predicate func(T) bool) ([]T, []T)
```

<b>Example:</b>
<b>示例:</b>

```go
import (
Expand All @@ -2634,4 +2636,58 @@ func main() {
// [1]
// [2 3 4 5]
}
```

<span id="RightPadding">RightPadding</span>

<p>TBD</p>

<b>函数签名:</b>

```go
func RightPadding[T any](slice []T, paddingValue T, paddingLength int) []T
```

<b>示例:</b>

```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]
}
```

<span id="LeftPadding">LeftPadding</span>

<p>TBD</p>

<b>函数签名:</b>

```go
func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T
```

<b>示例:</b>

```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]
}
```
56 changes: 56 additions & 0 deletions docs/en/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ import (
- [Partition](#Partition)
- [SetToDefaultIf](#SetToDefaultIf)
- [Break](#Break)
- [RightPadding](#RightPadding)
- [LeftPadding](#LeftPadding)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -2631,4 +2633,58 @@ func main() {
// [1]
// [2 3 4 5]
}
```

<span id="c">RightPadding</span>

<p>RightPadding adds padding to the right end of a slice.</p>

<b>Signature:</b>

```go
func RightPadding[T any](slice []T, paddingValue T, paddingLength int) []T
```

<b>Example:</b>

```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]
}
```

<span id="LeftPadding">LeftPadding</span>

<p>LeftPadding adds padding to the left begin of a slice.</p>

<b>Signature:</b>

```go
func LeftPadding[T any](slice []T, paddingValue T, paddingLength int) []T
```

<b>Example:</b>

```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]
}
```
32 changes: 32 additions & 0 deletions slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions slice/slice_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
12 changes: 12 additions & 0 deletions slice/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit ab50e81

Please sign in to comment.