Skip to content

Add Iterator.prototype bindings #7506

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
# Run integration tests with the oldest supported node version.
node-version: 20

- name: Make test directory
id: tmp-dir
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20
22
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Add `Array.findLast`, `Array.findLastWithIndex`, `Array.findLastIndex`, `Array.findLastIndexWithIndex` and `Array.findLastIndexOpt`. https://github.com/rescript-lang/rescript/pull/7503
- Add `options` argument to `Console.dir`. https://github.com/rescript-lang/rescript/pull/7504
- Show variant constructor's inline record types on hover. https://github.com/rescript-lang/rescript/pull/7519
- Add additional `Iterator.prototype` bindings to `runtime/Stdlib_Iterator.res`. https://github.com/rescript-lang/rescript/pull/7506

#### :bug: Bug fix

Expand All @@ -34,6 +35,9 @@
- Remove deprecated pipe last (`|>`) syntax. https://github.com/rescript-lang/rescript/pull/7512
- Improve error message for pipe (`->`) syntax. https://github.com/rescript-lang/rescript/pull/7520

#### :boom: Breaking Change
- `Iterator.forEach` now emits `Iterator.prototype.forEach` call. https://github.com/rescript-lang/rescript/pull/7506

# 12.0.0-alpha.13

#### :boom: Breaking Change
Expand Down
17 changes: 1 addition & 16 deletions lib/es6/Stdlib_Iterator.js
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@



function forEach(iterator, f) {
let iteratorDone = false;
while (!iteratorDone) {
let match = iterator.next();
f(match.value);
iteratorDone = match.done;
};
}

export {
forEach,
}
/* No side effect */
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
15 changes: 1 addition & 14 deletions lib/js/Stdlib_Iterator.js
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
'use strict';


function forEach(iterator, f) {
let iteratorDone = false;
while (!iteratorDone) {
let match = iterator.next();
f(match.value);
iteratorDone = match.done;
};
}

exports.forEach = forEach;
/* No side effect */
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
6 changes: 6 additions & 0 deletions runtime/Stdlib_Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,9 @@ let findMap = (arr, f) => {
let last = a => a->get(a->length - 1)

external ignore: array<'a> => unit = "%ignore"

@send
external entries: array<'a> => Stdlib_Iterator.t<(int, 'a)> = "entries"

@send
external values: array<'a> => Stdlib_Iterator.t<'a> = "values"
34 changes: 34 additions & 0 deletions runtime/Stdlib_Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -1387,3 +1387,37 @@ let last: array<'a> => option<'a>
without having to store or process it further.
*/
external ignore: array<'a> => unit = "%ignore"

/**
`entries(array)` returns a new array iterator object that contains the key/value pairs for each index in the array.

See [Array.prototype.entries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries) on MDN.

## Examples

```rescript
let array = [5, 6, 7]
let iterator : Iterator.t<(int, int)> = array->Array.entries
iterator->Iterator.next->assertEqual({done: false, value: Some((0, 5))})
iterator->Iterator.next->assertEqual({done: false, value: Some((1, 6))})
```
*/
@send
external entries: array<'a> => Stdlib_Iterator.t<(int, 'a)> = "entries"

/**
`values(array)` returns a new array iterator object that contains the values for each index in the array.

See [Array.prototype.values](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values) on MDN.

## Examples

```rescript
let array = [5, 6, 7]
let iterator : Iterator.t<int> = array->Array.values
iterator->Iterator.next->assertEqual({done: false, value: Some(5)})
iterator->Iterator.next->assertEqual({done: false, value: Some(6)})
```
*/
@send
external values: array<'a> => Stdlib_Iterator.t<'a> = "values"
41 changes: 31 additions & 10 deletions runtime/Stdlib_Iterator.res
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,38 @@ type value<'a> = {
}

@send external next: t<'a> => value<'a> = "next"
external toArray: t<'a> => array<'a> = "Array.from"
@send
external toArray: t<'a> => array<'a> = "toArray"
external toArrayWithMapper: (t<'a>, 'a => 'b) => array<'b> = "Array.from"

let forEach = (iterator, f) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm for this change 👍

Iterator.forEach's behaviour was different to Iterator.prototype.forEach as the latter only calls its callback with yielded values, not the iterator's final return value.

let iteratorDone = ref(false)

while !iteratorDone.contents {
let {done, value} = iterator->next
f(value)
iteratorDone := done
}
}
@send
external forEach: (t<'a>, 'a => unit) => unit = "forEach"

external ignore: t<'a> => unit = "%ignore"

@send
external drop: (t<'a>, int) => t<'a> = "drop"

@send
external every: (t<'a>, 'a => bool) => bool = "every"

@send
external filter: (t<'a>, 'a => bool) => t<'a> = "filter"

@send
external find: (t<'a>, 'a => bool) => option<'a> = "find"

@send
external flatMap: (t<'a>, 'a => t<'b>) => t<'b> = "flatMap"

@send
external map: (t<'a>, 'a => 'b) => t<'b> = "map"

@send
external reduce: (t<'a>, ('acc, 'a) => 'acc, ~initialValue: 'acc=?) => 'acc = "reduce"

@send
external some: (t<'a>, 'a => bool) => bool = "some"

@send
external take: (t<'a>, int) => t<'a> = "take"
Loading
Loading