File tree Expand file tree Collapse file tree 5 files changed +60
-0
lines changed Expand file tree Collapse file tree 5 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 202202 ],
203203 "rules" : {
204204 "camelcase" : " off" ,
205+ "unicorn/prevent-abbreviations" : " off" ,
205206 "unicorn/filename-case" : " off"
206207 },
207208 "overrides" : [
Original file line number Diff line number Diff line change 1+ import assert from 'assert' ;
2+ import Node from './Node.js' ;
3+
4+ /**
5+ * Compute the length of a non-empty list.
6+ *
7+ * @param {Node } x First node of the input list.
8+ * @return {number } The length of the input list.
9+ */
10+ export default function _len ( x ) {
11+ assert ( x instanceof Node ) ;
12+ let n = 1 ;
13+ let y = x . next ;
14+ while ( y !== x ) {
15+ ++ n ;
16+ y = y . next ;
17+ }
18+
19+ return n ;
20+ }
Original file line number Diff line number Diff line change @@ -2,11 +2,13 @@ export {default as Node} from './Node.js';
22export { default as _append } from './_append.js' ;
33export { default as _concat } from './_concat.js' ;
44export { default as _iter } from './_iter.js' ;
5+ export { default as _len } from './_len.js' ;
56export { default as _remove } from './_remove.js' ;
67export { default as concat } from './concat.js' ;
78export { default as empty } from './empty.js' ;
89export { default as from } from './from.js' ;
910export { default as iter } from './iter.js' ;
11+ export { default as len } from './len.js' ;
1012export { default as pop } from './pop.js' ;
1113export { default as push } from './push.js' ;
1214export { default as shift } from './shift.js' ;
Original file line number Diff line number Diff line change 1+ import assert from 'assert' ;
2+ import _len from './_len.js' ;
3+ import Node from './Node.js' ;
4+
5+ /**
6+ * Compute the length of a list (can be empty).
7+ *
8+ * @param {Node } x First node of the input list (can be null).
9+ * @return {number } The length of the input list.
10+ */
11+ const len = ( x ) => {
12+ assert ( x === null || x instanceof Node ) ;
13+ return x === null ? 0 : _len ( x ) ;
14+ } ;
15+
16+ export default len ;
Original file line number Diff line number Diff line change 1+ import test from 'ava' ;
2+
3+ import { list , range } from '@aureooms/js-itertools' ;
4+ import { from , len } from '../../src/index.js' ;
5+
6+ const macro = ( t , input ) => {
7+ const x = from ( input ) ;
8+ const expected = input . length ;
9+ t . is ( len ( x ) , expected ) ;
10+ } ;
11+
12+ macro . title = ( title , input ) =>
13+ title || `len(from(${ JSON . stringify ( input ) } )) = ${ input . length } ` ;
14+
15+ test ( macro , [ ] ) ;
16+ test ( macro , '' ) ;
17+ test ( macro , [ { } ] ) ;
18+ test ( macro , 'z' ) ;
19+ test ( macro , [ 1 , 2 , 3 ] ) ;
20+ test ( macro , 'abracadabra' ) ;
21+ test ( 'len(from(list(range(987)))) = 987' , macro , list ( range ( 987 ) ) ) ;
You can’t perform that action at this time.
0 commit comments