1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 15:12:26 +08:00

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
2024-02-28 04:27:23 +01:00
committed by GitHub
parent 6c7f38d8b3
commit 0e1593c67b
3 changed files with 164 additions and 0 deletions

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
}