File tree Expand file tree Collapse file tree 5 files changed +38
-6
lines changed Expand file tree Collapse file tree 5 files changed +38
-6
lines changed Original file line number Diff line number Diff line change 1+ import assert from 'assert' ;
2+ import Node from './Node.js' ;
3+ import _remove from './_remove.js' ;
4+
5+ /**
6+ * Removes last {@link Node} from a non-empty list.
7+ *
8+ * @param {Node } x First node (not null).
9+ * @return {Node } New list (possibly null).
10+ */
11+ export default function _pop ( x ) {
12+ assert ( x instanceof Node ) ;
13+ if ( x === x . prev ) return null ;
14+ _remove ( x . prev ) ;
15+ return x ;
16+ }
Original file line number Diff line number Diff line change 1+ import assert from 'assert' ;
2+ import Node from './Node.js' ;
3+ import _remove from './_remove.js' ;
4+
5+ /**
6+ * Removes first {@link Node} from a non-empty list.
7+ *
8+ * @param {Node } x First node (not null).
9+ * @return {Node } New list (possibly null).
10+ */
11+ export default function _shift ( x ) {
12+ assert ( x instanceof Node ) ;
13+ if ( x === x . next ) return null ;
14+ _remove ( x ) ;
15+ return x . next ;
16+ }
Original file line number Diff line number Diff line change @@ -4,7 +4,9 @@ export {default as _concat} from './_concat.js';
44export { default as _iter } from './_iter.js' ;
55export { default as _iter_fast } from './_iter_fast.js' ;
66export { default as _len } from './_len.js' ;
7+ export { default as _pop } from './_pop.js' ;
78export { default as _remove } from './_remove.js' ;
9+ export { default as _shift } from './_shift.js' ;
810export { default as concat } from './concat.js' ;
911export { default as empty } from './empty.js' ;
1012export { default as from } from './from.js' ;
Original file line number Diff line number Diff line change 11import assert from 'assert' ;
22import Node from './Node.js' ;
3- import _remove from './_remove .js' ;
3+ import _pop from './_pop .js' ;
44
55/**
66 * Removes last {@link Node} from a list. Throws if input list is empty.
@@ -12,6 +12,5 @@ export default function pop(x) {
1212 if ( x === null ) throw new Error ( 'input list is empty' ) ;
1313 assert ( x instanceof Node ) ;
1414 const node = x . prev ;
15- _remove ( node ) ;
16- return [ x === node ? null : x , node ] ;
15+ return [ _pop ( x ) , node ] ;
1716}
Original file line number Diff line number Diff line change 11import assert from 'assert' ;
22import Node from './Node.js' ;
3- import _remove from './_remove .js' ;
3+ import _shift from './_shift .js' ;
44
55/**
66 * Removes first {@link Node} from a list. Throws if input list is empty.
@@ -11,6 +11,5 @@ import _remove from './_remove.js';
1111export default function shift ( x ) {
1212 if ( x === null ) throw new Error ( 'input list is empty' ) ;
1313 assert ( x instanceof Node ) ;
14- _remove ( x ) ;
15- return [ x === x . next ? null : x . next , x ] ;
14+ return [ _shift ( x ) , x ] ;
1615}
You can’t perform that action at this time.
0 commit comments