-
Notifications
You must be signed in to change notification settings - Fork 3
/
closure.js
93 lines (86 loc) · 837 Bytes
/
closure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import test from 'ava';
import {closure, enumerate, list, takewhile} from '../../../src/index.js';
// https://oeis.org/A006577
const Collatz = (x) => (x % 2 === 1 ? 3 * x + 1 : Math.floor(x / 2));
const A006577 = [
0,
1,
7,
2,
5,
8,
16,
3,
19,
6,
14,
9,
9,
17,
17,
4,
12,
20,
20,
7,
7,
15,
15,
10,
23,
10,
111,
18,
18,
18,
106,
5,
26,
13,
13,
21,
21,
21,
34,
8,
109,
8,
29,
16,
16,
16,
104,
11,
24,
24,
24,
11,
11,
112,
112,
19,
32,
19,
32,
19,
19,
107,
107,
6,
27,
27,
27,
14,
14,
14,
102,
22,
];
function macro(t, n, a) {
const sequence = list(takewhile((x) => x !== 1, closure(Collatz, n)));
t.is(sequence.length, a);
}
macro.title = (_, n, a) => `closure [A006577] > a(${n}) = ${a}`;
for (const [n, a] of enumerate(A006577, 1)) {
test(macro, n, a);
}