Skip to content

add Int.clamp and Float.clamp #90

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

Merged
merged 5 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
- Add `Int.range` and `Int.rangeWithOptions`, https://github.com/rescript-association/rescript-core/pull/52
- Remove `Array.fromIterator` and `Array.fromIteratorWithMap`. The same functions exist in `Iterator` as `Iterator.fromArray` and `Iterator.fromArrayWithMapper`. https://github.com/rescript-association/rescript-core/pull/78
- Remove unsafe `Array.from` and `Array.fromWithMap`. https://github.com/rescript-association/rescript-core/pull/78
- Add `Int.clamp` and `Float.clamp`, https://github.com/rescript-association/rescript-core/pull/90

### Documentation

Expand Down
10 changes: 10 additions & 0 deletions src/Core__Float.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ function fromString(i) {
}
}

function clamp(min, max, value) {
var value$1 = max !== undefined && max < value ? max : value;
if (min !== undefined && min > value$1) {
return min;
} else {
return value$1;
}
}

export {
Constants ,
equal ,
compare ,
fromString ,
clamp ,
}
/* No side effect */
11 changes: 11 additions & 0 deletions src/Core__Float.res
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ external toInt: float => int = "%intoffloat"
external fromInt: int => float = "%identity"

@unboxed @noalloc external mod: (float, float) => float = "?fmod_float"

