summary

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 4 Imported by: 0

README

Go Reference Go Version

Summary

summary is a lightweight Go library for computing summary statistics from numeric collections and streams.

It provides an API inspired by Java's IntSummaryStatistics and DoubleSummaryStatistics, while following idiomatic Go design.

Features

  • Generic support for integer and floating-point slices
  • Read numbers directly from any io.Reader
  • Computes:
    • Count
    • Sum
    • Average
    • Minimum
    • Maximum
  • Invalid lines are ignored when reading streams
  • Small, dependency-free package

Installation

go get github.com/marcelo-sjr/summary_statistics

Usage

Example
stats := summary.Int(10, 20, 30, 40)

fmt.Println(stats.Count)
fmt.Println(stats.Sum)
fmt.Println(stats.Avg)
fmt.Println(stats.Min)
fmt.Println(stats.Max)

Reading from streams

Both StreamInt and StreamFloat accept any io.Reader and a bufio.SplitFunc, allowing you to control how the input is tokenized.

One value per line
file, err := os.Open("numbers.txt")
if err != nil {
    log.Fatal(err)
}
defer file.Close()

stats, err := summary.StreamInt(file, bufio.ScanLines)

Input:

10
20
30
40

Space-separated values
file, err := os.Open("numbers.txt")
if err != nil {
    log.Fatal(err)
}
defer file.Close()

stats, err := summary.StreamInt(file, bufio.ScanWords)

Input:

10 20 30 40

Mixed input

Using bufio.ScanWords, invalid tokens are ignored.

Input:

10
20
hello
30
40 foo
50

Only the numeric tokens are included in the statistics.

API

Slice functions
summary.Ints[T integer](t ...T) IntSummary
summary.Floats[T float](t ...T) FloatSummary
Stream functions
summary.StreamInts(r io.Reader, split bufio.SplitFunc) (IntSummary, error)
summary.StreamFloats(r io.Reader, bitSize int, split bufio.SplitFunc) (FloatSummary, error)

License

MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FloatSummary

type FloatSummary struct {
	Count int
	Sum   float64
	Avg   float64
	Min   float64
	Max   float64
}

func Floats

func Floats[T float](t ...T) FloatSummary

Float computes summary statistics for a collection of floating-point values. If the input is float32, the returned statistics are promoted to float64.

func StreamFloats

func StreamFloats(r io.Reader, bitSize int, split bufio.SplitFunc) (FloatSummary, error)

StreamFloat reads floating-point numbers from r and computes summary statistics.

Input is tokenized using the provided bufio.SplitFunc. Each token is parsed using strconv.ParseFloat with the provided bitSize. Tokens that cannot be parsed are ignored.

StreamFloat does not close r; the caller is responsible for closing it, if necessary.

If the scanner encounters an I/O error, an empty FloatSummary and the corresponding error are returned.

type IntSummary

type IntSummary struct {
	Count int
	Sum   int
	Avg   float64
	Min   int
	Max   int
}

func Ints

func Ints[T integer](t ...T) IntSummary

Ints computes summary statistics for a collection of integer values. the average is returned as a float64.

func StreamInts

func StreamInts(r io.Reader, split bufio.SplitFunc) (IntSummary, error)

StreamInt reads integers from r and computes summary statistics.

Input is tokenized using the provided bufio.SplitFunc. Each token is parsed as an integer using strconv.Atoi. Tokens that cannot be parsed are ignored.

StreamInt does not close r; the caller is responsible for closing it, if necessary.

If the scanner encounters an I/O error, an empty IntSummary and the corresponding error are returned.

Jump to

Keyboard shortcuts

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