Skip to content

Instantly share code, notes, and snippets.

@jiggzson
Created November 8, 2014 21:05
Show Gist options
  • Save jiggzson/348e7b1712f97678554e to your computer and use it in GitHub Desktop.
Save jiggzson/348e7b1712f97678554e to your computer and use it in GitHub Desktop.
This add-on function logs step using nerdamer
nerdamer.register({
parent: 'Helper',
name: 'withsteps',
visible: false,
build: function() {
var core = this;
var operationLog = [];
//write a function which would perform the log
var log = function(operator, symbol1, symbol2, result, is_first) {
if(!is_first) operationLog.pop();
operationLog.push([symbol1, operator, symbol2, result]);
};
var operations = [
['add', '+'],
['subtract', '-'],
['divide', '/'],
['multiply', '*'],
['pow', '^']
];
//use the set function to modify each operator function. Notice the use of a function
//to do this. This ensures each has its own scope
var set = function(o, s) {
core.PARSER.extend(o, function(symbol1, symbol2, f) {
var s1 = symbol1.text(), s2 = symbol2.text();
//keep track of whether this function is still in a recursive loop
core.PARSER.is_first = true;
var result = f.call(this, symbol1, symbol2);
log(s, s1, s2, result.text(), core.PARSER.is_first);
//recursive loop is done
core.PARSER.is_first = false;
return result;//we got what we need so return the result
}, true);
};
for(var i=0; i<operations.length; i++) {
set(operations[i][0], operations[i][1]);
}
return function(expr_string) {
operationLog = []; //clear the log
var eq = nerdamer(expr_string);
return [eq, operationLog];
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment