{"action":"created","issue":{"url":"https://api.github.com/repos/josdejong/mathjs/issues/939","repository_url":"https://api.github.com/repos/josdejong/mathjs","labels_url":"https://api.github.com/repos/josdejong/mathjs/issues/939/labels{/name}","comments_url":"https://api.github.com/repos/josdejong/mathjs/issues/939/comments","events_url":"https://api.github.com/repos/josdejong/mathjs/issues/939/events","html_url":"https://github.com/josdejong/mathjs/issues/939","id":257440239,"number":939,"title":"Transforming node tree starting with deepest node first?","user":{"login":"pklavins","id":31420672,"avatar_url":"https://avatars2.githubusercontent.com/u/31420672?v=4","gravatar_id":"","url":"https://api.github.com/users/pklavins","html_url":"https://github.com/pklavins","followers_url":"https://api.github.com/users/pklavins/followers","following_url":"https://api.github.com/users/pklavins/following{/other_user}","gists_url":"https://api.github.com/users/pklavins/gists{/gist_id}","starred_url":"https://api.github.com/users/pklavins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pklavins/subscriptions","organizations_url":"https://api.github.com/users/pklavins/orgs","repos_url":"https://api.github.com/users/pklavins/repos","events_url":"https://api.github.com/users/pklavins/events{/privacy}","received_events_url":"https://api.github.com/users/pklavins/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-09-13T16:17:41Z","updated_at":"2017-09-13T20:21:48Z","closed_at":null,"author_association":"NONE","body":"I have a node tree from doing `test = math.parse(\"sqrt(75 / 3) + det([[-1, 2], [3, 1]]) - sin(pi / 4)^2\")`. It's a rather large node tree with lots of functions and operators. What I want to do is to find the deepest function/operator node and evaluate it, replacing it with a constant node containing the result of the evaluation. However, the `transform()` function works the other way; it finds the 'shallowest' node first (in this case the - operator between the det and sin), evaluates that, and then nothing is left to evaluate.\r\n\r\n```\r\ntest2 = test.transform(node => {\r\n    if (node.isOperatorNode || node.isFunctionNode){\r\n        console.log(node); \r\n        return new math.expression.node.ConstantNode(node.eval()); \r\n    }\r\n})\r\n```\r\n\r\nI know I can find the ops/functions depth-first by doing a `filter()` and reversing that array, but the problem there is that I find the nodes 'out of context' of the whole function, so going in and evaluating that array of found nodes doesn't work either. Is there a way to modify `transform` to work depth-first?"},"comment":{"url":"https://api.github.com/repos/josdejong/mathjs/issues/comments/329285622","html_url":"https://github.com/josdejong/mathjs/issues/939#issuecomment-329285622","issue_url":"https://api.github.com/repos/josdejong/mathjs/issues/939","id":329285622,"user":{"login":"josdejong","id":568626,"avatar_url":"https://avatars1.githubusercontent.com/u/568626?v=4","gravatar_id":"","url":"https://api.github.com/users/josdejong","html_url":"https://github.com/josdejong","followers_url":"https://api.github.com/users/josdejong/followers","following_url":"https://api.github.com/users/josdejong/following{/other_user}","gists_url":"https://api.github.com/users/josdejong/gists{/gist_id}","starred_url":"https://api.github.com/users/josdejong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josdejong/subscriptions","organizations_url":"https://api.github.com/users/josdejong/orgs","repos_url":"https://api.github.com/users/josdejong/repos","events_url":"https://api.github.com/users/josdejong/events{/privacy}","received_events_url":"https://api.github.com/users/josdejong/received_events","type":"User","site_admin":false},"created_at":"2017-09-13T20:21:48Z","updated_at":"2017-09-13T20:21:48Z","author_association":"OWNER","body":"You could implement such a variant your self, that should go something like this (not tested):\r\n\r\n```js\r\nfunction transformDepthFirst(node, callback) {\r\n  // first process childs (depth first)\r\n  var nodeWithUpdatedChilds = node.map(child => transformDepthFirst(child, callback));\r\n\r\n  // then process the node itself\r\n  return callback(nodeWithUpdatedChilds);\r\n}\r\n```\r\n\r\nyou can make it a one liner if you want ;)"}}
{"actor":{"display_login":"josdejong"}}