Skip to content

Instantly share code, notes, and snippets.

@esco
Created November 4, 2020 17:42
Show Gist options
  • Save esco/ba6b289c81ed5541607f6107dc35d272 to your computer and use it in GitHub Desktop.
Save esco/ba6b289c81ed5541607f6107dc35d272 to your computer and use it in GitHub Desktop.
SequencePool
function SequencePool(start=0, _next=(num)=>num+1) {
let pool = [start]
function next() {
let item = pool.pop()
if (!pool.length) {
pool.push(_next(item))
}
return item
}
function recycle(item) {
pool.push(item)
}
return {next, recycle}
}
autoIncrement = SequencePool(1)
evenPool = SequencePool(0, (num)=>num+2)
oddPool = SequencePool(1, (num)=>num+2)
negPool = SequencePool(0, (num)=>num-1)
powerOf2Pool = SequencePool(1, (num)=>num * 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment