Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- const VARIABLES = {"x": 0, "y": 1, "z": 2};
- function newOperation(action, symbol) {
- function Operation(...args) {
- this.operands = args;
- }
- Operation.prototype.toString = function () {
- return this.operands.join(" ") + " " + symbol;
- };
- Operation.prototype.evaluate = function (...args) {
- return action(...this.operands.map(operand => (operand.evaluate(...args))));
- };
- return Operation;
- }
- function Const(x) {
- this.value = x;
- }
- Const.prototype.toString = function () {
- return this.value.toString();
- };
- Const.prototype.evaluate = function () {
- return this.value;
- };
- function Variable(s) {
- this.name = s;
- this.index = VARIABLES[s];
- }
- Variable.prototype.toString = function () {
- return this.name;
- };
- Variable.prototype.evaluate = function (...args) {
- return args[this.index];
- };
- const Add = newOperation((a, b) => a + b, "+");
- const Subtract = newOperation((a, b) => a - b, "-");
- const Multiply = newOperation((a, b) => a * b, "*");
- const Divide = newOperation((a, b) => a / b, "/");
- const Negate = newOperation(a => -a, "negate");
- const Min3 = newOperation((a, b, c) => Math.min(a, b, c), "min3");
- const Max5 = newOperation((a, b, c, d, e) => Math.max(a, b, c, d, e), "max5");
Advertisement
Add Comment
Please, Sign In to add comment