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

Add std.clamp #642

Closed
sbarzowski opened this issue Apr 30, 2019 · 2 comments · Fixed by #751
Closed

Add std.clamp #642

sbarzowski opened this issue Apr 30, 2019 · 2 comments · Fixed by #751
Labels

Comments

@sbarzowski
Copy link
Collaborator

sbarzowski commented Apr 30, 2019

clamp(x, minVal, maxVal):: 
  if x  < minVal then minVal
  else if x > maxVal then maxVal
  else val

It seems like a quite natural and easy addition. Not a very huge value added, but it's a common little thing (and imo way more readable than std.min(lowerBound, std.max(x, upperBound))).

And when we are at it we can add std.inRange which checks if a value is within a given range.

@seh
Copy link

seh commented May 1, 2019

Would you verify that minVal is less than or equal to maxVal?

@sbarzowski
Copy link
Collaborator Author

Hmm... that's not obvious and it's actually an argument against std.inRange since it may not be obvious for the person reading the code as well.

Anyway, the function std.range is inclusive, so std.inRange should include maxVal too I guess.

sbarzowski added a commit to sbarzowski/jsonnet that referenced this issue Dec 20, 2019
sbarzowski added a commit to sbarzowski/jsonnet that referenced this issue Dec 20, 2019
sbarzowski added a commit to sbarzowski/jsonnet that referenced this issue Dec 20, 2019
sbarzowski added a commit that referenced this issue Feb 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants