-
-
Notifications
You must be signed in to change notification settings - Fork 310
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
Adds a Num.restrictToInterval function #6286
base: main
Are you sure you want to change the base?
Changes from all commits
323fd8a
a0a5a5d
56c386a
2c50f37
942e293
9bf29fb
c4a3f7d
6764ac1
09f7bdd
eaa198f
827566c
ff2f684
ee0e148
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -44,6 +44,7 @@ interface Num | |||
mul, | ||||
min, | ||||
max, | ||||
restrictToInterval, | ||||
isLt, | ||||
isLte, | ||||
isGt, | ||||
|
@@ -867,6 +868,41 @@ max = \a, b -> | |||
else | ||||
b | ||||
|
||||
## Restricts a number to be between two other numbers. | ||||
## | ||||
## ``` | ||||
## restrictToInterval 5 { startAt: 3, endAt: 8 } == 5 | ||||
## restrictToInterval 1 { startAt: 3, endAt: 8 } == 3 | ||||
## restrictToInterval 10 { startAt: 3, endAt: 8 } == 8 | ||||
|
||||
## restrictToInterval 5 { startAt: 8, endAt: 3 } == 5 | ||||
## restrictToInterval 1 { startAt: 8, endAt: 3 } == 3 | ||||
## restrictToInterval 10 { startAt: 8, endAt: 3 } == 8 | ||||
## ``` | ||||
## You may know this function as `clamp` in other languages. | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd remove this from the Roc documentation. On the docs website, there is a way to list functions with other names in other languages: roc/www/public/different-names/index.html Line 19 in 26630ee
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see you've already done the later. |
||||
restrictToInterval : Num a, { startAt : Num a, endAt : Num a } -> Num a | ||||
restrictToInterval = \x, { startAt, endAt } -> | ||||
if endAt > startAt then | ||||
restrictToInterval x { startAt: endAt, endAt: startAt } | ||||
Comment on lines
+885
to
+886
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem right to me. Did you mean Separately, I wonder if that's the desired behavior. Maybe we should just clamp to the |
||||
else if x < startAt then | ||||
startAt | ||||
else if x > endAt then | ||||
endAt | ||||
else | ||||
x | ||||
|
||||
expect restrictToInterval 5 { startAt: 3, endAt: 8 } == 5 | ||||
expect restrictToInterval 1 { startAt: 3, endAt: 8 } == 3 | ||||
expect restrictToInterval 10 { startAt: 3, endAt: 8 } == 8 | ||||
expect restrictToInterval -5 { startAt: 3, endAt: 8 } == 3 | ||||
expect restrictToInterval -2 { startAt: -5, endAt: 5 } == -2 | ||||
expect restrictToInterval 7 { startAt: -5, endAt: 5 } == 5 | ||||
expect restrictToInterval 3 { startAt: -5, endAt: 5 } == 3 | ||||
|
||||
expect restrictToInterval 5 { startAt: 8, endAt: 3 } == 5 | ||||
expect restrictToInterval 1 { startAt: 8, endAt: 3 } == 3 | ||||
expect restrictToInterval 10 { startAt: 8, endAt: 3 } == 8 | ||||
|
||||
sin : Frac a -> Frac a | ||||
cos : Frac a -> Frac a | ||||
tan : Frac a -> Frac a | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.