From fcfbdea59792460531890c0e3f8d1819871ac1ad Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sat, 15 Oct 2022 14:49:49 +0800 Subject: [PATCH] feat: add Replace and ReplaceAll for slice --- slice/slice.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/slice/slice.go b/slice/slice.go index 3ec1b52..3d19a6e 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -453,6 +453,26 @@ func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initia return result } +// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new +func Replace[T comparable](slice []T, old T, new T, n int) []T { + result := make([]T, len(slice)) + copy(result, slice) + + for i := range result { + if result[i] == old && n != 0 { + result[i] = new + n-- + } + } + + return result +} + +// ReplaceAll returns a copy of the slice with all non-overlapping instances of old replaced by new. +func ReplaceAll[T comparable](slice []T, old T, new T) []T { + return Replace(slice, old, new, -1) +} + // InterfaceSlice convert param to slice of interface. func InterfaceSlice(slice any) []any { sv := sliceValue(slice)