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

Range and Iteration

Generate sequences of numbers lazily.

Basic Range

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

const numbers = await range({ to: 5 }).collect();
// [0, 1, 2, 3, 4]

Exclusive vs Inclusive

to (exclusive)

const nums = await range({ to: 3 }).collect();
// [0, 1, 2]

until (inclusive)

const nums = await range({ until: 3 }).collect();
// [0, 1, 2, 3]

Custom Start

const nums = await range({ from: 5, to: 10 }).collect();
// [5, 6, 7, 8, 9]

Custom Step

const evens = await range({ from: 0, to: 10, step: 2 }).collect();
// [0, 2, 4, 6, 8]

Counting Down

const countdown = await range({ from: 5, to: 0, step: -1 }).collect();
// [5, 4, 3, 2, 1]

Real-World Examples

Repeat N Times

await range({ to: 10 }).forEach(i => {
  console.log(`Iteration ${i}`);
});

Generate Test Data

const users = await range({ to: 100 })
  .map(i => ({
    id: i,
    name: `User ${i}`,
    email: `user${i}@example.com`
  }))
  .collect();

Batch Processing

const batchSize = 10;
const total = 100;

for await (const batch of range({ from: 0, to: total, step: batchSize })) {
  const items = data.slice(batch, batch + batchSize);
  await processBatch(items);
}

Pagination

const pages = Math.ceil(total / pageSize);

for await (const page of range({ to: pages })) {
  const items = await fetchPage(page);
  await processItems(items);
}

Retry Logic

for await (const attempt of range({ to: 3 })) {
  try {
    await operation();
    break;
  } catch (error) {
    if (attempt === 2) throw error;
    await sleep(1000 * (attempt + 1));
  }
}

Infinite Ranges

Warning: Don't collect infinite ranges!

// ❌ Never completes
const infinite = await range({ from: 0, to: Infinity }).collect();

// ✅ Use with take()
const first100 = await range({ from: 0, to: Infinity })
  .take(100)
  .collect();

Performance

Ranges are lazy—numbers generated on demand:

// Doesn't generate all numbers upfront
const huge = range({ to: 1_000_000_000 });

// Only generates what you use
const first10 = await huge.take(10).collect();

Next Steps