Skip to content

Commit 47f203e

Browse files
committed
remove Array.fromIterator + friends in favor of specialized functions in Iterator
1 parent 7bbdff0 commit 47f203e

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

src/Core__Array.res

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ external setUnsafe: (array<'a>, int, 'a) => unit = "%array_unsafe_set"
1010
@val
1111
external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> = "Array.from"
1212

13-
@val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from"
14-
@val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from"
15-
1613
@send external fillAllInPlace: (array<'a>, 'a) => unit = "fill"
1714

1815
@send external fillInPlaceToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"

src/Core__Array.resi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
@val external fromArrayLike: Js.Array2.array_like<'a> => array<'a> = "Array.from"
44
@val
55
external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b> = "Array.from"
6-
@val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from"
7-
@val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from"
86

97
/**
108
`make(~length, init)`

src/Core__Iterator.res

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ type value<'a> = {
66
}
77

88
@send external next: t<'a> => value<'a> = "next"
9-
@scope("Array") external toArray: t<'a> => array<'a> = "from"
9+
external toArray: t<'a> => array<'a> = "Array.from"
10+
external toArrayWithMapper: (t<'a>, 'a => 'b) => array<'b> = "Array.from"

src/Core__Iterator.resi

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,26 @@ let mapKeysAsArray = map->Map.keys->Iterator.toArray
5555
Console.log(mapKeysAsArray) // Logs ["someKey", "someKey2"] to the console.
5656
```
5757
*/
58-
@scope("Array")
59-
external toArray: t<'a> => array<'a> = "from"
58+
external toArray: t<'a> => array<'a> = "Array.from"
59+
60+
/**
61+
Turns an iterator into an array of the remaining values, applying the provided mapper function on each item.
62+
Remember that each invocation of `next` of an iterator consumes a value. `Iterator.toArrayWithMapper` will consume all remaining values of the iterator and return them in an array to you.
63+
64+
See [iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) on MDN.
65+
66+
## Examples
67+
```rescript
68+
let map = Map.make()
69+
map->Map.set("someKey", "someValue")
70+
map->Map.set("someKey2", "someValue2")
71+
72+
// `Map.keys` returns all keys of the map as an iterator.
73+
let mapKeysAsArray = map
74+
->Map.keys
75+
->Iterator.toArrayWithMapper(key => key->String.length)
76+
77+
Console.log(mapKeysAsArray) // Logs [7, 8] to the console.
78+
```
79+
*/
80+
external toArrayWithMapper: (t<'a>, 'a => 'b) => array<'b> = "Array.from"

test/TempTests.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Console.info("Symbol")
171171
Console.info("---")
172172
let x = Symbol.getFor("Foo")
173173
Console.log(x)
174-
let array: array<string> = Array.fromIterator(String.getSymbolUnsafe("foo", Symbol.iterator)(.))
174+
let array: array<string> = String.getSymbolUnsafe("foo", Symbol.iterator)(.)->Iterator.toArray
175175
Console.log(array)
176176

177177
Console.info("")

0 commit comments

Comments
 (0)