Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const abstractOperation = operation => (...operands) => (...values) => {
  2.     let result = [];
  3.     operands.map(currentOperand => result.push(currentOperand(...values)));
  4.     return operation(...result);
  5. };
  6.  
  7. const add = abstractOperation((a, b) => a + b);
  8.  
  9. const subtract = abstractOperation((a, b) => a - b);
  10.  
  11. const multiply = abstractOperation((a, b) => a * b);
  12.  
  13. const divide = abstractOperation((a, b) => a / b);
  14.  
  15. const negate = abstractOperation(a => -a);
  16.  
  17. const med3 = abstractOperation((...operands) => {
  18.     operands.sort(function (a, b) {
  19.         return a - b;
  20.     });
  21.     let len = operands.length;
  22.     return operands[(len - (len % 2)) / 2];
  23. });
  24.  
  25. const avg5 = abstractOperation((...operands) => {
  26.     let sum = operands.slice(0, 5).reduce((sum, current) => sum + current);
  27.     return sum / operands.length;
  28. });
  29.  
  30. const abs = abstractOperation(operand => Math.abs(operand));
  31.  
  32. const iff = abstractOperation((a, b, c) => (a >= 0 ? b : c));
  33.  
  34. const cnst = value => () => value;
  35.  
  36. const variable = name => (...values) => values[variables.indexOf(name)];
  37.  
  38. let variables = ['x', 'y', 'z'];
  39.  
  40. variables.forEach(function (currentVariable) {
  41.     this[currentVariable] = variable(currentVariable);
  42. });
  43.  
  44. let constants = {
  45.     'one': 1,
  46.     'two': 2,
  47.     'pi': Math.PI,
  48.     'e': Math.E
  49. };
  50.  
  51. Object.keys(constants).forEach(function (key) {
  52.     this[key] = cnst(constants[key]);
  53. });
  54.  
  55. function isDigit(symbol) {
  56.     return symbol >= '0' && symbol <= '9';
  57. }
  58.  
  59. const parse = expression => (...values) => {
  60.     let tokens = expression.split(/\s+/);
  61.  
  62.     let stack = [];
  63.  
  64.     let operations = {
  65.         '+': [add, 2],
  66.         '-': [subtract, 2],
  67.         '*': [multiply, 2],
  68.         '/': [divide, 2],
  69.         'negate': [negate, 1],
  70.         'med3': [med3, 3],
  71.         'avg5': [avg5, 5],
  72.         'abs': [abs, 1],
  73.         'iff': [iff, 3]
  74.     };
  75.     tokens.map(function (token) {
  76.         if (token in operations) {
  77.             let args = [];
  78.             let len = stack.length;
  79.             stack.slice(len - operations[token][1], len).forEach(function () {
  80.                 args.push(stack.pop());
  81.             });
  82.             args.reverse();
  83.             stack.push(operations[token][0].apply(this, args));
  84.         } else if (isDigit(token[0]) || (token[0] === '-' && token.length !== 1)) {
  85.             stack.push(cnst(parseInt(token)));
  86.         } else if (variables.indexOf(token) !== -1 || token in constants) {
  87.             stack.push(this[token]);
  88.         }
  89.     });
  90.     return stack.pop()(...values);
  91. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement