File tree Expand file tree Collapse file tree 3 files changed +19
-7
lines changed Expand file tree Collapse file tree 3 files changed +19
-7
lines changed Original file line number Diff line number Diff line change @@ -19,8 +19,8 @@ export function* frame ( iterable , n ) {
19
19
20
20
// Could have an implementation using a deque
21
21
// that doesn't slice (and thus allocate a new
22
- // vector everytime). Though the yieded object
23
- // cannot be modified by the caller in this case.
22
+ // vector everytime). Though the yield object
23
+ // could not be modified by the caller in that case.
24
24
25
25
const iterator = iter ( iterable ) ;
26
26
Original file line number Diff line number Diff line change @@ -14,9 +14,12 @@ import { drop } from './drop' ;
14
14
* @param {Number } n - The number of elements to include in the output.
15
15
* @returns {Array } - The last <code>n</code> elements of the input iterable.
16
16
*/
17
- export function tail ( iterable , n ) {
17
+ export function * tail ( iterable , n ) {
18
18
19
- if ( n < 0 ) return drop ( iterable , - n ) ;
19
+ if ( n < 0 ) {
20
+ yield * drop ( iterable , - n ) ;
21
+ return ;
22
+ }
20
23
21
24
const iterator = iter ( iterable ) ;
22
25
@@ -25,7 +28,10 @@ export function tail ( iterable , n ) {
25
28
26
29
while ( n -- > 0 ) {
27
30
const e = iterator . next ( ) ;
28
- if ( e . done ) return buffer ;
31
+ if ( e . done ) {
32
+ yield * buffer ;
33
+ return ;
34
+ }
29
35
buffer . push ( e . value ) ;
30
36
}
31
37
@@ -34,6 +40,6 @@ export function tail ( iterable , n ) {
34
40
buffer . shift ( ) ;
35
41
}
36
42
37
- return buffer ;
43
+ yield * buffer ;
38
44
39
45
}
Original file line number Diff line number Diff line change 1
1
import test from 'ava' ;
2
2
3
- import { range , tail , list } from '../../..' ;
3
+ import { range , tail , list , count } from '../../..' ;
4
4
5
5
test ( "tail" , t => {
6
6
@@ -11,3 +11,9 @@ test( "tail" , t => {
11
11
t . deepEqual ( list ( tail ( range ( 100 ) , 1000 ) ) , list ( range ( 100 ) ) ) ;
12
12
13
13
} ) ;
14
+
15
+ test ( "construct tail of infinite generator" , t => {
16
+
17
+ tail ( count ( ) , 10 ) ;
18
+
19
+ } ) ;
You can’t perform that action at this time.
0 commit comments