sliceutil

package
v1.79.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2023 License: MIT Imports: 0 Imported by: 0

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

func Filter[S ~[]E, E any](s S, f func(int, E) bool) S

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

func Map[S ~[]E, E any, U any](s S, f func(int, E) U) []U

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

func Reduce[S ~[]E, E any, U any](s S, init U, f func(int, E, U) U) U

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL