Skip to content

Commit 6ddfd80

Browse files
nojafcknitt
andauthored
Add Iterator.prototype bindings (#7506)
* Add Iterator.prototype bindings * Fix tests * Polyfill `Iterator.prototype.forEach` for node 20 * Polyfill iterator * Use assertEqual instead of Console.log * Revert iterator polyfill * Use node 22 for dev * Update .github/workflows/ci.yml Co-authored-by: Christoph Knittel <[email protected]> * Revert Core_TempTests.res * Remove import from compiled js * Add changelog entries * Comment out failing test * Add remark about Baseline 2025 * Fix code samples --------- Co-authored-by: Christoph Knittel <[email protected]>
1 parent 1004d78 commit 6ddfd80

File tree

15 files changed

+443
-218
lines changed

15 files changed

+443
-218
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@ jobs:
487487
- name: Use Node.js
488488
uses: actions/setup-node@v4
489489
with:
490-
node-version-file: .nvmrc
490+
# Run integration tests with the oldest supported node version.
491+
node-version: 20
491492

492493
- name: Make test directory
493494
id: tmp-dir

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20
1+
22

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- Add `Array.findLast`, `Array.findLastWithIndex`, `Array.findLastIndex`, `Array.findLastIndexWithIndex` and `Array.findLastIndexOpt`. https://github.com/rescript-lang/rescript/pull/7503
2929
- Add `options` argument to `Console.dir`. https://github.com/rescript-lang/rescript/pull/7504
3030
- Show variant constructor's inline record types on hover. https://github.com/rescript-lang/rescript/pull/7519
31+
- Add additional `Iterator.prototype` bindings to `runtime/Stdlib_Iterator.res`. https://github.com/rescript-lang/rescript/pull/7506
3132

3233
#### :bug: Bug fix
3334

@@ -60,6 +61,9 @@
6061
- Editor: add completions from included modules. https://github.com/rescript-lang/rescript/pull/7515
6162
- Add `-editor-mode` arg to `bsc` for doing special optimizations only relevant to the editor tooling. https://github.com/rescript-lang/rescript/pull/7541
6263

64+
#### :boom: Breaking Change
65+
- `Iterator.forEach` now emits `Iterator.prototype.forEach` call. https://github.com/rescript-lang/rescript/pull/7506
66+
6367
# 12.0.0-alpha.13
6468

6569
#### :boom: Breaking Change

lib/es6/Stdlib_Iterator.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1 @@
1-
2-
3-
4-
function forEach(iterator, f) {
5-
let iteratorDone = false;
6-
while (!iteratorDone) {
7-
let match = iterator.next();
8-
f(match.value);
9-
iteratorDone = match.done;
10-
};
11-
}
12-
13-
export {
14-
forEach,
15-
}
16-
/* No side effect */
1+
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */

lib/js/Stdlib_Iterator.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1 @@
1-
'use strict';
2-
3-
4-
function forEach(iterator, f) {
5-
let iteratorDone = false;
6-
while (!iteratorDone) {
7-
let match = iterator.next();
8-
f(match.value);
9-
iteratorDone = match.done;
10-
};
11-
}
12-
13-
exports.forEach = forEach;
14-
/* No side effect */
1+
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */

runtime/Stdlib_Array.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,9 @@ let findMap = (arr, f) => {
287287
let last = a => a->get(a->length - 1)
288288

289289
external ignore: array<'a> => unit = "%ignore"
290+
291+
@send
292+
external entries: array<'a> => Stdlib_Iterator.t<(int, 'a)> = "entries"
293+
294+
@send
295+
external values: array<'a> => Stdlib_Iterator.t<'a> = "values"

runtime/Stdlib_Array.resi

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,3 +1382,37 @@ let last: array<'a> => option<'a>
13821382
without having to store or process it further.
13831383
*/
13841384
external ignore: array<'a> => unit = "%ignore"
1385+
1386+
/**
1387+
`entries(array)` returns a new array iterator object that contains the key/value pairs for each index in the array.
1388+
1389+
See [Array.prototype.entries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries) on MDN.
1390+
1391+
## Examples
1392+
1393+
```rescript
1394+
let array = [5, 6, 7]
1395+
let iterator : Iterator.t<(int, int)> = array->Array.entries
1396+
iterator->Iterator.next->assertEqual({done: false, value: Some((0, 5))})
1397+
iterator->Iterator.next->assertEqual({done: false, value: Some((1, 6))})
1398+
```
1399+
*/
1400+
@send
1401+
external entries: array<'a> => Stdlib_Iterator.t<(int, 'a)> = "entries"
1402+
1403+
/**
1404+
`values(array)` returns a new array iterator object that contains the values for each index in the array.
1405+
1406+
See [Array.prototype.values](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values) on MDN.
1407+
1408+
## Examples
1409+
1410+
```rescript
1411+
let array = [5, 6, 7]
1412+
let iterator : Iterator.t<int> = array->Array.values
1413+
iterator->Iterator.next->assertEqual({done: false, value: Some(5)})
1414+
iterator->Iterator.next->assertEqual({done: false, value: Some(6)})
1415+
```
1416+
*/
1417+
@send
1418+
external values: array<'a> => Stdlib_Iterator.t<'a> = "values"

runtime/Stdlib_Iterator.res

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,38 @@ type value<'a> = {
77
}
88

99
@send external next: t<'a> => value<'a> = "next"
10-
external toArray: t<'a> => array<'a> = "Array.from"
10+
@send
11+
external toArray: t<'a> => array<'a> = "toArray"
1112
external toArrayWithMapper: (t<'a>, 'a => 'b) => array<'b> = "Array.from"
1213

13-
let forEach = (iterator, f) => {
14-
let iteratorDone = ref(false)
15-
16-
while !iteratorDone.contents {
17-
let {done, value} = iterator->next
18-
f(value)
19-
iteratorDone := done
20-
}
21-
}
14+
@send
15+
external forEach: (t<'a>, 'a => unit) => unit = "forEach"
2216

2317
external ignore: t<'a> => unit = "%ignore"
18+
19+
@send
20+
external drop: (t<'a>, int) => t<'a> = "drop"
21+
22+
@send
23+
external every: (t<'a>, 'a => bool) => bool = "every"
24+
25+
@send
26+
external filter: (t<'a>, 'a => bool) => t<'a> = "filter"
27+
28+
@send
29+
external find: (t<'a>, 'a => bool) => option<'a> = "find"
30+
31+
@send
32+
external flatMap: (t<'a>, 'a => t<'b>) => t<'b> = "flatMap"
33+
34+
@send
35+
external map: (t<'a>, 'a => 'b) => t<'b> = "map"
36+
37+
@send
38+
external reduce: (t<'a>, ('acc, 'a) => 'acc, ~initialValue: 'acc=?) => 'acc = "reduce"
39+
40+
@send
41+
external some: (t<'a>, 'a => bool) => bool = "some"
42+
43+
@send
44+
external take: (t<'a>, int) => t<'a> = "take"

0 commit comments

Comments
 (0)