mathseq

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package mathseq operates on numerical sequences.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Max

func Max[Item cmp.Ordered](items iter.Seq[Item]) (result Item, ok bool)

Max returns the maximum of items in the sequence, if any. When the sequence is empty, ok is false, it's true otherwise. For floating-point Item, Max propagates NaNs (any NaN value i x forces the output to be NaN). This is the same behaviour as slices.Max.

Does not return for infinite sequences.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/GodsBoss/g/seq/mathseq"
)

func main() {
	values := []int32{11, -17, 22, 5, -3, 18, 53, 35}

	maximum, ok := mathseq.Max(slices.Values(values))

	if ok {
		fmt.Printf("Maximum is %d.\n", maximum)
	}

}
Output:

Maximum is 53.

func MaxFunc

func MaxFunc[Item any](cmp func(current, next Item) int) func(iter.Seq[Item]) (result Item, ok bool)

MaxFunc returns the maximal value from a sequence, using cmp to compare elements. When the sequence is empty, ok is false, it's true otherwise. If there is more than one maximal element according to the cmp function, MaxFunc returns the first one. This is the same behaviour as slices.MaxFunc.

Does not return for infinite sequences.

func Min

func Min[Item cmp.Ordered](items iter.Seq[Item]) (result Item, ok bool)

Min returns the minimum of items in the sequence, if any. When the sequence is empty, ok is false, it's true otherwise. For floating-point Item, Min propagates NaNs (any NaN value i x forces the output to be NaN). This is the same behaviour as slices.Min.

Does not return for infinite sequences.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/GodsBoss/g/seq/mathseq"
)

func main() {
	values := []int32{11, -17, 22, 5, -3, 18, 53, 35}

	minimum, ok := mathseq.Min(slices.Values(values))

	if ok {
		fmt.Printf("Minimum is %d.\n", minimum)
	}

}
Output:

Minimum is -17.

func MinFunc

func MinFunc[Item any](cmp func(current, next Item) int) func(iter.Seq[Item]) (result Item, ok bool)

MinFunc returns the minimal value from a sequence, using cmp to compare elements. When the sequence is empty, ok is false, it's true otherwise. If there is more than one minimal element according to the cmp function, MinFunc returns the first one. This is the same behaviour as slices.MinFunc.

Does not return for infinite sequences.

func Product

func Product[Number Numeric](sequence iter.Seq[Number]) Number

Product returns the product of all the numbers from the sequence. If the sequence is empty, 1 is returned.

Does not return for infinite sequences.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/GodsBoss/g/seq/mathseq"
)

func main() {
	numbers := []int{2, 3, 5, 7}
	product := mathseq.Product(slices.Values(numbers))
	fmt.Println(product)

}
Output:

210

func Sum

func Sum[Number Numeric](sequence iter.Seq[Number]) Number

Sum returns the sum of all the numbers from the sequence. If the sequence is empty, 0 is returned.

Does not return for infinite sequences.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/GodsBoss/g/seq/mathseq"
)

func main() {
	numbers := []int{2, 3, 5, 7}
	sum := mathseq.Sum(slices.Values(numbers))
	fmt.Println(sum)

}
Output:

17

Types

type Numeric

type Numeric interface {
	~float32 | ~float64 | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

Numeric is a constraint that contains all numeric types.

Jump to

Keyboard shortcuts

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