summary

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 3 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
  • Configurable tokenization using bufio.SplitFunc
  • bufio.ScanWords is used by default when no split function is provided
  • Computes:
    • Count
    • Sum
    • Average
    • Minimum
    • Maximum
  • Invalid tokens are ignored when reading streams
  • Dependency-free

Installation

go get github.com/marcelo-sjr/summary_statistics

Usage

Slice
stats := summary.Ints(10, 20, 30, 40)

fmt.Println(stats.Count)
fmt.Println(stats.Sum)
fmt.Println(stats.Avg)
fmt.Println(stats.Min)
fmt.Println(stats.Max)
Floating-point slice
stats := summary.Floats(1.5, 2.5, 3.5)

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

Reading from streams

ReadInts and ReadFloats accept any io.Reader.

By default, input is tokenized using bufio.ScanWords. You may provide a custom bufio.SplitFunc when different tokenization is required.

Default behavior
file, err := os.Open("numbers.txt")
if err != nil {
	log.Fatal(err)
}
defer file.Close()

stats, err := summary.ReadInts(file, nil)
if err != nil {
	log.Fatal(err)
}

Input:

10 20 30 40

One value per line
stats, err := summary.ReadInts(file, bufio.ScanLines)

Input:

10
20
30
40

Space-separated values
stats, err := summary.ReadInts(file, bufio.ScanWords)

Input:

10 20 30 40

Mixed input

Invalid tokens are ignored.

10
20
hello
30
40 foo
50

Only numeric tokens are included in the statistics.

API

Slice functions
summary.Ints[T integer](values ...T) IntSummary
summary.Floats[T float](values ...T) FloatSummary
Reader functions
summary.ReadInts(r io.Reader, split bufio.SplitFunc) (IntSummary, error)
summary.ReadFloats(r io.Reader, 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

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

func ReadFloats added in v1.0.2

func ReadFloats(r io.Reader, split bufio.SplitFunc) (FloatSummary, error)

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

Input can be tokenized using the provided bufio.SplitFunc. If split is nil, bufio.ScanWords is used. Each token is parsed using strconv.ParseFloat with the bitSize=64. Tokens that cannot be parsed are ignored.

ReadFloats 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   int64
	Avg   float64
	Min   int64
	Max   int64
}

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 ReadInts added in v1.0.2

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

ReadInts reads integers from r and computes summary statistics.

Input can be tokenized using the provided bufio.SplitFunc. If split is nil, bufio.ScanWords is used. Each token is parsed as an integer using strconv.Atoi. Tokens that cannot be parsed are ignored.

ReadInts 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