Skip to content

Commit 2674f49

Browse files
♻️ refactor: Give better name.
1 parent b5fa01d commit 2674f49

File tree

5 files changed

+40
-34
lines changed

5 files changed

+40
-34
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ See [docs](https://set-theory.github.io/closure/index.html).
1010
1111

1212
```js
13-
import {closure} from '@set-theory/closure';
14-
closure( x => x + 1 , 0 ) ; // 0 1 2 3 ...
13+
import {singletonUnderUnaryOperator} from '@set-theory/closure';
14+
singletonUnderUnaryOperator( x => x + 1 , 0 ) ; // 0 1 2 3 ...
1515
```
1616

1717
[![License](https://img.shields.io/github/license/set-theory/closure.svg)](https://raw.githubusercontent.com/set-theory/closure/main/LICENSE)

src/closure.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export {default as closure} from './closure.js';
1+
export {default as singletonUnderUnaryOperator} from './singletonUnderUnaryOperator.js';

src/singletonUnderUnaryOperator.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Computes the closure of a unary operator, starting from a single element.
3+
*
4+
* @experimental Should handle multiple d-ary operators and an arbitrary
5+
* cardinality countable set in the future. An actual closure function would
6+
* keep track of encountered elements to stop when all elements have been
7+
* found. We can have multiple low-level function that enumerate candidates,
8+
* and a helper function that constructs a new set out of a combination of
9+
* those. Note that the arity of a JavaScript function can be retrieved through
10+
* the `length` property.
11+
*
12+
* @example
13+
* singletonUnderUnaryOperator( x => x + 1 , 0 ) ; // 0 1 2 3 ...
14+
*
15+
* @example
16+
* singletonUnderUnaryOperator( x => x * 2 , 1 ) ; // 1 2 4 8 ...
17+
*
18+
* @param {Function} operator The operator.
19+
* @param {Object} start The starting element.
20+
* @returns {Iterator} Iterator over the closure.
21+
*/
22+
export default function* singletonUnderUnaryOperator(operator, start) {
23+
yield start;
24+
25+
let element = start;
26+
27+
while (true) {
28+
element = operator(element);
29+
30+
yield element;
31+
}
32+
}

test/src/closure.js renamed to test/src/singletonUnderUnaryOperator.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {list} from '@iterable-iterator/list';
44
import {enumerate} from '@iterable-iterator/zip';
55
import {takewhile} from '@iterable-iterator/slice';
66

7-
import {closure} from '../../src/index.js';
7+
import {singletonUnderUnaryOperator} from '../../src/index.js';
88

99
// https://oeis.org/A006577
1010
const Collatz = (x) => (x % 2 === 1 ? 3 * x + 1 : Math.floor(x / 2));
@@ -85,12 +85,14 @@ const A006577 = [
8585
];
8686

8787
function macro(t, n, a) {
88-
const sequence = list(takewhile((x) => x !== 1, closure(Collatz, n)));
88+
const sequence = list(
89+
takewhile((x) => x !== 1, singletonUnderUnaryOperator(Collatz, n)),
90+
);
8991

9092
t.is(sequence.length, a);
9193
}
9294

93-
macro.title = (_, n, a) => `closure [A006577] > a(${n}) = ${a}`;
95+
macro.title = (_, n, a) => `[A006577] > a(${n}) = ${a}`;
9496

9597
for (const [n, a] of enumerate(A006577, 1)) {
9698
test(macro, n, a);

0 commit comments

Comments
 (0)