Skip to content

Instantly share code, notes, and snippets.

@jonniek
Created August 29, 2018 12:33
Show Gist options
  • Save jonniek/6ba271eb005e429a9dd4b499e94bf307 to your computer and use it in GitHub Desktop.
Save jonniek/6ba271eb005e429a9dd4b499e94bf307 to your computer and use it in GitHub Desktop.
function iterableStack() {
let arr = []
return {
push: (data) => arr.push(data),
pop: () => arr.pop(),
[Symbol.iterator]: function* () {
let index = 0
while (arr[index]) {
yield arr[index]
index++
}
},
}
}
const stack = iterableStack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
console.log("pushed to stack values: 1, 2, 3, 4, 5")
console.log("popped from stack value:", stack.pop())
console.log("iterating stack:")
for (let item of stack) {
console.log(" " + item)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment