Created
June 22, 2017 00:36
-
-
Save marcmartino/6f6dc56ca184dff18953ed02669c9447 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var funcNames = { | |
| addition: { | |
| func: function (paramsArr = []) { | |
| return paramsArr.reduce((tot, num) => tot + num, 0) | |
| }, | |
| textRep: '+' | |
| }, | |
| subtraction: { | |
| func: function ([head, ...tail] = []) { | |
| return tail.reduce((tot, num) => tot - num, head) | |
| }, | |
| textRep: '-' | |
| }, | |
| multiplication: { | |
| func: function (paramsArr = []) { | |
| return paramsArr.reduce((tot, num) => tot * num, 1) | |
| }, | |
| textRep: '*' | |
| }, | |
| division: { | |
| func: function ([head, ...tail] = []) { | |
| return tail.reduce((tot, num) => tot / num, head) | |
| }, | |
| textRep: '/' | |
| }, | |
| mean: { | |
| func: function (paramsArr = []) { | |
| return paramsArr.reduce((tot, num) => tot + num, 0) / paramsArr.length | |
| }, | |
| textRep: 'μ' | |
| }, | |
| min: { | |
| func: function (paramsArr = []) { | |
| return Math.min.apply(undefined, paramsArr) | |
| }, | |
| textRep: 'min' | |
| } | |
| } | |
| function expressionEval (exprObj, varsObj) { | |
| if (exprObj.type === 'expression') { | |
| return funcNames[exprObj.functionName].func( | |
| exprObj.value.map((childObj) => expressionEval(childObj, varsObj))) | |
| } | |
| return exprObj.type === 'variable' ? varsObj[exprObj.value] : exprObj.value | |
| } | |
| export {funcNames, expressionEval} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| id: 0, | |
| type: 'expression', | |
| functionName: 'mean', | |
| value: [ | |
| {id: 1, type: 'variable', value: 'ram'}, | |
| {id: 2, type: 'number', value: 6969}, | |
| {id: 3, | |
| type: 'expression', | |
| functionName: 'subtraction', | |
| value: [{id: 4, type: 'number', value: 3}, {id: 5, type: 'variable', value: 'screenSize'}] | |
| }, | |
| {id: 6, | |
| type: 'expression', | |
| functionName: 'multiplication', | |
| value: [{id: 7, type: 'number', value: 105}, {id: 8, type: 'variable', value: 'hardDrive'}] | |
| } | |
| ] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| ram: 10, | |
| screenSize: 11, | |
| hardDrive: 11 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment