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

Merged
merged 16 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use assertEqual instead of Console.log
  • Loading branch information
nojaf committed May 23, 2025
commit 23d80f9925b18ca43f44f05b15d1d8a72a475763
2 changes: 1 addition & 1 deletion runtime/Stdlib_Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ let last = a => a->get(a->length - 1)
external ignore: array<'a> => unit = "%ignore"

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

@send
external values: array<'a> => Stdlib_Iterator.t<'a> = "values"
12 changes: 6 additions & 6 deletions runtime/Stdlib_Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -1397,13 +1397,13 @@ See [Array.prototype.entries](https://developer.mozilla.org/en-US/docs/Web/JavaS

```rescript
let array = [5, 6, 7]
let iterator : Iterator.t<int> = array->Array.entries
Console.log(iterator->Iterator.next) // {done: false, value: [0,5]}
Console.log(iterator->Iterator.next) // {done: false, value: [1,6]}
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<'a> = "entries"
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.
Expand All @@ -1415,8 +1415,8 @@ See [Array.prototype.values](https://developer.mozilla.org/en-US/docs/Web/JavaSc
```rescript
let array = [5, 6, 7]
let iterator : Iterator.t<int> = array->Array.values
Console.log(iterator->Iterator.next) // {done: false, value: 5}
Console.log(iterator->Iterator.next) // {done: false, value: 6}
iterator->Iterator.next->assertEqual({done: false, value: Some(5)})
iterator->Iterator.next->assertEqual({done: false, value: Some(6)})
```
*/
@send
Expand Down
47 changes: 23 additions & 24 deletions runtime/Stdlib_Iterator.resi
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ map->Map.set("someKey2", "someValue2")
// `Map.keys` returns all keys of the map as an iterator.
let mapKeysAsArray = map->Map.keys->Iterator.toArray

Console.log(mapKeysAsArray) // Logs ["someKey", "someKey2"] to the console.
mapKeysAsArray->assertEqual(["someKey", "someKey2"])
```
*/
@send
Expand All @@ -84,7 +84,7 @@ let mapKeysAsArray = map
->Map.keys
->Iterator.toArrayWithMapper(key => key->String.length)

Console.log(mapKeysAsArray) // Logs [7, 8] to the console.
mapKeysAsArray->assertEqual([7, 8])
```
*/
external toArrayWithMapper: (t<'a>, 'a => 'b) => array<'b> = "Array.from"
Expand All @@ -97,13 +97,12 @@ See [iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript
## Examples
```rescript
let iterator: Iterator.t<string> = ["a", "b", "c"]->Array.values
let acc = ref("")
iterator->Iterator.forEach(v => {
Console.log(v)
acc := acc.contents ++ v
})

// "a"
// "b"
// "c"
acc.contents->assertEqual("abc")
```
*/
@send
Expand All @@ -124,11 +123,11 @@ See [Iterator.prototype.drop](https://developer.mozilla.org/en-US/docs/Web/JavaS

## Examples
```rescript
let fibonacci: Iterator.t<int> = [ 1, 1, 2, 3, 5, 8, 13, 21 ]->Array.entries
let fibonacci: Iterator.t<int> = [ 1, 1, 2, 3, 5, 8, 13, 21 ]->Array.values

let seq = fibonacci->Iterator.drop(2)
Console.log(seq->Iterator.next) // {done: false, value: Some(2)}
Console.log(seq->Iterator.next) // {done: false, value: Some(3)}
seq->Iterator.next->assertEqual({done: false, value: Some(2)})
seq->Iterator.next->assertEqual({done: false, value: Some(3)})
```
*/
@send
Expand All @@ -141,10 +140,10 @@ See [Iterator.prototype.every](https://developer.mozilla.org/en-US/docs/Web/Java

## Examples
```rescript
let fibonacci: Iterator.t<int> = [ 1, 1, 2, 3, 5, 8, 13, 21 ]->Array.entries
let fibonacci: Iterator.t<int> = [ 1, 1, 2, 3, 5, 8, 13, 21 ]->Array.values

let areAllEven = fibonacci->Iterator.every(n => n % 2 == 0)
Console.log(areAllEven) // false
areAllEven->assertEqual(false)
```
*/
@send
Expand All @@ -157,11 +156,11 @@ See [Iterator.prototype.filter](https://developer.mozilla.org/en-US/docs/Web/Jav

## Examples
```rescript
let fibonacci: Iterator.t<int> = [ 1, 1, 2, 3, 5, 8, 13, 21 ]->Array.entries
let fibonacci: Iterator.t<int> = [ 1, 1, 2, 3, 5, 8, 13, 21 ]->Array.values

let seq = fibonacci->Iterator.filter(n => n % 2 == 0)
Console.log(seq->Iterator.next) // {done: false, value: Some(2)}
Console.log(seq->Iterator.next) // {done: false, value: Some(8)}
seq->Iterator.next->assertEqual({done: false, value: Some(2)})
seq->Iterator.next->assertEqual({done: false, value: Some(8)})
```
*/
@send
Expand All @@ -174,10 +173,10 @@ See [Iterator.prototype.find](https://developer.mozilla.org/en-US/docs/Web/JavaS

## Examples
```rescript
let fibonacci: Iterator.t<int> = [ 1, 1, 2, 3, 5, 8, 13, 21 ]->Array.entries
let fibonacci: Iterator.t<int> = [ 1, 1, 2, 3, 5, 8, 13, 21 ]->Array.values

let seq = fibonacci->Iterator.find(n => n % 2 == 0)
Console.log(seq) // Some(2)
seq->assertEqual(Some(2))
```
*/
@send
Expand All @@ -198,7 +197,7 @@ let letters =
->Array.values
->Iterator.flatMap(m => Map.keys(m))
->Array.fromIterator
Console.log(letters) // ["a", "b", "c", "d", "e", "f"]
letters->assertEqual(["a", "b", "c", "d", "e", "f"])
```
*/
@send
Expand All @@ -213,7 +212,7 @@ See [Iterator.prototype.map](https://developer.mozilla.org/en-US/docs/Web/JavaSc
```rescript
let map = Map.fromArray([("a", 1), ("b", 2), ("c", 3)])
let letters = map->Map.keys->Iterator.map(v => v->String.toUpperCase)->Array.fromIterator
Console.log(letters) // ["A", "B", "C"]
letters->assertEqual(["A", "B", "C"])
```
*/
@send
Expand All @@ -226,10 +225,10 @@ See [Iterator.prototype.reduce](https://developer.mozilla.org/en-US/docs/Web/Jav

## Examples
```rescript
let numbers: Iterator.t<int> = [ 1, 2, 3 ]->Array.entries
let numbers: Iterator.t<int> = [ 1, 2, 3 ]->Array.values

let sum = numbers->Iterator.reduce((acc, n) => acc + n, ~initialValue=0)
Console.log(sum) // 6
sum->assertEqual(6)
```
*/
@send
Expand All @@ -247,7 +246,7 @@ See [Iterator.prototype.some](https://developer.mozilla.org/en-US/docs/Web/JavaS
let numbers: Iterator.t<int> = [ 1, 2, 3 ]->Array.values

let hasEven = numbers->Iterator.some(n => n % 2 == 0)
Console.log(hasEven) // true
hasEven->assertEqual(true)
```
*/
@send
Expand All @@ -263,9 +262,9 @@ See [Iterator.prototype.take](https://developer.mozilla.org/en-US/docs/Web/JavaS
let fibonacci: Iterator.t<int> = [ 1, 1, 2, 3, 5, 8, 13, 21 ]->Array.values

let seq = fibonacci->Iterator.take(2)
Console.log(seq->Iterator.next) // {done: false, value: Some(1)}
Console.log(seq->Iterator.next) // {done: false, value: Some(1)}
Console.log(seq->Iterator.next) // {done: true, value: None}
seq->Iterator.next->assertEqual({done: false, value: Some(1)})
seq->Iterator.next->assertEqual({done: false, value: Some(1)})
seq->Iterator.next->assertEqual({done: true, value: None})
```
*/
@send
Expand Down
Loading