Skip to content

Commit 842c910

Browse files
authored
refactor(Typescript): decomp, pollard, cons (#139)
* decomp, transform * update imports * pollard, cons * decomp * clean up * while (iscons) -> tail, iterator, some * clean up code * clean up code
1 parent 7b66dbd commit 842c910

39 files changed

+302
-540
lines changed

inspector.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ class AtomFormatter {
4848
{ name: 'b', value: x.q.b.toString() },
4949
]);
5050
} else if (x.k == CONS) {
51-
const items = [];
52-
while (iscons(x)) {
53-
items.push(car(x));
54-
x = cdr(x);
55-
}
51+
const items = iscons(x) ? [...x] : [];
5652
return propertyList(items);
5753
} else if (x.k == TENSOR) {
5854
const elems = split_tensor(x, 0, 0)[1];

runtime/count.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { equal } from '../sources/misc';
22
import { car, cdr, iscons, istensor, Sym, U } from './defs';
3+
4+
const sum = (arr: number[]): number =>
5+
arr.reduce((a: number, b: number) => a + b, 0);
6+
37
export function count(p: U) {
48
let n: number;
59
if (iscons(p)) {
6-
n = 0;
7-
while (iscons(p)) {
8-
n += count(car(p)) + 1;
9-
p = cdr(p);
10-
}
10+
const items = [...p];
11+
n = sum(items.map(count)) + items.length;
1112
} else {
1213
n = 1;
1314
}
@@ -21,15 +22,11 @@ export function count(p: U) {
2122
export function countOccurrencesOfSymbol(needle: Sym, p: U) {
2223
let n = 0;
2324
if (iscons(p)) {
24-
while (iscons(p)) {
25-
n += countOccurrencesOfSymbol(needle, car(p));
26-
p = cdr(p);
27-
}
28-
} else {
29-
if (equal(needle, p)) {
30-
n = 1;
31-
}
25+
n = sum([...p].map((el) => countOccurrencesOfSymbol(needle, el)));
26+
} else if (equal(needle, p)) {
27+
n = 1;
3228
}
29+
3330
return n;
3431
}
3532

@@ -43,10 +40,8 @@ export function countsize(p: U) {
4340
n += count(p.tensor.elem[i]);
4441
}
4542
} else if (iscons(p)) {
46-
while (iscons(p)) {
47-
n += count(car(p)) + 1;
48-
p = cdr(p);
49-
}
43+
const items = [...p];
44+
n = sum(items.map(count)) + items.length;
5045
} else {
5146
n = 1;
5247
}

runtime/find.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { equal } from '../sources/misc';
33
import {
44
caddr,
55
cadr,
6-
car,
7-
cdr,
86
Constants,
97
E,
108
iscons,
@@ -29,11 +27,8 @@ export function Find(p: U, q: U): boolean {
2927
return false;
3028
}
3129

32-
while (iscons(p)) {
33-
if (Find(car(p), q)) {
34-
return true;
35-
}
36-
p = cdr(p);
30+
if (iscons(p)) {
31+
return [...p].some((p1: U) => Find(p1, q));
3732
}
3833

3934
return false;
@@ -67,11 +62,8 @@ export function findPossibleClockForm(p: U, p1: U): boolean {
6762
return false;
6863
}
6964

70-
while (iscons(p)) {
71-
if (findPossibleClockForm(car(p), p1)) {
72-
return true;
73-
}
74-
p = cdr(p);
65+
if (iscons(p)) {
66+
return [...p].some((el) => findPossibleClockForm(el, p1));
7567
}
7668

7769
return false;
@@ -92,11 +84,8 @@ export function findPossibleExponentialForm(p: U): boolean {
9284
return false;
9385
}
9486

95-
while (iscons(p)) {
96-
if (findPossibleExponentialForm(car(p))) {
97-
return true;
98-
}
99-
p = cdr(p);
87+
if (iscons(p)) {
88+
return [...p].some(findPossibleExponentialForm);
10089
}
10190

10291
return false;

runtime/otherCFunctions.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { stop } from './run';
2-
import { push } from './stack';
32
import { nativeInt } from '../sources/bignum';
43
import { isZeroAtomOrTensor } from '../sources/is';
54
import {
@@ -15,7 +14,7 @@ import {
1514
U,
1615
} from './defs';
1716
import { get_binding } from './symbol';
18-
import { list } from '../sources/list';
17+
import { makeList } from '../sources/list';
1918

2019
export function strcmp(str1: string, str2: string): Sign {
2120
if (str1 === str2) {
@@ -177,18 +176,16 @@ export function __range__(
177176
}
178177

179178
// Append one list to another.
180-
export function append(p1: U, p2: U) {
179+
export function append(p1: U, p2: U): U {
181180
// from https://github.com/gbl08ma/eigenmath/blob/8be989f00f2f6f37989bb7fd2e75a83f882fdc49/src/append.cpp
182-
const h = defs.tos;
183-
while (iscons(p1)) {
184-
push(car(p1));
185-
p1 = cdr(p1);
181+
const arr = [];
182+
if (iscons(p1)) {
183+
arr.push(...p1);
186184
}
187-
while (iscons(p2)) {
188-
push(car(p2));
189-
p2 = cdr(p2);
185+
if (iscons(p2)) {
186+
arr.push(...p2);
190187
}
191-
list(defs.tos - h);
188+
return makeList(...arr);
192189
}
193190

194191
export function jn(n: number, x: number): number {

sources/bake.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
ADD,
33
car,
44
Cons,
5-
defs,
65
doexpand,
76
FOR,
87
isadd,

sources/cons.ts

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

sources/cosh.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ export function Eval_cosh(p1: U) {
4141
}
4242

4343
export function ycosh(p1: U): U {
44-
return yycosh(p1);
45-
}
46-
47-
function yycosh(p1: U): U {
4844
if (car(p1) === symbol(ARCCOSH)) {
4945
return cadr(p1);
5046
}

0 commit comments

Comments
 (0)