JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q9814F
  • License MIT

A lazy iterator interface for synchronous and asynchronous iterators

Package Exports

  • lazy-iters

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (lazy-iters) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

lazy-iters

The lazy-iters package provides a common interface for iterators. It provides common intermediate operations, such as map and filter which are only evaluated after calling a terminal operation. These same methods are also offered for asynchronous iterators. The only difference is that all terminal operations for asynchronous generators are themselves asynchronous.

This interface was inspired in large part by Rust's Iterator trait.

Examples

import { iterator } from "lazy-iters";

// Create anything that can be iterated over using for ... of.
const list = [1, 2, 3, 4, 5];

// Convert it into a lazy iterator and chain methods on it.
const odds = iterator(list)
  .filter(x => x % 2 != 0)
  .collect();

// You can also do whatever else you want with it.
const sumOfSquaredEvens = iterator(list)
  .filter(x => x % 2 == 0)
  .map(x => x * x)
  .sum();

In addition, asynchronous iterators are also offered:

import { asyncIterator } from "lazy-iters";

/**
 * Sleeps for `ms` milliseconds.
 * @param {number} ms the number of milliseconds to sleep for
 */
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

/**
 * Generate a number every 1 second.
 */
async function* asyncGenerator() {
  let i = 0;
  while (true) {
    yield i;
    await sleep(1000);
  }
}

async function doSomething() {
  const generator = asyncGenerator();
  const sumOfFirst10Evens = await asyncIterator(generator)
    .filter(x => x % 2 == 0)
    .take(10)
    .sum();
}