Skip to content

Commit b0f5a87

Browse files
♻️ refactor: Simplify API.
1 parent 1062c0b commit b0f5a87

File tree

7 files changed

+64
-34
lines changed

7 files changed

+64
-34
lines changed

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@ See [docs](https://string-data-structure.github.io/fibonacci-string/index.html).
1212
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
1313
1414
```js
15-
import {build} from '@string-data-structure/fibonacci-string';
16-
17-
const f = build({iadd: (x, y) => x+y, zero: () => 1, one: () => 1});
18-
19-
f(0); // 1
20-
f(1); // 0
21-
f(2); // 1
22-
f(3); // 1
23-
f(4); // 0
24-
f(5); // 1
25-
f(6); // 0
26-
f(7); // 1
27-
f(8); // 1
28-
f(9); // 0
29-
30-
const fn = build({iadd: (x, y) => x+y, zero: () => 1n, one: () => 1n});
31-
f(4802349082340928340983n); // 0
15+
import {makeIndex, query} from '@string-data-structure/fibonacci-string';
16+
17+
const F = makeIndex({iadd: (x, y) => x+y, zero: () => 1, one: () => 2}, 10);
18+
19+
query(F, 0); // 0
20+
query(F, 1); // 1
21+
query(F, 2); // 0
22+
query(F, 3); // 0
23+
query(F, 4); // 1
24+
query(F, 5); // 0
25+
query(F, 6); // 1
26+
query(F, 7); // 0
27+
query(F, 8); // 0
28+
query(F, 9); // 1
29+
query(F, 10); // 0
30+
31+
const Fn = makeIndex({iadd: (x, y) => x+y, zero: () => 1n, one: () => 2n}, 10n**100n);
32+
query(Fn, 4802349082340928340983n); // 1n
33+
query(Fn, 123456748023490823409283409834802349082340928340983n); // 0n
3234
```
3335

3436
[![License](https://img.shields.io/github/license/string-data-structure/fibonacci-string.svg)](https://raw.githubusercontent.com/string-data-structure/fibonacci-string/main/LICENSE)

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export {default as build} from './build.js';
1+
export {default as makeIndex} from './makeIndex.js';
22
export {default as gen} from './gen.js';
33
export {default as query} from './query.js';

src/build.js renamed to src/makeIndex.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import {le} from '@functional-abstraction/predicate';
44
import gen from './gen.js';
55

66
/**
7-
* Build.
7+
* Make index up to at least a given query position.
88
*
99
* @param {Object}
1010
* @param {number|bigint} n
1111
* @return {number[]|bigint[]}
1212
*/
13-
const build = ({zero, one, iadd}, n) => {
13+
const makeIndex = ({zero, one, iadd}, n) => {
1414
const g = gen(iadd, zero(), one());
1515
return Array.from(takewhile(le(n), g));
1616
};
1717

18-
export default build;
18+
export default makeIndex;

src/query.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,28 @@
11
import assert from 'node:assert';
2-
import build from './build.js';
32

43
/**
54
* Query ϕ[i].
65
*
7-
* @param {Object} type
8-
* @param {number[]|bigint[]} array
6+
* @param {number[]|bigint[]} F
97
* @param {number|bigint} i
108
* @return {number|bigint}
119
*/
12-
const query = (type, array, i) => {
10+
const query = (F, i) => {
1311
assert(typeof i === 'bigint' || (Number.isInteger(i) && i >= 0));
1412
assert(typeof i !== 'bigint' || i >= 0n);
15-
assert(array || type);
16-
const F = array ?? build(type, i);
1713
let n_1 = F.length - 1;
1814
const zero = n_1 === -1 ? i : F[0] - F[0];
19-
assert(array || i === zero || i >= F[n_1]);
2015
// eslint-disable-next-line no-constant-condition
2116
while (true) {
2217
assert(i >= zero);
23-
assert(array || n_1 + 1 === F.length);
2418
if (n_1 <= 0) return i;
2519
assert(n_1 + 1 >= 2);
2620
assert(i < F[n_1] + F[n_1 - 1]); // Equivalent to i < F[n] = F[n_1 + 1]
2721
if (i >= F[n_1]) {
2822
i -= F[n_1];
2923
--n_1;
30-
if (F !== array) F.pop();
3124
}
3225

33-
if (F !== array) F.pop();
3426
--n_1;
3527
}
3628
};

test/src/_fixtures.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const gs = function* ({a, b}, n) {
3939

4040
const F = Array.from(take(gen(iadd, a.length, a.length + b.length), n - 1));
4141
const Fn = F.pop();
42-
for (const i of range(Fn)) yield query(undefined, F, i);
42+
for (const i of range(Fn)) yield query(F, i);
4343
};
4444

4545
export const makeSymbol = ({a, b}) => {

test/src/examples.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import test from 'ava';
2+
3+
import {makeIndex, query} from '../../src/index.js';
4+
5+
test('README.md (Number)', (t) => {
6+
const F = makeIndex({iadd: (x, y) => x + y, zero: () => 1, one: () => 2}, 10);
7+
8+
t.is(query(F, 0), 0);
9+
t.is(query(F, 1), 1);
10+
t.is(query(F, 2), 0);
11+
t.is(query(F, 3), 0);
12+
t.is(query(F, 4), 1);
13+
t.is(query(F, 5), 0);
14+
t.is(query(F, 6), 1);
15+
t.is(query(F, 7), 0);
16+
t.is(query(F, 8), 0);
17+
t.is(query(F, 9), 1);
18+
t.is(query(F, 10), 0);
19+
});
20+
21+
test('README.md (BigInt)', (t) => {
22+
const Fn = makeIndex(
23+
{iadd: (x, y) => x + y, zero: () => 1n, one: () => 2n},
24+
10n ** 100n,
25+
);
26+
27+
t.is(query(Fn, 4_802_349_082_340_928_340_983n), 1n);
28+
t.is(
29+
query(
30+
Fn,
31+
123_456_748_023_490_823_409_283_409_834_802_349_082_340_928_340_983n,
32+
),
33+
0n,
34+
);
35+
});

test/src/query.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import test from 'ava';
22

33
import {enumerate} from '@iterable-iterator/zip';
44

5-
import {query} from '../../src/index.js';
5+
import {query, makeIndex} from '../../src/index.js';
66

77
import {makeSymbol, type, typen, repr} from './_fixtures.js';
88

99
const symbol = makeSymbol({a: 'a', b: 'b'});
1010

1111
const macro = (t, type, i, expected) => {
12-
t.is(symbol(query(type, undefined, i)), expected);
12+
const F = makeIndex(type, i);
13+
t.is(symbol(query(F, i)), expected);
1314
};
1415

1516
macro.title = (title, _, i, expected) =>

0 commit comments

Comments
 (0)