Skip to content

Commit f36931f

Browse files
committed
feat(int): add range and rangeWithOptions
1 parent 85372d5 commit f36931f

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/Core__Int.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3+
import * as Core__Array from "./Core__Array.mjs";
34

45
function fromString(radix, x) {
56
var maybeInt = radix !== undefined ? parseInt(x, radix) : parseInt(x);
@@ -10,6 +11,20 @@ function fromString(radix, x) {
1011
}
1112
}
1213

14+
function rangeWithOptions(start, end, options) {
15+
var n = options.step;
16+
var step = n !== undefined ? n : 1;
17+
var match = options.inclusive;
18+
var end$1 = match !== undefined && match ? end + 1 | 0 : end;
19+
return Core__Array.init(end$1 - start | 0, (function (i) {
20+
return Math.imul(start, Math.imul(i, step));
21+
}));
22+
}
23+
24+
function range(start, end) {
25+
return rangeWithOptions(start, end, {});
26+
}
27+
1328
var Constants = {
1429
minValue: -2147483648,
1530
maxValue: 2147483647
@@ -18,5 +33,7 @@ var Constants = {
1833
export {
1934
Constants ,
2035
fromString ,
36+
rangeWithOptions ,
37+
range ,
2138
}
2239
/* No side effect */

src/Core__Int.res

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,20 @@ let fromString = (~radix=?, x) => {
3535
}
3636

3737
external mod: (int, int) => int = "%modint"
38+
39+
type rangeOptions = {step?: int, inclusive?: bool}
40+
let rangeWithOptions = (start, end, options) => {
41+
let step = switch options.step {
42+
| None => 1
43+
| Some(n) => n
44+
}
45+
46+
let end = switch options.inclusive {
47+
| Some(true) => end + 1
48+
| _ => end
49+
}
50+
51+
Core__Array.init(end - start, i => start * (i * step))
52+
}
53+
54+
let range = (start, end) => rangeWithOptions(start, end, {})

src/Core__Int.resi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,7 @@ Int.mod(7, 4) == 3
268268
```
269269
*/
270270
external mod: (int, int) => int = "%modint"
271+
272+
type rangeOptions = {step?: int, inclusive?: bool}
273+
let rangeWithOptions: (int, int, rangeOptions) => array<int>
274+
let range: (int, int) => array<int>

0 commit comments

Comments
 (0)