Skip to main content
Module

x/iterators/cache.ts>cacheIterator

Iterators and related functions for Deno. Similar to Python's itertools module. Unlicensed, do what you want.
Latest
function cacheIterator
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.

Type Parameters

T
optional
TReturn = any
optional
TNext = undefined

Parameters

iterator: Iterator<T, TReturn, TNext>