Options
All
  • Public
  • Public/Protected
  • All
Menu

Class LazyBufferedIterable<T>

Makes a copy of the elements returned by the given iterable so that multiple iterations over these elements are possible. This is used by IStream#fork.

The original iterable is queried only on-demand, ie. when items are requested from this iterable. This reduces memory consumption and allows unlimited iterables to be buffered.


// An iterable that returns an unlimited amount of random numbers.
function * randomStream() {
  while(true) yield Math.random();
}

// Many iterations over the same random numbers are possible with buffering.
const iterable = new LazyBufferedIterable(randomStream());
for (const it = iterable[Symbol.iterator](), i = 10; i-->0;)
  console.log(it.next().value);
for (const it = iterable[Symbol.iterator](), i = 10; i-->0;)
  console.log(it.next().value);
// => Both times the same random numbers are printed.

// Illustrates buffered vs. unbuffered iterators
const set = new Set();
set.add(1);
set.add(2);
set.add(3);

// An unbuffered iterables only returns its values once.
const unbuffered = set.values();
Array.from(unbuffered) // => [1,2,3]
Array.from(ubuffered) // => []

// A buffered iterable can return them as many times as requested.
const buffered = new LazyBufferedIterable(set.values());
Array.from(buffered) // => [1,2,3]
Array.from(buffered) // => [1,2,3]

Type parameters

  • T

Hierarchy

  • LazyBufferedIterable

Implements

  • Iterable<T>

Index

Constructors

Methods

Constructors

constructor

Methods

__@iterator

  • __@iterator(): Iterator<T>

Generated using TypeDoc