-
Notifications
You must be signed in to change notification settings - Fork 0
/
adt_calculator.spec.es6.js
73 lines (68 loc) · 1.56 KB
/
adt_calculator.spec.es6.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
import chai from 'chai';
import { match } from '../lib/match-js';
chai.expect();
const expect = chai.expect;
describe('Matching on Algebraic Data Type: An Calculator', function () {
class BinOpExp {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
class Add extends BinOpExp {
unapply(exp) {
if (exp instanceof Add){
return [exp.x, exp.y];
}
return undefined;
}
}
class Min extends BinOpExp {
unapply(exp) {
if (exp instanceof Min){
return [exp.x, exp.y];
}
return undefined;
}
}
class Mul extends BinOpExp {
unapply(exp) {
if (exp instanceof Mul){
return [exp.x, exp.y];
}
return undefined;
}
}
class Div extends BinOpExp {
unapply(exp) {
if (exp instanceof Div){
return [exp.x, exp.y];
}
return undefined;
}
}
class Number {
unapply(e) {
if (typeof e === 'number'){
return e;
}
return undefined;
}
}
const evaluate = (exp) => match(exp)(
[Add, ([x, y])=> evaluate(x)+evaluate(y)],
[Min, ([x, y])=> evaluate(x)-evaluate(y)],
[Mul, ([x, y])=> evaluate(x)*evaluate(y)],
[Div, ([x, y])=> evaluate(x)/evaluate(y)],
[Number, (x) => x]
);
it('evaluate(5) should eval to 5', () => {
expect(evaluate(5)).to.be.equal(5);
});
it('/( *( +(3)(2) )(6) ) (2) should eval to 15', () => {
const exp = new Div(
new Mul( new Add(3, 2), 6),
2);
expect(evaluate(exp)).to.be.equal(15);
});
});