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

Adds a Num.restrictToInterval function #6286

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
5 changes: 5 additions & 0 deletions crates/cli_testing_examples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ benchmarks/rbtree-ck
benchmarks/rbtree-insert
benchmarks/test-astar
benchmarks/test-base64
benchmarks/CFold
benchmarks/Closure
benchmarks/Deriv
benchmarks/Issue2279
benchmarks/NQueens
36 changes: 36 additions & 0 deletions crates/compiler/builtins/roc/Num.roc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface Num
mul,
min,
max,
restrictToInterval,
isLt,
isLte,
isGt,
Expand Down Expand Up @@ -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
Comment on lines +876 to +878
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
## restrictToInterval 10 { startAt: 3, endAt: 8 } == 8
## restrictToInterval 5 { startAt: 8, endAt: 3 } == 5
## 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.
Copy link
Member

Choose a reason for hiding this comment

The 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:

<h1>Different Names</h1>

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem right to me. Did you mean endAt < startAt?

Separately, I wonder if that's the desired behavior. Maybe we should just clamp to the startAt value in this case?

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
Expand Down
1 change: 1 addition & 0 deletions crates/compiler/module/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,7 @@ define_builtins! {
162 NUM_PI: "pi"
163 NUM_TAU: "tau"
164 NUM_BITWISE_NOT: "bitwiseNot"
165 NUM_RESTRICT_TO_INTERVAL: "restrictToInterval"
}
4 BOOL: "Bool" => {
0 BOOL_BOOL: "Bool" exposed_type=true // the Bool.Bool type alias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ procedure List.99 (List.468, List.469, List.470):
ret List.570;

procedure Num.22 (#Attr.2, #Attr.3):
let Num.294 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.294;
let Num.309 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.309;

procedure Num.51 (#Attr.2, #Attr.3):
let Num.293 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.293;
let Num.308 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.308;

procedure Num.77 (#Attr.2, #Attr.3):
let Num.292 : U64 = lowlevel NumSubSaturated #Attr.2 #Attr.3;
ret Num.292;
let Num.307 : U64 = lowlevel NumSubSaturated #Attr.2 #Attr.3;
ret Num.307;

procedure Test.1 (Test.2):
let Test.13 : U64 = 0i64;
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/test_mono/generated/binary_tree_fbip.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
procedure Num.19 (#Attr.2, #Attr.3):
let Num.291 : I64 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.291;
let Num.306 : I64 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.306;

procedure Test.4 (Test.27):
let Test.39 : [<rnu>C [<rnu><null>, C *self *self] *self, <null>] = TagId(0) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ procedure List.88 (#Derived_gen.3, #Derived_gen.4, #Derived_gen.5, #Derived_gen.
jump List.556 #Derived_gen.3 #Derived_gen.4 #Derived_gen.5 #Derived_gen.6 #Derived_gen.7;

procedure Num.22 (#Attr.2, #Attr.3):
let Num.292 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.292;
let Num.307 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.307;

procedure Num.51 (#Attr.2, #Attr.3):
let Num.291 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.291;
let Num.306 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.306;

procedure Test.10 (Test.69, #Attr.12):
let Test.72 : {} = UnionAtIndex (Id 0) (Index 0) #Attr.12;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ procedure List.9 (List.315):
ret List.555;

procedure Num.22 (#Attr.2, #Attr.3):
let Num.291 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.291;
let Num.306 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.306;

procedure Result.5 (Result.12, Result.13):
let Result.39 : U8 = 1i64;
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/test_mono/generated/choose_i128_layout.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
procedure Num.19 (#Attr.2, #Attr.3):
let Num.292 : I128 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.292;
let Num.307 : I128 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.307;

procedure Test.0 ():
let Test.6 : I128 = 18446744073709551616i64;
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/test_mono/generated/choose_u128_layout.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
procedure Num.19 (#Attr.2, #Attr.3):
let Num.291 : U128 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.291;
let Num.306 : U128 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.306;

procedure Test.0 ():
let Test.2 : U128 = 170141183460469231731687303715884105728u128;
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/test_mono/generated/choose_u64_layout.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
procedure Num.19 (#Attr.2, #Attr.3):
let Num.291 : U64 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.291;
let Num.306 : U64 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.306;

procedure Test.0 ():
let Test.2 : U64 = 9999999999999999999i64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ procedure List.88 (#Derived_gen.0, #Derived_gen.1, #Derived_gen.2, #Derived_gen.
jump List.556 #Derived_gen.0 #Derived_gen.1 #Derived_gen.2 #Derived_gen.3 #Derived_gen.4;

procedure Num.22 (#Attr.2, #Attr.3):
let Num.292 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.292;
let Num.307 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.307;

procedure Num.51 (#Attr.2, #Attr.3):
let Num.291 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.291;
let Num.306 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.306;

procedure Str.3 (#Attr.2, #Attr.3):
let Str.294 : Str = lowlevel StrConcat #Attr.2 #Attr.3;
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/test_mono/generated/dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ procedure List.6 (#Attr.2):
ret List.553;

procedure Num.20 (#Attr.2, #Attr.3):
let Num.291 : U8 = lowlevel NumSub #Attr.2 #Attr.3;
ret Num.291;
let Num.306 : U8 = lowlevel NumSub #Attr.2 #Attr.3;
ret Num.306;

procedure Test.0 ():
let Test.3 : {} = Struct {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ procedure List.66 (#Attr.2, #Attr.3):
ret List.558;

procedure Num.22 (#Attr.2, #Attr.3):
let Num.291 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.291;
let Num.306 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.306;

procedure Test.2 (Test.5):
dec Test.5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,44 +311,44 @@ procedure List.99 (List.468, List.469, List.470):
ret List.732;

procedure Num.127 (#Attr.2):
let Num.312 : U8 = lowlevel NumIntCast #Attr.2;
ret Num.312;
let Num.327 : U8 = lowlevel NumIntCast #Attr.2;
ret Num.327;

procedure Num.19 (#Attr.2, #Attr.3):
let Num.316 : U64 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.316;
let Num.331 : U64 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.331;

procedure Num.20 (#Attr.2, #Attr.3):
let Num.313 : U64 = lowlevel NumSub #Attr.2 #Attr.3;
ret Num.313;
let Num.328 : U64 = lowlevel NumSub #Attr.2 #Attr.3;
ret Num.328;

procedure Num.21 (#Attr.2, #Attr.3):
let Num.318 : U64 = lowlevel NumMul #Attr.2 #Attr.3;
ret Num.318;
let Num.333 : U64 = lowlevel NumMul #Attr.2 #Attr.3;
ret Num.333;

procedure Num.22 (#Attr.2, #Attr.3):
let Num.324 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.324;
let Num.339 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.339;

procedure Num.24 (#Attr.2, #Attr.3):
let Num.332 : Int1 = lowlevel NumGt #Attr.2 #Attr.3;
ret Num.332;
let Num.347 : Int1 = lowlevel NumGt #Attr.2 #Attr.3;
ret Num.347;

procedure Num.51 (#Attr.2, #Attr.3):
let Num.321 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.321;
let Num.336 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.336;

procedure Num.75 (#Attr.2, #Attr.3):
let Num.331 : U64 = lowlevel NumSubWrap #Attr.2 #Attr.3;
ret Num.331;
let Num.346 : U64 = lowlevel NumSubWrap #Attr.2 #Attr.3;
ret Num.346;

procedure Num.77 (#Attr.2, #Attr.3):
let Num.330 : U64 = lowlevel NumSubSaturated #Attr.2 #Attr.3;
ret Num.330;
let Num.345 : U64 = lowlevel NumSubSaturated #Attr.2 #Attr.3;
ret Num.345;

procedure Num.94 (#Attr.2, #Attr.3):
let Num.317 : U64 = lowlevel NumDivCeilUnchecked #Attr.2 #Attr.3;
ret Num.317;
let Num.332 : U64 = lowlevel NumDivCeilUnchecked #Attr.2 #Attr.3;
ret Num.332;

procedure Str.12 (#Attr.2):
let Str.310 : List U8 = lowlevel StrToUtf8 #Attr.2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,44 +254,44 @@ procedure List.99 (List.468, List.469, List.470):
ret List.664;

procedure Num.127 (#Attr.2):
let Num.296 : U8 = lowlevel NumIntCast #Attr.2;
ret Num.296;
let Num.311 : U8 = lowlevel NumIntCast #Attr.2;
ret Num.311;

procedure Num.19 (#Attr.2, #Attr.3):
let Num.300 : U64 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.300;
let Num.315 : U64 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.315;

procedure Num.20 (#Attr.2, #Attr.3):
let Num.297 : U64 = lowlevel NumSub #Attr.2 #Attr.3;
ret Num.297;
let Num.312 : U64 = lowlevel NumSub #Attr.2 #Attr.3;
ret Num.312;

procedure Num.21 (#Attr.2, #Attr.3):
let Num.302 : U64 = lowlevel NumMul #Attr.2 #Attr.3;
ret Num.302;
let Num.317 : U64 = lowlevel NumMul #Attr.2 #Attr.3;
ret Num.317;

procedure Num.22 (#Attr.2, #Attr.3):
let Num.308 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.308;
let Num.323 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.323;

procedure Num.24 (#Attr.2, #Attr.3):
let Num.316 : Int1 = lowlevel NumGt #Attr.2 #Attr.3;
ret Num.316;
let Num.331 : Int1 = lowlevel NumGt #Attr.2 #Attr.3;
ret Num.331;

procedure Num.51 (#Attr.2, #Attr.3):
let Num.305 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.305;
let Num.320 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.320;

procedure Num.75 (#Attr.2, #Attr.3):
let Num.315 : U64 = lowlevel NumSubWrap #Attr.2 #Attr.3;
ret Num.315;
let Num.330 : U64 = lowlevel NumSubWrap #Attr.2 #Attr.3;
ret Num.330;

procedure Num.77 (#Attr.2, #Attr.3):
let Num.314 : U64 = lowlevel NumSubSaturated #Attr.2 #Attr.3;
ret Num.314;
let Num.329 : U64 = lowlevel NumSubSaturated #Attr.2 #Attr.3;
ret Num.329;

procedure Num.94 (#Attr.2, #Attr.3):
let Num.301 : U64 = lowlevel NumDivCeilUnchecked #Attr.2 #Attr.3;
ret Num.301;
let Num.316 : U64 = lowlevel NumDivCeilUnchecked #Attr.2 #Attr.3;
ret Num.316;

procedure Str.12 (#Attr.2):
let Str.303 : List U8 = lowlevel StrToUtf8 #Attr.2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,44 +261,44 @@ procedure List.99 (List.468, List.469, List.470):
ret List.664;

procedure Num.127 (#Attr.2):
let Num.296 : U8 = lowlevel NumIntCast #Attr.2;
ret Num.296;
let Num.311 : U8 = lowlevel NumIntCast #Attr.2;
ret Num.311;

procedure Num.19 (#Attr.2, #Attr.3):
let Num.300 : U64 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.300;
let Num.315 : U64 = lowlevel NumAdd #Attr.2 #Attr.3;
ret Num.315;

procedure Num.20 (#Attr.2, #Attr.3):
let Num.297 : U64 = lowlevel NumSub #Attr.2 #Attr.3;
ret Num.297;
let Num.312 : U64 = lowlevel NumSub #Attr.2 #Attr.3;
ret Num.312;

procedure Num.21 (#Attr.2, #Attr.3):
let Num.302 : U64 = lowlevel NumMul #Attr.2 #Attr.3;
ret Num.302;
let Num.317 : U64 = lowlevel NumMul #Attr.2 #Attr.3;
ret Num.317;

procedure Num.22 (#Attr.2, #Attr.3):
let Num.308 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.308;
let Num.323 : Int1 = lowlevel NumLt #Attr.2 #Attr.3;
ret Num.323;

procedure Num.24 (#Attr.2, #Attr.3):
let Num.316 : Int1 = lowlevel NumGt #Attr.2 #Attr.3;
ret Num.316;
let Num.331 : Int1 = lowlevel NumGt #Attr.2 #Attr.3;
ret Num.331;

procedure Num.51 (#Attr.2, #Attr.3):
let Num.305 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.305;
let Num.320 : U64 = lowlevel NumAddWrap #Attr.2 #Attr.3;
ret Num.320;

procedure Num.75 (#Attr.2, #Attr.3):
let Num.315 : U64 = lowlevel NumSubWrap #Attr.2 #Attr.3;
ret Num.315;
let Num.330 : U64 = lowlevel NumSubWrap #Attr.2 #Attr.3;
ret Num.330;

procedure Num.77 (#Attr.2, #Attr.3):
let Num.314 : U64 = lowlevel NumSubSaturated #Attr.2 #Attr.3;
ret Num.314;
let Num.329 : U64 = lowlevel NumSubSaturated #Attr.2 #Attr.3;
ret Num.329;

procedure Num.94 (#Attr.2, #Attr.3):
let Num.301 : U64 = lowlevel NumDivCeilUnchecked #Attr.2 #Attr.3;
ret Num.301;
let Num.316 : U64 = lowlevel NumDivCeilUnchecked #Attr.2 #Attr.3;
ret Num.316;

procedure Str.12 (#Attr.2):
let Str.303 : List U8 = lowlevel StrToUtf8 #Attr.2;
Expand Down
Loading