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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user