From 6d0b6d7c11648ebee1ebaaa6f8353c1aa8fe8269 Mon Sep 17 00:00:00 2001 From: mazkev Date: Tue, 27 Oct 2026 10:30:55 +0700 Subject: [PATCH] fix(mutable): move Fill to slice.go to export it properly Fill was accidentally defined in slice_example_test.go instead of slice.go, causing it to not be exported as part of the public mutable package. This moves func Fill into slice.go, adds ExampleFill() in slice_example_test.go, and updates the source reference and example in docs/data/mutable-fill.md. --- docs/data/mutable-fill.md | 76 ++++++++++++++++++----------------- mutable/slice.go | 8 ++++ mutable/slice_example_test.go | 13 +++--- 3 files changed, 54 insertions(+), 43 deletions(-) diff --git a/docs/data/mutable-fill.md b/docs/data/mutable-fill.md index eb4536919..7992d2ee5 100644 --- a/docs/data/mutable-fill.md +++ b/docs/data/mutable-fill.md @@ -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} -``` \ No newline at end of file +--- +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} +``` \ No newline at end of file diff --git a/mutable/slice.go b/mutable/slice.go index 4031e3825..00345d8c5 100644 --- a/mutable/slice.go +++ b/mutable/slice.go @@ -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 + } +} diff --git a/mutable/slice_example_test.go b/mutable/slice_example_test.go index b73726377..defd3504a 100644 --- a/mutable/slice_example_test.go +++ b/mutable/slice_example_test.go @@ -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] }