import { cacheIterator } from "https://deno.land/x/iterators@v0.2.0/cache.ts";
cacheIterator creates a wrapper around an iterator and caches its values
when next()
is called. The cache can be traversed with prev()
.
const it = [0, 1, 2][Symbol.iterator]()
const cached = cacheIterator(it)
cached.next() // { value: 0, done: false }
cached.next() // { value: 1, done: false }
cached.prev() // { value: 0, done: false, index: 0 }
If you call prev()
with nothing in the cache, it will throw.
const it = [0, 1, 2][Symbol.iterator]()
const cached = cacheIterator(it)
cached.prev() // Error: CachedIterator: no values cached, call next() at least once.