SHARE
TWEET

Untitled

a guest Mar 26th, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. const cnst = (x) => () => x;
  4.  
  5. const pi = cnst(Math.PI);
  6. const e = cnst(Math.E);
  7.  
  8. const variable = (name) => (x, y, z) => {
  9.     if (name === "x") return x;
  10.     if (name === "y") return y;
  11.     if (name === "z") return z;
  12. }
  13.  
  14. const add = (a, b) => (...args) => a(...args) + b(...args);
  15.  
  16. const subtract = (a, b) => (...args) => a(...args) - b(...args);
  17.  
  18. const multiply = (a, b) => (...args) => a(...args) * b(...args);
  19.  
  20. const divide = (a, b) => (...args) => a(...args) / b(...args);
  21.  
  22. const negate = (a) => (...args) => (-1) * a(...args);
  23.  
  24. const avg5 = (a, b, c, d, e) => (...args) => {
  25.     return (a(...args) + b(...args) + c(...args) + d(...args) + e(...args)) / 5;
  26. }
  27.  
  28. const med3 = (a, b, c) => (...args) => {
  29.     let arr = [a(...args), b(...args), c(...args)];
  30.     arr = arr.sort((a, b) => a - b);
  31.     return arr[1];
  32. }
  33.  
  34. const  operations = [ 'negate', '+', '-', '*', '/', 'med3', 'avg5' ];
  35.  
  36. const operationToArity = {
  37.     'negate': 1,
  38.     '+' : 2,
  39.     '-' : 2,
  40.     '*' : 2,
  41.     '/' : 2,
  42.     'med3' : 3,
  43.     'avg5' : 5
  44. };
  45.  
  46. const operationToFunction = {
  47.     'negate': negate,
  48.     '+' : add,
  49.     '-' : subtract,
  50.     '*' : multiply,
  51.     '/' : divide,
  52.     'med3' : med3,
  53.     'avg5' : avg5
  54. };
  55.  
  56. const parse = function(expression) {
  57.     let stack = [];
  58.     let tokens = expression.split(' ').filter((word) => word !== '');
  59.     for (let token of tokens) {
  60.         let arity = token in operationToArity ? operationToArity[token] : 0;
  61.         let args = [];
  62.         for (let i = 0; i < arity; i++) {
  63.             args.push(stack.pop());
  64.         }
  65.         args = args.reverse();
  66.         stack.push(makeOperation(token, ...args));
  67.     }
  68.     return stack.pop();
  69. }
  70.  
  71. const makeOperation = function (token, ...args) {
  72.     switch (token) {
  73.         case 'negate': return negate(...args);
  74.         case "+": return add(...args);
  75.         case "-": return subtract(...args);
  76.         case "*": return multiply(...args);
  77.         case "/": return divide(...args);
  78.         case 'med3': return med3(...args);
  79.         case 'avg5': return avg5(...args);
  80.         case "x": return variable("x");
  81.         case "y": return variable("y");
  82.         case "z": return variable("z");
  83.         case 'pi': return pi;
  84.         case 'e': return e;
  85.     }
  86.     return cnst(Number.parseInt(token));
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top