Skip to content

Instantly share code, notes, and snippets.

@variousauthors
Created November 25, 2013 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save variousauthors/7640545 to your computer and use it in GitHub Desktop.
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
Author

Here is a fiddle to play with.

@lastobelus
Copy link

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