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!
Core - Math helpersโ
This page lists all math operations available in the core package of lo.
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) []int
RangeFromโ
Creates a slice starting at
start
with 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) []T
RangeWithStepsโ
Creates a slice progressing from
start
up to, but not including,end
bystep
. Returns empty ifstep
is 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) []T
Clampโ
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) T
Sums 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) T
SumByโ
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)
})
// 6Prototype:func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R
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) T
ProductByโ
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) R
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) T
MeanByโ
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) R
Modeโ
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