Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Zip and Enumerate

Combine and index iterables.

enumerate()

Wrap any iterable for Array-like methods:

import { enumerate } from "jsr:@j50n/proc@0.23.3";

const result = await enumerate([1, 2, 3])
  .map(n => n * 2)
  .collect();
// [2, 4, 6]

.enum()

Add indices to items:

const indexed = await enumerate(["a", "b", "c"])
  .enum()
  .collect();
// [["a", 0], ["b", 1], ["c", 2]]

Format with Indices

const numbered = await enumerate(["apple", "banana", "cherry"])
  .enum()
  .map(([fruit, i]) => `${i + 1}. ${fruit}`)
  .collect();
// ["1. apple", "2. banana", "3. cherry"]

zip()

Combine two iterables:

import { zip } from "jsr:@j50n/proc@0.23.3";

const names = ["Alice", "Bob", "Charlie"];
const ages = [25, 30, 35];

const people = await zip(names, ages)
  .map(([name, age]) => ({ name, age }))
  .collect();
// [{ name: "Alice", age: 25 }, ...]

Multiple Iterables

const combined = await zip(iter1, iter2)
  .map(([a, b]) => a + b)
  .collect();

Real-World Examples

Number Lines

const numbered = await read("file.txt")
  .lines
  .enum()
  .map(([line, i]) => `${i + 1}: ${line}`)
  .forEach(console.log);

Combine Data Sources

const merged = await zip(
  read("names.txt").lines,
  read("emails.txt").lines
)
  .map(([name, email]) => ({ name, email }))
  .collect();

Track Progress

const items = [...]; // Large array

await enumerate(items)
  .enum()
  .forEach(([item, i]) => {
    console.log(`Processing ${i + 1}/${items.length}`);
    process(item);
  });

Next Steps