Skip to content

Commit 5ccbf6a

Browse files
✨ feat: Implement values and _iter_fast.
Fixes #45 and fixes #46.
1 parent 81107e8 commit 5ccbf6a

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

src/_iter_fast.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import assert from 'assert';
2+
import Node from './Node.js';
3+
4+
/**
5+
* Generator of nodes in list in order. The list cannot be empty. You should
6+
* not modify the current node's next pointer unless you know what you are
7+
* doing.
8+
*
9+
* /!\ Modifying the next pointer of the current node will change which node
10+
* comes next in the iteration.
11+
*
12+
* @param {Node} first First node of the list.
13+
* @return {IterableIterator<Node>} Yields nodes of a list in order.
14+
*/
15+
export default function* _iter_fast(first) {
16+
assert(first instanceof Node);
17+
let next = first;
18+
19+
do {
20+
yield next;
21+
next = next.next;
22+
} while (next !== first);
23+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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 _iter_fast} from './_iter_fast.js';
56
export {default as _len} from './_len.js';
67
export {default as _remove} from './_remove.js';
78
export {default as concat} from './concat.js';
@@ -13,3 +14,4 @@ export {default as pop} from './pop.js';
1314
export {default as push} from './push.js';
1415
export {default as shift} from './shift.js';
1516
export {default as unshift} from './unshift.js';
17+
export {default as values} from './values.js';

src/values.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 _iter_fast from './_iter_fast.js';
3+
import Node from './Node.js';
4+
5+
/**
6+
* Generator of nodes in list in order.
7+
*
8+
* @param {Node} first First node of the list (can be null).
9+
* @return {IterableIterator<any>}
10+
*/
11+
export default function* values(first) {
12+
assert(first === null || first instanceof Node);
13+
if (first !== null) {
14+
for (const x of _iter_fast(first)) yield x.value;
15+
}
16+
}

test/src/api.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava';
22

3-
import {list, map} from '@aureooms/js-itertools';
3+
import {list} from '@aureooms/js-itertools';
44

55
import {
66
empty,
@@ -10,51 +10,51 @@ import {
1010
shift,
1111
unshift,
1212
concat,
13-
iter,
13+
values,
1414
} from '../../src/index.js';
1515

16-
const values = (first) => list(map((x) => x.value, iter(first)));
16+
const toArray = (first) => list(values(first));
1717

1818
test('API', (t) => {
1919
let A = empty();
2020

21-
t.deepEqual(values(A), []);
21+
t.deepEqual(toArray(A), []);
2222

2323
t.throws(() => pop(A), {message: /empty/});
2424
t.throws(() => shift(A), {message: /empty/});
2525

2626
A = push(A, 1);
2727

28-
t.deepEqual(values(A), [1]);
28+
t.deepEqual(toArray(A), [1]);
2929

3030
A = unshift(A, 2);
3131

32-
t.deepEqual(values(A), [2, 1]);
32+
t.deepEqual(toArray(A), [2, 1]);
3333

3434
let B = from([4, 3]);
3535

36-
t.deepEqual(values(B), [4, 3]);
36+
t.deepEqual(toArray(B), [4, 3]);
3737

3838
B = concat(A, B);
3939
A = empty();
4040

41-
t.deepEqual(values(B), [2, 1, 4, 3]);
42-
t.deepEqual(values(A), []);
41+
t.deepEqual(toArray(B), [2, 1, 4, 3]);
42+
t.deepEqual(toArray(A), []);
4343

4444
B = concat(A, B);
45-
t.deepEqual(values(B), [2, 1, 4, 3]);
45+
t.deepEqual(toArray(B), [2, 1, 4, 3]);
4646

4747
B = concat(B, A);
48-
t.deepEqual(values(B), [2, 1, 4, 3]);
48+
t.deepEqual(toArray(B), [2, 1, 4, 3]);
4949

5050
A = unshift(A, 8);
51-
t.deepEqual(values(A), [8]);
51+
t.deepEqual(toArray(A), [8]);
5252
B = push(B, 7);
53-
t.deepEqual(values(B), [2, 1, 4, 3, 7]);
53+
t.deepEqual(toArray(B), [2, 1, 4, 3, 7]);
5454

5555
A = concat(A, B);
5656
B = empty();
5757

58-
t.deepEqual(values(A), [8, 2, 1, 4, 3, 7]);
59-
t.deepEqual(values(B), []);
58+
t.deepEqual(toArray(A), [8, 2, 1, 4, 3, 7]);
59+
t.deepEqual(toArray(B), []);
6060
});

0 commit comments

Comments
 (0)