Open
Description
According to MDN, an iterator is any object that implements a next
method that returns an IteratorResult
(Iterator.value<'a>
in RescriptCore
). An iterable is any object that implements the [Symbol.iterator]
method that returns an iterator.
Right now Iterator.toArray
is defined as an extern using Array.from
:
However, Array.from
accepts an iterable, not an iterator. This means that if we implement a valid iterator (not an iterable, but an object with next
), Iterator.next
and Iterator.forEach
will work just fine but Iterator.toArray
will not: it returns an empty array.
To fix this, Iterator.toArray
should be defined something like:
let toArray = (iterator: t<'a>): array<'a> => {
let results = []
iterator->forEach(value => {
switch value {
| Some(value) => Array.push(results, value)
| None => _
}
})
results
}
To use Array.from
, a separate module for iterables should be defined and used instead.
Metadata
Metadata
Assignees
Labels
No labels