Public
Edited
Dec 25, 2023
Fork of Queue
Importers
Insert cell
Insert cell
Queue = function Queue(...initialValues) {
const list = [];
if (initialValues) {
// todo is this going to rerun ever
list.push(...initialValues);
}

this.size = function() {
return list.length;
}

this.peek = function peek() { // returns optional value
return list.length ? list[0] : null;
}

this.push = function push(value) {
if (Array.isArray(value)) list.push(...value);
else list.push(value);
return value; // this.peek();
}

this.pop = function pop() { // returns optional value
if (list.length > 0)
return list.pop();
else
return null;
}

this.find = function find(value) { // returns optional value (Maybe observable.com need Typescript...)
const index = list.indexOf(value);
if (index === -1)
return null;
else
return index;
}
this.print = function print(logger=console.log) {
const output = `Queue() <[${list.toString()}]>`;
return output;
}
}
Insert cell
{ // Test 1
let q = new Queue();
q.push(1);
assert(q.pop() === 1);
assert(q.size() == 0);
}
Insert cell
x = new Queue(1, 2, 3);
Insert cell
x.print();
Insert cell
x.find(3);
Insert cell
x.peek();
Insert cell
x.push(2)
Insert cell
x.push([10,11])
Insert cell
x.print()
Insert cell
assert = function(b) {
if (!b) {
throw Error("Assertion error.")
}
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more