Skip to content

Instantly share code, notes, and snippets.

@nubunto
Last active December 29, 2015 03:29
Show Gist options
  • Select an option

  • Save nubunto/7607922 to your computer and use it in GitHub Desktop.

Select an option

Save nubunto/7607922 to your computer and use it in GitHub Desktop.
Priority Queue implemented in JavaScript. It is a closure, that returns an object, with push, shift, and length. It's first argument is a function that sorts the queue as you add elements to it. It allows successive method calls.
var PriorityQueue = function (fn) {
var queue = [];
return {
push: function (a) {
queue.push(a);
queue.sort(fn);
return this;
},
shift: function () {
return queue.shift();
},
length: function () {
return queue.length;
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment