Math - Core helpers
This page lists all math operations available in the core package of lo.
This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Rangeโ
Creates a slice of integers of the given length starting at 0. Negative length produces a descending sequence.
lo.Range(4)// []int{0, 1, 2, 3}lo.Range(-4)// []int{0, -1, -2, -3}Prototype:func Range(elementNum int) []intRangeFromโ
Creates a slice starting at
startwith the specified length. Negative length yields a descending sequence.lo.RangeFrom(1, 5)// []int{1, 2, 3, 4, 5}Prototype:func RangeFrom[T constraints.Integer | constraints.Float](start T, elementNum int) []TRangeWithStepsโ
Creates a slice progressing from
startup to, but not including,endbystep. Returns empty ifstepis 0 or direction mismatches.lo.RangeWithSteps(0, 20, 5)// []int{0, 5, 10, 15}Prototype:func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step T) []TClampโ
Clamps a number within inclusive lower and upper bounds.
lo.Clamp(42, -10, 10)// 10Prototype:func Clamp[T constraints.Ordered](value T, mIn T, mAx T) TSums the values in a collection. Returns 0 for an empty collection.
lo.Sum([]int{1, 2, 3, 4, 5})// 15Prototype:func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) TSumByโ
Sums the values computed by a predicate across a collection. Returns 0 for an empty collection.
strings := []string{"foo", "bar"}lo.SumBy(strings, func(item string) int {return len(item)})// 6// See also: SumByErr for error handlingstrings := []string{"foo", "bar", "baz"}sum, err := lo.SumByErr(strings, func(item string) (int, error) {if item == "bar" {return 0, fmt.Errorf("invalid item: %s", item)}return len(item), nil})// sum: 3, err: invalid item: barPrototype:func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) RSumByErrโ
Sums the values computed by a predicate across a collection, stopping early and returning an error if the predicate returns one. Returns 0 for an empty collection.
strings := []string{"foo", "bar", "baz"}sum, err := lo.SumByErr(strings, func(item string) (int, error) {if item == "bar" {return 0, fmt.Errorf("invalid item: %s", item)}return len(item), nil})// sum: 3, err: invalid item: barstrings := []string{"foo", "bar"}sum, err := lo.SumByErr(strings, func(item string) (int, error) {return len(item), nil})// sum: 6, err: nilstrings := []string{}sum, err := lo.SumByErr(strings, func(item string) (int, error) {return len(item), nil})// sum: 0, err: nilPrototype:func SumByErr[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) (R, error)) (R, error)Productโ
Calculates the product of the values in a collection. Returns 1 for nil or empty collections.
lo.Product([]int{1, 2, 3, 4, 5})// 120Prototype:func Product[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) TProductByโ
Calculates the product of values computed by a predicate. Returns 1 for nil or empty collections.
strings := []string{"foo", "bar"}lo.ProductBy(strings, func(item string) int {return len(item)})// 9Prototype:func ProductBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) RProductByErrโ
Calculates the product of values computed by a predicate. Returns 1 for nil or empty collections.
If the iteratee returns an error, iteration stops and the error is returned.
strings := []string{"foo", "bar"}result, err := lo.ProductByErr(strings, func(item string) (int, error) {return len(item), nil})// 9, <nil>Example with error:
strings := []string{"foo", "bar", "baz"}result, err := lo.ProductByErr(strings, func(item string) (int, error) {if item == "bar" {return 0, fmt.Errorf("bar is not allowed")}return len(item), nil})// 3, error("bar is not allowed")Prototype:func ProductByErr[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) (R, error)) (R, error)Calculates the arithmetic mean of a collection of numbers. Returns 0 for an empty collection.
lo.Mean([]int{2, 3, 4, 5})// 3Prototype:func Mean[T constraints.Float | constraints.Integer](collection []T) TMeanByโ
Calculates the mean of values computed by a predicate. Returns 0 for an empty collection.
list := []string{"aa", "bbb", "cccc", "ddddd"}lo.MeanBy(list, func(item string) float64 {return float64(len(item))})// 3.5Prototype:func MeanBy[T any, R constraints.Float | constraints.Integer](collection []T, iteratee func(item T) R) RMeanByErrโ
Calculates the mean of values computed by a predicate. Returns 0 for an empty collection.
If the iteratee returns an error, iteration stops and the error is returned.
list := []string{"aa", "bbb", "cccc", "ddddd"}result, err := lo.MeanByErr(list, func(item string) (float64, error) {return float64(len(item)), nil})// 3.5, <nil>Example with error:
list := []string{"aa", "bbb", "cccc", "ddddd"}result, err := lo.MeanByErr(list, func(item string) (float64, error) {if item == "cccc" {return 0, fmt.Errorf("cccc is not allowed")}return float64(len(item)), nil})// 0, error("cccc is not allowed")Prototype:func MeanByErr[T any, R constraints.Float | constraints.Integer](collection []T, iteratee func(item T) (R, error)) (R, error)Returns the mode(s), i.e., the most frequent value(s) in a collection. If multiple values share the highest frequency, returns all. Empty input yields an empty slice.
lo.Mode([]int{2, 2, 3, 3})// []int{2, 3}Prototype:func Mode[T constraints.Integer | constraints.Float](collection []T) []T