Skip to content

Commit f65a8c8

Browse files
✨ feat: Add len and _len.
Fixes #49.
1 parent 4ef0a2d commit f65a8c8

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
],
203203
"rules": {
204204
"camelcase": "off",
205+
"unicorn/prevent-abbreviations": "off",
205206
"unicorn/filename-case": "off"
206207
},
207208
"overrides": [

src/_len.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ export {default as Node} from './Node.js';
22
export {default as _append} from './_append.js';
33
export {default as _concat} from './_concat.js';
44
export {default as _iter} from './_iter.js';
5+
export {default as _len} from './_len.js';
56
export {default as _remove} from './_remove.js';
67
export {default as concat} from './concat.js';
78
export {default as empty} from './empty.js';
89
export {default as from} from './from.js';
910
export {default as iter} from './iter.js';
11+
export {default as len} from './len.js';
1012
export {default as pop} from './pop.js';
1113
export {default as push} from './push.js';
1214
export {default as shift} from './shift.js';

src/len.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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;

test/src/len.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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)));

0 commit comments

Comments
 (0)