sliceutil

package
v1.101.3 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package sliceutil provides a collection of utility functions for Go slices.

It includes functions for filtering, mapping, and reducing slices, as well as generating descriptive statistics for numerical slices.

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

type DescStats added in v1.80.0

type DescStats[V typeutil.Number] struct {
	// Count is the total number of items in the data set.
	Count int `json:"count"`

	// Entropy computes the Shannon entropy of a distribution.
	Entropy float64 `json:"entropy"`

	// ExKurtosis is the population excess kurtosis of the data set.
	// The kurtosis is defined by the 4th moment of the mean divided by the squared variance.
	// The excess kurtosis subtracts 3.0 so that the excess kurtosis of the normal distribution is zero.
	ExKurtosis float64 `json:"exkurtosis"`

	// Max is the maximum value of the data.
	Max V `json:"max"`

	// MaxID is the index (key) of the Max malue in a data set.
	MaxID int `json:"maxid"`

	// Mean or Average is a central tendency of the data.
	Mean float64 `json:"mean"`

	// MeanDev is the Mean Deviation or Mean Absolute Deviation.
	// It is an average of absolute differences between each value in the data, and the average of all values.
	MeanDev float64 `json:"meandev"`

	// Median is the value that divides the data into 2 equal parts.
	// When the data is sorted, the number of terms on the left and right side of median is the same.
	Median float64 `json:"median"`

	// Min is the minimal value of the data.
	Min V `json:"min"`

	// MinID is the index (key) of the Min malue in a data set.
	MinID int `json:"minid"`

	// Mode is the term appearing maximum time in data set.
	// It is the term that has the highest frequency.
	Mode V `json:"mode"`

	// ModeFreq is the frequency of the Mode value.
	ModeFreq int `json:"modefreq"`

	// Range is the difference between the highest (Max) and lowest (Min) value.
	Range V `json:"range"`

	// Skewness is a measure of the asymmetry of the probability distribution of a real-valued random variable about its mean.
	// Provides the adjusted Fisher-Pearson standardized moment coefficient.
	Skewness float64 `json:"skewness"`

	// StdDev is the Standard deviation of the data.
	// It measures the average distance between each quantity and mean.
	StdDev float64 `json:"stddev"`

	// Sum of all the values in the data.
	Sum V `json:"sum"`

	// Variance is a square of average distance between each quantity and Mean.
	Variance float64 `json:"variance"`
}

DescStats contains descriptive statistics items for a data set.

func Stats added in v1.80.0

func Stats[S ~[]V, V typeutil.Number](s S) (*DescStats[V], error)

Stats returns descriptive statistics parameters to summarize the input data set.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/sliceutil"
)

func main() {
	data := []int{53, 83, 13, 79, 13, 37, 83, 29, 37, 13, 83, 83}

	ds, err := sliceutil.Stats(data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Count:      %d\n", ds.Count)
	fmt.Printf("Entropy:    %.3f\n", ds.Entropy)
	fmt.Printf("ExKurtosis: %.3f\n", ds.ExKurtosis)
	fmt.Printf("Max:        %d\n", ds.Max)
	fmt.Printf("MaxID:      %d\n", ds.MaxID)
	fmt.Printf("Mean:       %.3f\n", ds.Mean)
	fmt.Printf("MeanDev:    %.3f\n", ds.MeanDev)
	fmt.Printf("Median:     %.3f\n", ds.Median)
	fmt.Printf("Min:        %d\n", ds.Min)
	fmt.Printf("MinID:      %d\n", ds.MinID)
	fmt.Printf("Mode:       %d\n", ds.Mode)
	fmt.Printf("ModeFreq:   %d\n", ds.ModeFreq)
	fmt.Printf("Range:      %d\n", ds.Range)
	fmt.Printf("Skewness:   %.3f\n", ds.Skewness)
	fmt.Printf("StdDev:     %.3f\n", ds.StdDev)
	fmt.Printf("Sum:        %d\n", ds.Sum)
	fmt.Printf("Variance:   %.3f\n", ds.Variance)

}
Output:

Count:      12
Entropy:    2.302
ExKurtosis: -1.910
Max:        83
MaxID:      1
Mean:       50.500
MeanDev:    0.000
Median:     45.000
Min:        13
MinID:      2
Mode:       83
ModeFreq:   4
Range:      70
Skewness:   -0.049
StdDev:     30.285
Sum:        606
Variance:   917.182

Jump to

Keyboard shortcuts

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