Skip to content

Instantly share code, notes, and snippets.

@variousauthors
Created November 25, 2013 12:27
Show Gist options
  • Select an option

  • Save variousauthors/7640545 to your computer and use it in GitHub Desktop.

Select an option

Save variousauthors/7640545 to your computer and use it in GitHub Desktop.
This is a stack implemented without assignment statements.
var Stack = function () {
return {
pop: function() {
throw "popped from an empty stack";
},
push: function(value) {
return (function (self) {
return _.extend({}, self._incrementCount(), {
head: value,
pop: function () {
return self;
}
});
})(this);
},
_incrementCount: function () {
return _.extend({}, this, { count: this._getCount() + 1 });
},
_getCount: function () {
if (this.count) {
return this.count;
} else {
return 0;
}
},
peek: function () {
return this.head;
}
};
};
var bob = new Stack;
var jane = bob.push(3).push(4).pop();
console.log(jane.peek());
console.log(jane.peek());
@variousauthors

Copy link
Copy Markdown
Author

Here is a fiddle to play with.

@lastobelus

Copy link
Copy Markdown

WHO IS JANE AND WHAT IS SHE DOING WITH BOB????????

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