Skip to content

Instantly share code, notes, and snippets.

@marcmartino
Created June 22, 2017 00:36
Show Gist options
  • Select an option

  • Save marcmartino/6f6dc56ca184dff18953ed02669c9447 to your computer and use it in GitHub Desktop.

Select an option

Save marcmartino/6f6dc56ca184dff18953ed02669c9447 to your computer and use it in GitHub Desktop.
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}
{
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'}]
}
]
}
{
ram: 10,
screenSize: 11,
hardDrive: 11
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment