Slice: Add SetToDefaultIf (#187)

SetToDefaultIf sets elements to their zero value if they match the given predicate.
It retains the positions of the elements in the slice.
It returns slice of T and the count of modified slice items
This commit is contained in:
donutloop authored and GitHub committed 2024-02-28 11:27:23 +08:00
1 parent 6c7f38d8b3
commit 0e1593c67b
3 files changed
+164

No files matched your search

+10
View File
@@ -1102,3 +1102,13 @@ func ExampleRandom() {
// Output:
// okk
}
func ExampleSetToDefaultIf() {
strs := []string{"a", "b", "a", "c", "d", "a"}
modifiedStrs, count := SetToDefaultIf(strs, func(s string) bool { return "a" == s })
fmt.Println(modifiedStrs)
fmt.Println(count)
// Output:
// [ b c d ]
// 3
}