Documentation
¶
Overview ¶
Package sliceutil provides a collection of slice functions.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filter ¶
Filter returns a new slice containing only the elements in the input slice s for which the specified function f is true.
Example ¶
package main
import (
"fmt"
"github.com/Vonage/gosrvlib/pkg/sliceutil"
)
func main() {
s := []string{"Hello", "World", "Extra"}
filterFn := func(_ int, v string) bool { return v == "World" }
s2 := sliceutil.Filter(s, filterFn)
fmt.Println(s2)
}
Output: [World]
func Map ¶
Map returns a new slice that contains each of the elements of the input slice s mutated by the specified function.
Example ¶
package main
import (
"fmt"
"github.com/Vonage/gosrvlib/pkg/sliceutil"
)
func main() {
s := []string{"Hello", "World", "Extra"}
mapFn := func(k int, v string) int { return k + len(v) }
s2 := sliceutil.Map(s, mapFn)
fmt.Println(s2)
}
Output: [5 6 7]
func Reduce ¶
Reduce applies the reducing function f to each element of the input slice s, and returns the value of the last call to f. The first parameter of the reducing function f is initialized with init.
Example ¶
package main
import (
"fmt"
"github.com/Vonage/gosrvlib/pkg/sliceutil"
)
func main() {
s := []int{2, 3, 5, 7, 11}
init := 97
reduceFn := func(k, v, r int) int { return k + v + r }
r := sliceutil.Reduce(s, init, reduceFn)
fmt.Println(r)
}
Output: 135
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.