let clamp = (~min=?, ~max=?, value): float => {
let value = switch max {
| Some(max) if max < value => max
| _ => value
}
switch min {
| Some(min) if min > value => min
| _ => value
}
}
16 changes: 16 additions & 0 deletions src/Core__Float.resi
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,19 @@ Int.mod(7.0, 4.0) == 3
```
*/
external mod: (float, float) => float = "?fmod_float"

/**
`clamp(~min=?, ~max=?, value)` returns `value`, optionally bounded by `min` and `max`.

if `max` < `min` returns `min`.

## Examples

```rescript
Int.clamp(4.2) == 4.2
Int.clamp(4.2, ~min=4.3) == 4.3
Int.clamp(4.2, ~max=4.1) == 4.1
Int.clamp(4.2, ~min=4.3, ~max=4.1) == 4.3
```
*/
let clamp: (~min: float=?, ~max: float=?, float) => float
10 changes: 10 additions & 0 deletions src/Core__Int.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ function range(start, end) {
return rangeWithOptions(start, end, {});
}

function clamp(min, max, value) {
var value$1 = max !== undefined && max < value ? max : value;
if (min !== undefined && min > value$1) {
return min;
} else {
return value$1;
}
}

var Constants = {
minValue: -2147483648,
maxValue: 2147483647
Expand All @@ -73,5 +82,6 @@ export {
fromString ,
range ,
rangeWithOptions ,
clamp ,
}
/* No side effect */
11 changes: 11 additions & 0 deletions src/Core__Int.res
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,14 @@ let rangeWithOptions = (start, end, options) => {
}

let range = (start, end) => rangeWithOptions(start, end, {})

let clamp = (~min=?, ~max=?, value): int => {
let value = switch max {
| Some(max) if max < value => max
| _ => value
}
switch min {
| Some(min) if min > value => min
| _ => value
}
}
16 changes: 16 additions & 0 deletions src/Core__Int.resi
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,19 @@ Int.rangeWithOptions(3, 6, {step: -2}) // RangeError
- Raises `RangeError` if `step == 0 && start != end`.
*/
let rangeWithOptions: (int, int, rangeOptions) => array<int>

/**
`clamp(~min=?, ~max=?, value)` returns `value`, optionally bounded by `min` and `max`.

if `max` < `min` returns `min`.

## Examples

```rescript
Int.clamp(42) == 42
Int.clamp(42, ~min=50) == 50
Int.clamp(42, ~max=40) == 40
Int.clamp(42, ~min=50, ~max=40) == 50
```
*/
let clamp: (~min: int=?, ~max: int=?, int) => int
243 changes: 243 additions & 0 deletions test/FloatTests.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Test from "./Test.mjs";
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
import * as Pervasives from "rescript/lib/es6/pervasives.js";
import * as Core__Float from "../src/Core__Float.mjs";

var eq = Caml_obj.equal;

Test.run([
[
"FloatTests.res",
5,
20,
27
],
"clamp"
], Core__Float.clamp(undefined, undefined, 4.2), eq, 4.2);

Test.run([
[
"FloatTests.res",
6,
20,
35
],
"clamp - < min"
], Core__Float.clamp(4.3, undefined, 4.1), eq, 4.3);

Test.run([
[
"FloatTests.res",
7,
20,
35
],
"clamp - > min"
], Core__Float.clamp(4.1, undefined, 4.2), eq, 4.2);

Test.run([
[
"FloatTests.res",
8,
20,
35
],
"clamp - < max"
], Core__Float.clamp(undefined, 4.3, 4.2), eq, 4.2);

Test.run([
[
"FloatTests.res",
9,
20,
35
],
"clamp - > max"
], Core__Float.clamp(undefined, 4.1, 4.2), eq, 4.1);

Test.run([
[
"FloatTests.res",
10,
20,
42
],
"clamp - < min, < max"
], Core__Float.clamp(4.3, 4.5, 4.2), eq, 4.3);

Test.run([
[
"FloatTests.res",
11,
20,
42
],
"clamp - < min, > max"
], Core__Float.clamp(4.3, 4.1, 4.2), eq, 4.3);

Test.run([
[
"FloatTests.res",
12,
20,
42
],
"clamp - > min, < max"
], Core__Float.clamp(4.1, 4.5, 4.2), eq, 4.2);

Test.run([
[
"FloatTests.res",
13,
20,
42
],
"clamp - > min, > max"
], Core__Float.clamp(4.1, 4.1, 4.2), eq, 4.1);

Test.run([
[
"FloatTests.res",
14,
20,
33
],
"clamp - nan"
], isNaN(Core__Float.clamp(4.1, 4.3, Number.NaN)), eq, true);

Test.run([
[
"FloatTests.res",
15,
20,
38
],
"clamp - infinity"
], Core__Float.clamp(4.1, 4.3, Pervasives.infinity), eq, 4.3);

Test.run([
[
"FloatTests.res",
16,
20,
39
],
"clamp - -infinity"
], Core__Float.clamp(4.1, 4.3, Pervasives.neg_infinity), eq, 4.1);

Test.run([
[
"FloatTests.res",
17,
20,
37
],
"clamp - min nan"
], Core__Float.clamp(Number.NaN, undefined, 4.2), eq, 4.2);

Test.run([
[
"FloatTests.res",
18,
20,
37
],
"clamp - max nan"
], Core__Float.clamp(undefined, Number.NaN, 4.2), eq, 4.2);

Test.run([
[
"FloatTests.res",
19,
20,
46
],
"clamp - min nan, max nan"
], Core__Float.clamp(Number.NaN, Number.NaN, 4.2), eq, 4.2);

Test.run([
[
"FloatTests.res",
20,
20,
42
],
"clamp - min infinity"
], Core__Float.clamp(Pervasives.infinity, undefined, 4.2), eq, Pervasives.infinity);

Test.run([
[
"FloatTests.res",
21,
20,
42
],
"clamp - max infinity"
], Core__Float.clamp(undefined, Pervasives.infinity, 4.2), eq, 4.2);

Test.run([
[
"FloatTests.res",
22,
20,
43
],
"clamp - min -infinity"
], Core__Float.clamp(Pervasives.neg_infinity, undefined, 4.2), eq, 4.2);

Test.run([
[
"FloatTests.res",
23,
20,
43
],
"clamp - max -infinity"
], Core__Float.clamp(undefined, Pervasives.neg_infinity, 4.2), eq, Pervasives.neg_infinity);

Test.run([
[
"FloatTests.res",
25,
13,
49
],
"clamp - min infinity, max infinity"
], Core__Float.clamp(Pervasives.infinity, Pervasives.infinity, 4.2), eq, Pervasives.infinity);

Test.run([
[
"FloatTests.res",
31,
13,
50
],
"clamp - min -infinity, max infinity"
], Core__Float.clamp(Pervasives.neg_infinity, Pervasives.infinity, 4.2), eq, 4.2);

Test.run([
[
"FloatTests.res",
37,
13,
50
],
"clamp - min infinity, max -infinity"
], Core__Float.clamp(Pervasives.infinity, Pervasives.neg_infinity, 4.2), eq, Pervasives.infinity);

Test.run([
[
"FloatTests.res",
43,
13,
51
],
"clamp - min -infinity, max -infinity"
], Core__Float.clamp(Pervasives.neg_infinity, Pervasives.neg_infinity, 4.2), eq, Pervasives.neg_infinity);

export {
eq ,
}
/* Not a pure module */
Loading