Created
November 4, 2020 17:42
-
-
Save esco/ba6b289c81ed5541607f6107dc35d272 to your computer and use it in GitHub Desktop.
SequencePool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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