Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 39 additions & 37 deletions docs/data/mutable-fill.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
---
name: Fill
slug: fill
sourceRef: mutable/slice_example_test.go#L72
category: mutable
subCategory: slice
signatures:
- "func Fill[T any, Slice ~[]T](collection Slice, initial T)"
playUrl: https://go.dev/play/p/VwR34GzqEub
variantHelpers:
- mutable#slice#fill
similarHelpers:
- core#slice#fill
- core#slice#repeat
- core#slice#times
position: 60
---

Fills all elements of a slice with the specified initial value. The operation modifies the slice in place.

```go
slice := make([]int, 5)
lo.Fill(slice, 42)
// []int{42, 42, 42, 42, 42}

slice = make([]string, 3)
lo.Fill(slice, "default")
// []string{"default", "default", "default"}

slice = make([]bool, 4)
lo.Fill(slice, true)
// []bool{true, true, true, true}

slice = []int{1, 2, 3, 4, 5}
lo.Fill(slice, 0)
// []int{0, 0, 0, 0, 0}
```
---
name: Fill
slug: fill
sourceRef: mutable/slice.go#L78
category: mutable
subCategory: slice
signatures:
- "func Fill[T any, Slice ~[]T](collection Slice, initial T)"
playUrl: https://go.dev/play/p/VwR34GzqEub
variantHelpers:
- mutable#slice#fill
similarHelpers:
- core#slice#fill
- core#slice#repeat
- core#slice#times
position: 60
---

Fills all elements of a slice with the specified initial value. The operation modifies the slice in place.

```go
import lom "github.com/samber/lo/mutable"

slice := make([]int, 5)
lom.Fill(slice, 42)
// []int{42, 42, 42, 42, 42}

slice = make([]string, 3)
lom.Fill(slice, "default")
// []string{"default", "default", "default"}

slice = make([]bool, 4)
lom.Fill(slice, true)
// []bool{true, true, true, true}

slice = []int{1, 2, 3, 4, 5}
lom.Fill(slice, 0)
// []int{0, 0, 0, 0, 0}
```
8 changes: 8 additions & 0 deletions mutable/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ func Reverse[T any, Slice ~[]T](collection Slice) {
collection[i], collection[j] = collection[j], collection[i]
}
}

// Fill fills elements of a slice with `initial` value.
// Play: https://go.dev/play/p/VwR34GzqEub
func Fill[T any, Slice ~[]T](collection Slice, initial T) {
for i := range collection {
collection[i] = initial
}
}
13 changes: 7 additions & 6 deletions mutable/slice_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ func ExampleReverse() {
// Output: [5 4 3 2 1 0]
}

// Fill fills elements of a slice with `initial` value.
// Play: https://go.dev/play/p/VwR34GzqEub
func Fill[T any, Slice ~[]T](collection Slice, initial T) {
for i := range collection {
collection[i] = initial
}
func ExampleFill() {
list := []int{0, 1, 2, 3, 4}

Fill(list, 42)

fmt.Printf("%v", list)
// Output: [42 42 42 42 42]
}