Skip to content

Commit 41c46e6

Browse files
✨ feat: Draft for sequence and count.
1 parent 9d89581 commit 41c46e6

File tree

6 files changed

+59
-3
lines changed

6 files changed

+59
-3
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@
7575
"bugs": {
7676
"url": "https://github.com/aureooms/js-set-partition/issues"
7777
},
78-
"dependencies": {},
78+
"dependencies": {
79+
"@aureooms/js-itertools": "^4.1.0"
80+
},
7981
"devDependencies": {
80-
"@aureooms/js-itertools": "^4.1.0",
8182
"@babel/cli": "7.11.6",
8283
"@babel/core": "7.11.6",
8384
"@babel/preset-env": "7.11.5",

src/count.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {nth} from '@aureooms/js-itertools';
2+
3+
import sequence from './sequence';
4+
5+
const count = (k) => nth(sequence(), k);
6+
export default count;

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import count from './count';
12
import isPartition from './isPartition';
3+
import sequence from './sequence';
24

35
/* eslint import/no-anonymous-default-export: [2, {"allowObject": true}] */
46
export default {
7+
count,
58
isPartition,
9+
sequence,
610
};
711

8-
export {isPartition};
12+
export {count, isPartition, sequence};

src/sequence.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default function* sequence() {
2+
yield 1n;
3+
const a = [1n];
4+
while (true) {
5+
const n = a.length;
6+
let current = a[n - 1];
7+
yield current;
8+
for (let i = 0; i < n; ++i) {
9+
const next = current + a[i];
10+
a[i] = current;
11+
current = next;
12+
}
13+
14+
a.push(current);
15+
}
16+
}

test/src/count.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import test from 'ava';
2+
3+
import {count} from '../../src';
4+
5+
const macro = (t, k, Bk) => {
6+
t.is(count(k), Bk);
7+
};
8+
9+
macro.title = (title, k, Bk) => `count(${k}) = ${Bk}`;
10+
11+
test(macro, 0, 1n);
12+
test(macro, 1, 1n);
13+
test(macro, 2, 2n);
14+
test(macro, 3, 5n);
15+
test(macro, 4, 15n);
16+
test(macro, 5, 52n);
17+
test(macro, 6, 203n);
18+
test(macro, 26, 49631246523618756274n);

test/src/sequence.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import test from 'ava';
2+
3+
import {list, take} from '@aureooms/js-itertools';
4+
5+
import {sequence} from '../../src';
6+
7+
test('sequence', (t) => {
8+
const n = 7;
9+
const expected = [1n, 1n, 2n, 5n, 15n, 52n, 203n];
10+
t.deepEqual(list(take(sequence(), n)), expected);
11+
});

0 commit comments

Comments
 (0)