Iterator.tee
Description:
MDN:
There is no way to "fork" an iterator to allow it to be iterated multiple times.
Of course there is.
If Python itertools can do it, so can JavaScript.
Define a function that takes an iterator and optionally a number ( defaulting to 2
), and returns an array of $NUMBER
forked iterators.
All returned iterators should iterate the original iterator independently. They should also be iterable.
Values produced by generators as iterators and as iterables
iterator.next
returns an object of the form { value, done }
. Note that generators may, once, produce a value
other than undefined
when done
first is true
if they return
a value instead of yield
ing it. This behaviour should be replicated, and will be tested.
An iterable does not produce this value, though it is generated and then cannot be produced by next
later ( the generator is closed by then ).
Passing an argument to next
iterator.next
accepts an argument. There is, however, no consistent way to handle arguments passed to forked iterators: the original iterator may already have been called, either without or with a different argument. Expected behaviour is unspecified, and there will be no testing with arguments passed to next
.
Errors thrown by iterators
Iterators can throw
an Error
. Expected behaviour in this case is unspecified, and it will not be tested.
Generators have a throw
method ( for iterators, this is optional ). There will be no calling of throw
.
Test constraints
- input iterator may be infinite
- up to
10 000
output iterators - up to
10 000
values per output iterator
Example
it = function*() { yield* [1,2,3,4] } ()
[xs,ys] = tee(it)
xs.next() -> { value: 1, done: false }
xs.next() -> { value: 2, done: false }
xs.next() -> { value: 3, done: false }
ys.next() -> { value: 1, done: false }
ys.next() -> { value: 2, done: false }
xs.next() -> { value: 4, done: false }
xs.next() -> { value: undefined, done: true }
Array.from(ys) -> [3,4]
Similar Kata:
Stats:
Created | Apr 14, 2024 |
Published | Apr 14, 2024 |
Warriors Trained | 231 |
Total Skips | 23 |
Total Code Submissions | 278 |
Total Times Completed | 61 |
JavaScript Completions | 57 |
TypeScript Completions | 8 |
Total Stars | 15 |
% of votes with a positive feedback rating | 98% of 23 |
Total "Very Satisfied" Votes | 22 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 13 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |