Documentation
¶
Overview ¶
Package mathseq operates on numerical sequences.
Index ¶
- func Max[Item cmp.Ordered](items iter.Seq[Item]) (result Item, ok bool)
- func MaxFunc[Item any](cmp func(current, next Item) int) func(iter.Seq[Item]) (result Item, ok bool)
- func Min[Item cmp.Ordered](items iter.Seq[Item]) (result Item, ok bool)
- func MinFunc[Item any](cmp func(current, next Item) int) func(iter.Seq[Item]) (result Item, ok bool)
- func Product[Number Numeric](sequence iter.Seq[Number]) Number
- func Sum[Number Numeric](sequence iter.Seq[Number]) Number
- type Numeric
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Max ¶
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 ¶
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 ¶
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 ¶
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