Skip to content

Commit 6c68c5a

Browse files
authored
Merge pull request #50 from Kacarott/multiply
Multiply
2 parents e6983ab + 2b7ff99 commit 6c68c5a

File tree

4 files changed

+21
-32
lines changed

4 files changed

+21
-32
lines changed

src/lambda-calculus.js

+7-17
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Kacarott - https://github.com/Kacarott
1515
*/
1616

1717
// Default options
18-
const config = { verbosity: "Calm" // Calm | Concise | Loquacious | Verbose
18+
export const config = { verbosity: "Calm" // Calm | Concise | Loquacious | Verbose
1919
, purity: "Let" // Let | LetRec | PureLC
2020
, numEncoding: "Church" // None | Church | Scott | BinaryScott
2121
};
@@ -97,9 +97,9 @@ primitives.setThunk( "trace", new Tuple( Primitive( function(v) { console.info(S
9797

9898
const Y = new L("f",new A(new L("x",new A(new V("f"),new A(new V("x"),new V("x")))),new L("x",new A(new V("f"),new A(new V("x"),new V("x"))))));
9999

100-
function fromInt(n) { return fromIntWith()(n); }
100+
export function fromInt(n) { return fromIntWith()(n); }
101101

102-
function fromIntWith(cfg={}) {
102+
export function fromIntWith(cfg={}) {
103103
const {numEncoding,verbosity} = Object.assign( {}, config, cfg );
104104
return function fromInt(n) {
105105
if ( numEncoding === "Church" )
@@ -132,9 +132,9 @@ function fromIntWith(cfg={}) {
132132
} ;
133133
}
134134

135-
function toInt(term) { return toIntWith()(term); }
135+
export function toInt(term) { return toIntWith()(term); }
136136

137-
function toIntWith(cfg={}) {
137+
export function toIntWith(cfg={}) {
138138
const {numEncoding,verbosity} = Object.assign( {}, config, cfg );
139139
return function toInt(term) {
140140
try {
@@ -314,9 +314,9 @@ function parseWith(cfg={}) {
314314
}
315315
}
316316

317-
function compile(code) { return compileWith()(code); }
317+
export function compile(code) { return compileWith()(code); }
318318

319-
function compileWith(cfg={}) {
319+
export function compileWith(cfg={}) {
320320
const {numEncoding,purity,verbosity} = Object.assign( {}, config, cfg );
321321
return function compile(code) {
322322
if (typeof code !== "string" || !code) throw new TypeError("missing code");
@@ -426,13 +426,3 @@ function printStackTrace(error, term, stack) { console.log("printStackTrace",con
426426
}
427427

428428
Object.defineProperty( Function.prototype, "valueOf", { value: function valueOf() { return toInt(this); } } );
429-
430-
export {
431-
config,
432-
compile,
433-
compileWith,
434-
fromInt,
435-
fromIntWith,
436-
toInt,
437-
toIntWith,
438-
};

tests/multiply/initialSolution.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
multiply = \ m n . n (m s ) z

tests/multiply/solution.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
multiply = \ m n . n (m s ) z
1+
multiply = \ m n s . n ( m s )

tests/multiply/test.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,25 @@ chaiConfig.truncateThreshold = 0;
66
import * as LC from "../../src/lambda-calculus.js";
77
LC.config.purity = "LetRec";
88
LC.config.numEncoding = "Church";
9-
// LC.config.verbosity = "Concise"; // reinstate for production
9+
LC.config.verbosity = "Concise";
1010

1111
const solutionText = readFileSync(new URL("./solution.txt", import.meta.url), {encoding: "utf8"});
12+
const {multiply} = LC.compile(solutionText);
13+
const fromInt = LC.fromIntWith(LC.config);
14+
const toInt = LC.toIntWith(LC.config);
1215

13-
describe("Multiply",function(){
14-
// REVIEW Is this intentional? If so, it should be tested that `compile` throws
15-
// and remove other test cases.
16-
try {
17-
const {multiply} = LC.compile(solutionText);
18-
it("example tests",()=>{
19-
assert.equal( multiply(7)(7), 49 );
20-
assert.equal( multiply(11)(11), 121 );
16+
describe("Multiply",()=>{
17+
18+
it("example tests",function(){
19+
assert.equal( toInt(multiply(fromInt(7))(fromInt(7))), 49 );
20+
assert.equal( toInt(multiply(fromInt(11))(fromInt(11))), 121 );
2121
});
22-
it("random tests",()=>{
22+
23+
it("random tests",function(){
2324
const rnd = (m,n=0) => Math.random() * (n-m) + m | 0 ;
2425
for ( let i=1; i<=100; i++ ) {
2526
const m = rnd(i), n = rnd(i);
26-
assert.equal( multiply(m)(n), m*n );
27+
assert.equal( toInt(multiply(fromInt(m))(fromInt(n))), m*n );
2728
}
2829
});
29-
} catch (e) {
30-
console.error(e);
31-
}
3230
});

0 commit comments

Comments
 (0)