Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tpl/math: Allow variadic math functions to take slice args, add math.Product, math.Sum #11091

Merged
merged 2 commits into from
Jun 13, 2023

Conversation

bep
Copy link
Member

@bep bep commented Jun 13, 2023

  • 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 #11030

@bep bep requested a review from jmooring June 13, 2023 07:54
Copy link
Member

@jmooring jmooring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These throw the "must provide at least two numbers" error:

{{ math.Sum 1 }}
{{ math.Sum (slice 1) }}
{{ math.Product 1 }}
{{ math.Product (slice 1) }}
{{ math.Min 1 }}
{{ math.Min (slice 1) }}
{{ math.Max 1 }}
{{ math.Max (slice 1) }}

…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
@bep
Copy link
Member Author

bep commented Jun 13, 2023

@jmooring I have pushed an update that removes that validation, meaning that e.g.

{{ math.Sum (slice) => 0 }}

Which I guess makes sense.

@jmooring
Copy link
Member

1) These work as desired (1 arg... an empty slice):

{{ math.Sum slice }}     --> 0
{{ math.Product slice }} --> 0
{{ math.Min slice }}     --> 0
{{ math.Max slice }}     --> 0

2) But I think these should throw an error (no args):

{{ math.Sum }}      --> 0
{{ math.Product }}  --> 0
{{ math.Min }}      --> 0
{{ math.Max }}      --> 0

The use case for the first example above is:

{{ $awards := slice }}
{{ range where site.RegularPages "Type" "films" }}
  {{ $awards = $awards | append .Params.awards }}
{{ end }}

{{ math.Sum $awards }} 
{{ math.Min $awards }} 
{{ math.Max $awards }} 

@bep
Copy link
Member Author

bep commented Jun 13, 2023

How does my last attempt ... work?

@jmooring
Copy link
Member

Nice. Thank you.

@bep bep merged commit 2ba2271 into gohugoio:master Jun 13, 2023
8 checks passed
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tpl/math: Allow variadic math functions to take slice args
2 participants