statistics

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 24, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package statistics provides reducers for gathering statistics from an iterator.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Max

func Max[T cmp.Ordered](acc *T, in T)

Max is a iterator.Reducer, which collects the maximum value of a stream. acc must be initialized to the minimum value of the type.

Example
package main

import (
	"fmt"
	"math"

	"github.com/KrischanCS/go-toolbox/iterator"
	statistics2 "github.com/KrischanCS/go-toolbox/iterator/reducer/statistics"
)

func main() {
	i := iterator.Of(5, 2, 8, 1, 7)
	acc := math.MinInt

	// Reduce a sequence to find the maximum value
	iterator.Reduce(i, &acc, statistics2.Max[int])

	fmt.Println(acc)

}
Output:

8

func Mean

func Mean[T constraints.RealNumber](acc *MeanAccumulator[T], in T)

Mean is a iterator.Reducer, which collects the mean value of a stream.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
	statistics2 "github.com/KrischanCS/go-toolbox/iterator/reducer/statistics"
)

func main() {
	i := iterator.Of(0, -3, 5, 8, -2, 7)
	acc := statistics2.MeanAccumulator[int]{}

	iterator.Reduce(i, &acc, statistics2.Mean[int])

	fmt.Println(acc.Mean())
}
Output:

2.5

func Min

func Min[T cmp.Ordered](acc *T, in T)

Min is a iterator.Reducer, which collects the minimum value of a stream. acc must be initialized to the maximum value of the type.

Example
package main

import (
	"fmt"
	"math"

	"github.com/KrischanCS/go-toolbox/iterator"
	statistics2 "github.com/KrischanCS/go-toolbox/iterator/reducer/statistics"
)

func main() {
	i := iterator.Of(5, 2, 8, 1, 7)
	acc := math.MaxInt

	// Reduce a sequence to find the maximum value
	iterator.Reduce(i, &acc, statistics2.Min[int])

	fmt.Println(acc)

}
Output:

1

func MinMax

func MinMax[T cmp.Ordered](acc *MinMaxAccumulator[T], in T)

MinMax is a iterator.Reducer, which collects the minimum and maximum value of a stream.

acc min must be initialized to the maximum value of the type, acc max must be initialized to the minimum value of the type.

For real numbers, a correctly initialized MinMaxAccumulator can be created with NewMinMaxAccumulator.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
	statistics2 "github.com/KrischanCS/go-toolbox/iterator/reducer/statistics"
)

func main() {
	i := iterator.Of(-6.28, 2.78, 9.81, 1.41)
	acc := statistics2.NewMinMaxAccumulator[float64]()

	iterator.Reduce(i, &acc, statistics2.MinMax[float64])

	fmt.Printf("Min: %.2f, Max: %.2f\n", acc.Min(), acc.Max())
}
Output:

Min: -6.28, Max: 9.81

Types

type MeanAccumulator

type MeanAccumulator[T constraints.RealNumber] struct {
	// contains filtered or unexported fields
}

MeanAccumulator is the accumulator type for the Mean reducer.

func (MeanAccumulator[T]) Mean

func (m MeanAccumulator[T]) Mean() float64

Mean returns the arithmetic mean of the gathered values.

type MinMaxAccumulator

type MinMaxAccumulator[T any] interface {
	Min() T
	Max() T
	// contains filtered or unexported methods
}

MinMaxAccumulator is the accumulator type for the MinMax reducer.

func NewMinMaxAccumulator

func NewMinMaxAccumulator[T constraints.RealNumber]() MinMaxAccumulator[T]

NewMinMaxAccumulator creates a new MinMaxAccumulator with the correct initial values.

Jump to

Keyboard shortcuts

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