Skip to content

Commit

Permalink
use deque in tail and trunc implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Jul 27, 2016
1 parent f81f9d5 commit 9716cbc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
23 changes: 3 additions & 20 deletions src/base/tail.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { deque } from 'aureooms-js-collections' ;

import { iter } from './iter' ;
import { drop } from './drop' ;

Expand All @@ -21,25 +23,6 @@ export function* tail ( iterable , n ) {
return ;
}

const iterator = iter( iterable ) ;

// use real deque here
const buffer = [ ] ;

while ( n --> 0 ) {
const e = iterator.next( ) ;
if ( e.done ) {
yield* buffer ;
return ;
}
buffer.push( e.value ) ;
}

for ( const value of iterator ) {
buffer.push( value ) ;
buffer.shift() ;
}

yield* buffer ;
yield* deque( iterable , n ) ;

}
16 changes: 11 additions & 5 deletions src/base/trunc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { deque } from 'aureooms-js-collections' ;

import { iter } from './iter' ;
import { take } from './take' ;

Expand All @@ -21,20 +23,24 @@ export function* trunc ( iterable , n ) {
return ;
}

if ( n === 0 ) {
yield* iterable ;
return ;
}

const iterator = iter( iterable ) ;

// use real deque here
const buffer = [ ] ;
const buffer = deque( null , n ) ;

while ( n --> 0 ) {
const e = iterator.next( ) ;
if ( e.done ) return ;
buffer.push( e.value ) ;
buffer.append( e.value ) ;
}

for ( const value of iterator ) {
buffer.push( value ) ;
yield buffer.shift() ;
yield buffer.popleft() ;
buffer.append( value ) ;
}

}
2 changes: 1 addition & 1 deletion src/map/permutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function* permutations ( iterable , r ) {

if ( cycles[i] === 0 ) {

// use deque
// could be costly
indices.push( indices.splice( i , 1 )[0] ) ;

cycles[i] = len - i ;
Expand Down

0 comments on commit 9716cbc

Please sign in to comment.