Skip to content

Commit e6983ab

Browse files
authored
Merge pull request #49 from codewars/use-esm
Remove `fs` dependency and use ES modules
2 parents b08d213 + a129f56 commit e6983ab

File tree

15 files changed

+180
-174
lines changed

15 files changed

+180
-174
lines changed

kata/submit-tests.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const LC = require("lambda-calculus.js");
2-
const assert = require("assert");
3-
const chai = require("chai");
4-
chai.config.truncateThreshold = 0;
1+
import {readFileSync} from "fs";
2+
import {assert, config as chaiConfig} from "chai";
3+
chaiConfig.truncateThreshold = 0;
54

65
// Uses LC-Codewars for compilation
7-
// https://github.com/Kacarott/LC-Codewars
8-
6+
// https://github.com/codewars/lambda-calculus
7+
import * as LC from "@codewars/lambda-calculus";
98
LC.config.purity = "Let";
109
LC.config.numEncoding = "Church";
1110

12-
const solution = compile().example;
11+
const solutionText = readFileSync(new URL("./solution.txt", import.meta.url), {encoding: "utf8"});
12+
const solution = LC.compile(solutionText).example;
1313

1414
describe("Sample tests", function() {
1515
it("should return True", function() {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "@codewars/lambda-calculus",
3+
"type": "module",
34
"version": "1.0.0",
45
"description": "Lambda Calculus evaluator for Codewars",
56
"main": "src/lambda-calculus.js",
@@ -8,7 +9,7 @@
89
"test": "tests"
910
},
1011
"scripts": {
11-
"test": "mocha --reporter spec ./tests/run-tests.js",
12+
"test": "mocha --reporter spec ./tests/*/test.js",
1213
"repl": "node ./src/LC-REPL.js"
1314
},
1415
"repository": {

src/LC-REPL.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Simple custom REPL for testing LC code.
55
Written by Kacarott
66
*/
77

8-
const repl = require("repl");
9-
const LC = require("./lambda-calculus.js");
8+
import repl from "repl";
9+
import * as LC from "./lambda-calculus.js";
1010

1111
// REPL defaults
1212
LC.config.purity = "LetRec";

src/lambda-calculus.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ JohanWiltink - https://github.com/JohanWiltink
1414
Kacarott - https://github.com/Kacarott
1515
*/
1616

17-
const fs = require("fs");
18-
1917
// Default options
2018
const config = { verbosity: "Calm" // Calm | Concise | Loquacious | Verbose
2119
, purity: "Let" // Let | LetRec | PureLC
@@ -320,7 +318,8 @@ function compile(code) { return compileWith()(code); }
320318

321319
function compileWith(cfg={}) {
322320
const {numEncoding,purity,verbosity} = Object.assign( {}, config, cfg );
323-
return function compile(code=fs.readFileSync("./solution.txt", "utf8")) {
321+
return function compile(code) {
322+
if (typeof code !== "string" || !code) throw new TypeError("missing code");
324323
const env = parseWith({numEncoding,purity,verbosity})(code);
325324
const r = {};
326325
for ( const [name] of env )
@@ -428,10 +427,12 @@ function printStackTrace(error, term, stack) { console.log("printStackTrace",con
428427

429428
Object.defineProperty( Function.prototype, "valueOf", { value: function valueOf() { return toInt(this); } } );
430429

431-
exports.config = config;
432-
exports.compile = compile;
433-
exports.compileWith = compileWith;
434-
exports.fromInt = fromInt;
435-
exports.fromIntWith = fromIntWith;
436-
exports.toInt = toInt;
437-
exports.toIntWith = toIntWith;
430+
export {
431+
config,
432+
compile,
433+
compileWith,
434+
fromInt,
435+
fromIntWith,
436+
toInt,
437+
toIntWith,
438+
};

tests/basics-binary-scott/test.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
const chai = require("chai");
2-
const {assert} = chai;
1+
import {readFileSync} from "fs";
2+
import {assert} from "chai";
33

4-
const LC = require("../../src/lambda-calculus.js");
4+
import * as LC from "../../src/lambda-calculus.js";
55
LC.config.purity = "LetRec";
66
LC.config.numEncoding = "BinaryScott";
77

8-
const solution = LC.compile();
8+
const solutionText = readFileSync(new URL("./solution.txt", import.meta.url), {encoding: "utf8"});
9+
const solution = LC.compile(solutionText);
910
const fromInt = LC.fromIntWith(LC.config);
1011
const toInt = LC.toIntWith(LC.config);
1112

@@ -255,4 +256,4 @@ describe("Binary Scott tests",function(){
255256
assert.strictEqual( even (fromInt(n)) (false)(true), ! (n&1) );
256257
}
257258
});
258-
});
259+
});

tests/basics-church/test.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
const chai = require("chai");
2-
const {assert} = chai;
1+
import {readFileSync} from "fs";
2+
import {assert} from "chai";
33

4-
const LC = require("../../src/lambda-calculus.js");
4+
import * as LC from "../../src/lambda-calculus.js";
55
LC.config.purity = "LetRec";
66
LC.config.numEncoding = "Church";
77

8-
const solution = LC.compile();
8+
const solutionText = readFileSync(new URL("./solution.txt", import.meta.url), {encoding: "utf8"});
9+
const solution = LC.compile(solutionText);
910
const fromInt = LC.fromIntWith(LC.config);
1011
const toInt = LC.toIntWith(LC.config);
1112

@@ -39,4 +40,4 @@ describe("Church tests",function(){
3940
assert.strictEqual( toInt(pow(fromInt(10))(fromInt(3))), 1e3 );
4041
assert.strictEqual( toInt(pred(pow(fromInt(10))(fromInt(3)))), 1e3-1 );
4142
});
42-
});
43+
});

tests/counter/test.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
const chai = require("chai");
2-
const {assert} = chai;
3-
chai.config.truncateThreshold = 0;
1+
import {readFileSync} from "fs";
2+
import {assert, config as chaiConfig} from "chai";
3+
chaiConfig.truncateThreshold = 0;
44

5-
const LC = require("../../src/lambda-calculus.js");
5+
import * as LC from "../../src/lambda-calculus.js";
66
LC.config.purity = "Let";
77
LC.config.numEncoding = "Church";
8-
9-
const {counter} = LC.compile();
8+
const solutionText = readFileSync(new URL("./solution.txt", import.meta.url), {encoding: "utf8"});
9+
const {counter} = LC.compile(solutionText);
1010

1111
const toInt = LC.toIntWith(LC.config);
1212

1313
const T = t => f => t ;
1414
const F = t => f => f ;
1515

16-
it("fixed tests", function() {
17-
assert.deepEqual( toInt( counter(T)(T)(T)(F) ), 3 );
18-
assert.deepEqual( toInt( counter(T)(F) ), 1 );
19-
assert.deepEqual( toInt( counter(T)(T)(T)(T)(T)(T)(T)(F) ), 7 );
16+
describe("counter", () => {
17+
it("fixed tests", function() {
18+
assert.deepEqual( toInt( counter(T)(T)(T)(F) ), 3 );
19+
assert.deepEqual( toInt( counter(T)(F) ), 1 );
20+
assert.deepEqual( toInt( counter(T)(T)(T)(T)(T)(T)(T)(F) ), 7 );
21+
});
2022
});

tests/delta-generators/test.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
const {assert} = require("chai");
1+
import {readFileSync} from "fs";
2+
import {assert} from "chai";
23

3-
const LC = require("../../src/lambda-calculus.js");
4+
import * as LC from "../../src/lambda-calculus.js";
45
LC.config.purity = "LetRec";
56
LC.config.numEncoding = "Scott";
67

7-
const {delta} = LC.compile();
8+
const solutionText = readFileSync(new URL("./solution.txt", import.meta.url), {encoding: "utf8"});
9+
const {delta} = LC.compile(solutionText);
810

911
const {fin} = LC.compile(String.raw`
1012
nil = \ _ _ x . x
@@ -33,7 +35,9 @@ function toArr(a, n) { // lists use double pair encoding, not Scott!
3335
return res;
3436
}
3537

36-
it("fixed tests", function() {
37-
assert.deepEqual( toArr( delta (fromInt(2)) (fin), 2 ), [1, 1] );
38-
assert.deepEqual( toArr( delta (fromInt(1)) (inf), 10 ), [1,1,1,1,1,1,1,1,1,1] );
38+
describe("delta-generators", () => {
39+
it("fixed tests", function() {
40+
assert.deepEqual( toArr( delta (fromInt(2)) (fin), 2 ), [1, 1] );
41+
assert.deepEqual( toArr( delta (fromInt(1)) (inf), 10 ), [1,1,1,1,1,1,1,1,1,1] );
42+
});
3943
});

tests/hello-world/test.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
const chai = require("chai");
2-
const {assert} = chai;
3-
chai.config.truncateThreshold = 0;
1+
import {readFileSync} from "fs";
2+
import {assert, config as chaiConfig} from "chai";
3+
chaiConfig.truncateThreshold = 0;
44

5-
const LC = require("../../src/lambda-calculus.js");
5+
import * as LC from "../../src/lambda-calculus.js";
66
LC.config.purity = "Let";
77
LC.config.numEncoding = "Church";
88

9-
const {hello} = LC.compile();
9+
const solutionText = readFileSync(new URL("./solution.txt", import.meta.url), {encoding: "utf8"});
10+
const {hello} = LC.compile(solutionText);
1011

1112
const toInt = LC.toIntWith(LC.config);
1213

@@ -20,6 +21,8 @@ const tail = xs => xs (Snd) (Snd) ;
2021
// double pair encoding for list
2122
const toString = xs => isNil (xs) ? "" : String.fromCharCode(toInt(head(xs))) + toString(tail(xs)) ;
2223

23-
it("fixed test", function() {
24-
assert.equal( toString(hello), "Hello, world!" );
24+
describe("hello-world", () => {
25+
it("fixed test", function() {
26+
assert.equal( toString(hello), "Hello, world!" );
27+
});
2528
});

tests/is-prime-scott/test.js

+30-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
const chai = require("chai");
2-
chai.config.truncateThreshold = 0;
3-
const {assert} = chai;
1+
import {readFileSync} from "fs";
2+
import {assert, config as chaiConfig} from "chai";
3+
chaiConfig.truncateThreshold = 0;
44

5-
const LC = require("../../src/lambda-calculus.js");
5+
import * as LC from "../../src/lambda-calculus.js";
66
LC.config.purity = "LetRec";
77
LC.config.numEncoding = "Scott";
88

9-
const {isPrime} = LC.compile();
9+
const solutionText = readFileSync(new URL("./solution.txt", import.meta.url), {encoding: "utf8"});
10+
const {isPrime} = LC.compile(solutionText);
1011
const fromInt = LC.fromIntWith(LC.config);
1112

12-
it("fixed tests", function() {
13-
this.timeout(12e3);
14-
assert.equal( isPrime(fromInt( 0)) (true)(false), false );
15-
assert.equal( isPrime(fromInt( 1)) (true)(false), false );
16-
assert.equal( isPrime(fromInt( 2)) (true)(false), true );
17-
assert.equal( isPrime(fromInt( 3)) (true)(false), true );
18-
assert.equal( isPrime(fromInt( 4)) (true)(false), false );
19-
assert.equal( isPrime(fromInt( 5)) (true)(false), true );
20-
assert.equal( isPrime(fromInt( 6)) (true)(false), false );
21-
assert.equal( isPrime(fromInt( 7)) (true)(false), true );
22-
assert.equal( isPrime(fromInt( 8)) (true)(false), false );
23-
assert.equal( isPrime(fromInt( 9)) (true)(false), false );
24-
assert.equal( isPrime(fromInt(10)) (true)(false), false );
25-
assert.equal( isPrime(fromInt(11)) (true)(false), true );
26-
assert.equal( isPrime(fromInt(12)) (true)(false), false );
27-
assert.equal( isPrime(fromInt(13)) (true)(false), true );
28-
assert.equal( isPrime(fromInt(14)) (true)(false), false );
29-
assert.equal( isPrime(fromInt(15)) (true)(false), false );
30-
assert.equal( isPrime(fromInt(16)) (true)(false), false );
31-
assert.equal( isPrime(fromInt(17)) (true)(false), true );
32-
assert.equal( isPrime(fromInt(18)) (true)(false), false );
33-
assert.equal( isPrime(fromInt(19)) (true)(false), true );
13+
describe("is-prime-scott", () => {
14+
it("fixed tests", function() {
15+
this.timeout(12e3);
16+
assert.equal( isPrime(fromInt( 0)) (true)(false), false );
17+
assert.equal( isPrime(fromInt( 1)) (true)(false), false );
18+
assert.equal( isPrime(fromInt( 2)) (true)(false), true );
19+
assert.equal( isPrime(fromInt( 3)) (true)(false), true );
20+
assert.equal( isPrime(fromInt( 4)) (true)(false), false );
21+
assert.equal( isPrime(fromInt( 5)) (true)(false), true );
22+
assert.equal( isPrime(fromInt( 6)) (true)(false), false );
23+
assert.equal( isPrime(fromInt( 7)) (true)(false), true );
24+
assert.equal( isPrime(fromInt( 8)) (true)(false), false );
25+
assert.equal( isPrime(fromInt( 9)) (true)(false), false );
26+
assert.equal( isPrime(fromInt(10)) (true)(false), false );
27+
assert.equal( isPrime(fromInt(11)) (true)(false), true );
28+
assert.equal( isPrime(fromInt(12)) (true)(false), false );
29+
assert.equal( isPrime(fromInt(13)) (true)(false), true );
30+
assert.equal( isPrime(fromInt(14)) (true)(false), false );
31+
assert.equal( isPrime(fromInt(15)) (true)(false), false );
32+
assert.equal( isPrime(fromInt(16)) (true)(false), false );
33+
assert.equal( isPrime(fromInt(17)) (true)(false), true );
34+
assert.equal( isPrime(fromInt(18)) (true)(false), false );
35+
assert.equal( isPrime(fromInt(19)) (true)(false), true );
36+
});
3437
});

tests/is-prime/test.js

+30-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
const chai = require("chai");
2-
const {assert} = chai;
3-
chai.config.truncateThreshold = 0;
1+
import {readFileSync} from "fs";
2+
import {assert, config as chaiConfig} from "chai";
3+
chaiConfig.truncateThreshold = 0;
44

5-
const LC = require("../../src/lambda-calculus.js");
5+
import * as LC from "../../src/lambda-calculus.js";
66
LC.config.purity = "LetRec";
77
LC.config.numEncoding = "Church";
88

9-
const {isPrime} = LC.compile();
9+
const solutionText = readFileSync(new URL("./solution.txt", import.meta.url), {encoding: "utf8"});
10+
const {isPrime} = LC.compile(solutionText);
1011
const fromInt = LC.fromIntWith(LC.config);
1112

12-
it("fixed tests", function() {
13-
this.timeout(12e3);
14-
assert.equal( isPrime(fromInt(0)) (true)(false), false );
15-
assert.equal( isPrime(fromInt(1)) (true)(false), false );
16-
assert.equal( isPrime(fromInt(2)) (true)(false), true );
17-
assert.equal( isPrime(fromInt(3)) (true)(false), true );
18-
assert.equal( isPrime(fromInt(4)) (true)(false), false );
19-
assert.equal( isPrime(fromInt(5)) (true)(false), true );
20-
assert.equal( isPrime(fromInt(6)) (true)(false), false );
21-
assert.equal( isPrime(fromInt(7)) (true)(false), true );
22-
assert.equal( isPrime(fromInt(8)) (true)(false), false );
23-
assert.equal( isPrime(fromInt(9)) (true)(false), false );
24-
assert.equal( isPrime(fromInt(10)) (true)(false), false );
25-
assert.equal( isPrime(fromInt(11)) (true)(false), true );
26-
assert.equal( isPrime(fromInt(12)) (true)(false), false );
27-
assert.equal( isPrime(fromInt(13)) (true)(false), true );
28-
assert.equal( isPrime(fromInt(14)) (true)(false), false );
29-
assert.equal( isPrime(fromInt(15)) (true)(false), false );
30-
assert.equal( isPrime(fromInt(16)) (true)(false), false );
31-
assert.equal( isPrime(fromInt(17)) (true)(false), true );
32-
assert.equal( isPrime(fromInt(18)) (true)(false), false );
33-
assert.equal( isPrime(fromInt(19)) (true)(false), true );
13+
describe("is-prime", () => {
14+
it("fixed tests", function() {
15+
this.timeout(12e3);
16+
assert.equal( isPrime(fromInt(0)) (true)(false), false );
17+
assert.equal( isPrime(fromInt(1)) (true)(false), false );
18+
assert.equal( isPrime(fromInt(2)) (true)(false), true );
19+
assert.equal( isPrime(fromInt(3)) (true)(false), true );
20+
assert.equal( isPrime(fromInt(4)) (true)(false), false );
21+
assert.equal( isPrime(fromInt(5)) (true)(false), true );
22+
assert.equal( isPrime(fromInt(6)) (true)(false), false );
23+
assert.equal( isPrime(fromInt(7)) (true)(false), true );
24+
assert.equal( isPrime(fromInt(8)) (true)(false), false );
25+
assert.equal( isPrime(fromInt(9)) (true)(false), false );
26+
assert.equal( isPrime(fromInt(10)) (true)(false), false );
27+
assert.equal( isPrime(fromInt(11)) (true)(false), true );
28+
assert.equal( isPrime(fromInt(12)) (true)(false), false );
29+
assert.equal( isPrime(fromInt(13)) (true)(false), true );
30+
assert.equal( isPrime(fromInt(14)) (true)(false), false );
31+
assert.equal( isPrime(fromInt(15)) (true)(false), false );
32+
assert.equal( isPrime(fromInt(16)) (true)(false), false );
33+
assert.equal( isPrime(fromInt(17)) (true)(false), true );
34+
assert.equal( isPrime(fromInt(18)) (true)(false), false );
35+
assert.equal( isPrime(fromInt(19)) (true)(false), true );
36+
});
3437
});

0 commit comments

Comments
 (0)