Last active
March 16, 2017 19:14
-
-
Save zbraniecki/56279c9e14b6ab464224682c8a59a8f0 to your computer and use it in GitHub Desktop.
[JS] Iterable that can be replayed and caches resolved results
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 cachedIterable(iterable) { | |
const cache = []; | |
return { | |
[Symbol.iterator]() { | |
return { | |
ptr: 0, | |
next() { | |
if (cache.length <= this.ptr) { | |
cache.push(iterable.next()); | |
} | |
return cache[this.ptr++]; | |
} | |
}; | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment