Skip to content

Instantly share code, notes, and snippets.

@duncanmak
Created June 5, 2010 17:06
Show Gist options
  • Select an option

  • Save duncanmak/426786 to your computer and use it in GitHub Desktop.

Select an option

Save duncanmak/426786 to your computer and use it in GitHub Desktop.
var doTree = function (tree, func) {
func (tree.value);
for (var child in tree.children) { doTree (child); }
};
var makeNode = function (i) { return { value: i, children: [] };};
var numTree = { value: 42, children: [makeNode (1), makeNode (2)]};
var sumTree = function (i) { if (this.sum == undefined) this.sum = 0; else this.sum += i; };
doTree (numTree, sumTree);
print (sumTree.sum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment