Skip to content

Instantly share code, notes, and snippets.

@kaw2k
Last active August 11, 2016 12:10
Show Gist options
  • Save kaw2k/e61b0a8f05b1b1781b8c to your computer and use it in GitHub Desktop.
Save kaw2k/e61b0a8f05b1b1781b8c to your computer and use it in GitHub Desktop.
function treeNode(value, left, right) {
return {
value: value,
left: left,
right: right,
map: function(transformation) {
var nextValue = transformation(nextValue);
var nextLeft;
if (left) nextLeft = left.map(transformation);
var nextRight;
if (right) nextRight = right.map(transformation);
return treeNode(nextValue, nextLeft, nextRight);
}
};
}
@chriskrycho
Copy link

You've got a typo: line 7 should read:

var nextValue = transformation(value);

As it currently stands, it's calling transformation on a value which doesn't yet exist. (It only runs without complaint because of the use of var to bind the variable name.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment