Skip to content

Commit 1253554

Browse files
committed
Lazily consume the first N values
1 parent e5bed6a commit 1253554

File tree

1 file changed

+11
-10
lines changed
  • lib/node_modules/@stdlib/iter/slice/lib

1 file changed

+11
-10
lines changed

lib/node_modules/@stdlib/iter/slice/lib/main.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,17 @@ function iterSlice( iterator, begin, end ) {
9191
M = END;
9292
}
9393
n = M - N;
94+
i = 0;
9495

9596
// Create an iterator protocol-compliant object:
9697
iter = {};
9798
setReadOnly( iter, 'next', next );
9899
setReadOnly( iter, 'return', finish );
99100

100-
// If an environment supports `Symbol.iterator`, make the iterator iterable:
101+
// If an environment supports `Symbol.iterator` and a provided iterator is iterable, make the iterator iterable:
101102
if ( iteratorSymbol && isFunction( iterator[ iteratorSymbol ] ) ) {
102103
setReadOnly( iter, iteratorSymbol, factory );
103104
}
104-
105-
// Consume the first `N` values from the provided iterator...
106-
if ( n > 0 ) {
107-
for ( i = 0; i < N; i++ ) {
108-
FLG = iterator.next().done;
109-
}
110-
}
111-
i = 0;
112105
return iter;
113106

114107
/**
@@ -120,8 +113,16 @@ function iterSlice( iterator, begin, end ) {
120113
function next() {
121114
var out;
122115
var v;
116+
123117
i += 1;
124-
if ( FLG || i > n ) {
118+
if ( n > 0 && i < N ) {
119+
// Lazily consume the first `N` values from the provided iterator...
120+
for ( i = 0; i < N; i++ ) {
121+
FLG = iterator.next().done;
122+
}
123+
}
124+
n -= 1;
125+
if ( FLG || n < 0 ) {
125126
return {
126127
'done': true
127128
};

0 commit comments

Comments
 (0)