Skip to content

Commit

Permalink
tpl/math: Allow variadic math functions to take slice args, add math.…
Browse files Browse the repository at this point in the history
…Product, math.Sum

* Update math.Min and math.Max to allow 1 or more args, either scalar or slice, or combination of the two
* Add math.Sum (allow 1 or more args, either scalar or slice, or combination of the two)
* Add math.Product (allow 1 or more args, either scalar or slice, or combination of the two)

Fixes gohugoio#11030
  • Loading branch information
bep committed Jun 13, 2023
1 parent 258884f commit ea7eb51
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 38 deletions.
105 changes: 69 additions & 36 deletions tpl/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package math

import (
"errors"
"fmt"
"math"
"reflect"
"sync/atomic"

_math "github.com/gohugoio/hugo/common/math"
Expand Down Expand Up @@ -85,48 +87,30 @@ func (ns *Namespace) Log(n any) (float64, error) {
return math.Log(af), nil
}

// Max returns the greater of the multivalued numbers n1 and n2 or more values.
// Max returns the greater of all numbers in inputs. Any slices in inputs are flattened.
func (ns *Namespace) Max(inputs ...any) (maximum float64, err error) {
if len(inputs) < 2 {
err = errMustTwoNumbersError
return
}
var value float64
for index, input := range inputs {
value, err = cast.ToFloat64E(input)
if err != nil {
err = errors.New("Max operator can't be used with non-float value")
return
}
if index == 0 {
maximum = value
continue
}
maximum = math.Max(value, maximum)
}
return
return ns.applyOpToScalarsOrSlices("Max", math.Max, inputs...)
}

// Min returns the smaller of multivalued numbers n1 and n2 or more values.
// Min returns the smaller of all numbers in inputs. Any slices in inputs are flattened.
func (ns *Namespace) Min(inputs ...any) (minimum float64, err error) {
if len(inputs) < 2 {
err = errMustTwoNumbersError
return
return ns.applyOpToScalarsOrSlices("Min", math.Min, inputs...)
}

// Sum returns the sum of all numbers in inputs. Any slices in inputs are flattened.
func (ns *Namespace) Sum(inputs ...any) (sum float64, err error) {
fn := func(x, y float64) float64 {
return x + y
}
var value float64
for index, input := range inputs {
value, err = cast.ToFloat64E(input)
if err != nil {
err = errors.New("Max operator can't be used with non-float value")
return
}
if index == 0 {
minimum = value
continue
}
minimum = math.Min(value, minimum)
return ns.applyOpToScalarsOrSlices("Sum", fn, inputs...)
}

// Product returns the product of all numbers in inputs. Any slices in inputs are flattened.
func (ns *Namespace) Product(inputs ...any) (product float64, err error) {
fn := func(x, y float64) float64 {
return x * y
}
return
return ns.applyOpToScalarsOrSlices("Product", fn, inputs...)
}

// Mod returns n1 % n2.
Expand Down Expand Up @@ -197,6 +181,55 @@ func (ns *Namespace) Sub(inputs ...any) (any, error) {
return ns.doArithmetic(inputs, '-')
}

func (ns *Namespace) applyOpToScalarsOrSlices(opName string, op func(x, y float64) float64, inputs ...any) (result float64, err error) {
var i int
for _, input := range inputs {
var values []float64
values, err = ns.toFloatsE(input)
if err != nil {
err = fmt.Errorf("%s operator can't be used with non-float values", opName)
return
}
for _, value := range values {
i++
if i == 1 {
result = value
continue
}
result = op(result, value)
}
}

if i < 2 {
err = errMustTwoNumbersError
return
}
return

}

func (ns *Namespace) toFloatsE(v any) ([]float64, error) {
vv := reflect.ValueOf(v)
switch vv.Kind() {
case reflect.Slice, reflect.Array:
var floats []float64
for i := 0; i < vv.Len(); i++ {
f, err := cast.ToFloat64E(vv.Index(i).Interface())
if err != nil {
return nil, err
}
floats = append(floats, f)
}
return floats, nil
default:
f, err := cast.ToFloat64E(v)
if err != nil {
return nil, err
}
return []float64{f}, nil
}
}

func (ns *Namespace) doArithmetic(inputs []any, operation rune) (value any, err error) {
if len(inputs) < 2 {
return nil, errMustTwoNumbersError
Expand Down
54 changes: 52 additions & 2 deletions tpl/math/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ func TestMax(t *testing.T) {
{[]any{0, "a"}, false},
{[]any{"a", 0}, false},
{[]any{"a", "b"}, false},
// Issue #11030
{[]any{7, []any{3, 4}}, 7.0},
{[]any{8, []any{3, 12}, 3}, 12.0},
{[]any{[]any{3, 5, 2}}, 5.0},
{[]any{3, []int{3, 6}, 3}, 6.0},
// miss values
{[]any{}, false},
{[]any{0}, false},
Expand All @@ -437,7 +442,7 @@ func TestMax(t *testing.T) {
}

c.Assert(err, qt.IsNil)
c.Assert(result, qt.Equals, test.expect)
c.Assert(result, qt.Equals, test.expect, qt.Commentf("values: %v", test.values))
}
}

Expand Down Expand Up @@ -468,6 +473,11 @@ func TestMin(t *testing.T) {
{[]any{0, "a"}, false},
{[]any{"a", 0}, false},
{[]any{"a", "b"}, false},
// Issue #11030
{[]any{1, []any{3, 4}}, 1.0},
{[]any{8, []any{3, 2}, 3}, 2.0},
{[]any{[]any{3, 2, 2}}, 2.0},
{[]any{8, []int{3, 2}, 3}, 2.0},
// miss values
{[]any{}, false},
{[]any{0}, false},
Expand All @@ -484,7 +494,47 @@ func TestMin(t *testing.T) {
continue
}

c.Assert(err, qt.IsNil)
c.Assert(err, qt.IsNil, qt.Commentf("values: %v", test.values))
c.Assert(result, qt.Equals, test.expect)
}
}

func TestSum(t *testing.T) {
t.Parallel()
c := qt.New(t)

ns := New()

mustSum := func(values ...any) any {
result, err := ns.Sum(values...)
c.Assert(err, qt.IsNil)
return result
}

c.Assert(mustSum(1, 2, 3), qt.Equals, 6.0)
c.Assert(mustSum(1, 2, 3.0), qt.Equals, 6.0)
c.Assert(mustSum(1, 2, []any{3, 4}), qt.Equals, 10.0)

_, err := ns.Sum(1)
c.Assert(err, qt.Not(qt.IsNil))
}

func TestProduct(t *testing.T) {
t.Parallel()
c := qt.New(t)

ns := New()

mustProduct := func(values ...any) any {
result, err := ns.Product(values...)
c.Assert(err, qt.IsNil)
return result
}

c.Assert(mustProduct(2, 2, 3), qt.Equals, 12.0)
c.Assert(mustProduct(1, 2, 3.0), qt.Equals, 6.0)
c.Assert(mustProduct(1, 2, []any{3, 4}), qt.Equals, 24.0)

_, err := ns.Product(1)
c.Assert(err, qt.Not(qt.IsNil))
}

0 comments on commit ea7eb51

Please sign in to comment.