Skip to content

iterate

Create an iterator for traversing array elements.

Basic Usage

typescript
import { iterate } from 'radash'

const numbers = [1, 2, 3, 4, 5]
const iterator = iterate(numbers)

let result = iterator.next()
while (!result.done) {
  console.log(result.value) // 1, 2, 3, 4, 5
  result = iterator.next()
}

Syntax

typescript
function iterate<T>(
  array: readonly T[]
): Iterator<T>

Parameters

  • array (readonly T[]): The array to iterate over

Return Value

Returns a standard JavaScript iterator object containing the next() method.

Examples

Basic Iteration

typescript
import { iterate } from 'radash'

const fruits = ['apple', 'banana', 'cherry']
const iterator = iterate(fruits)

console.log(iterator.next()) // { value: 'apple', done: false }
console.log(iterator.next()) // { value: 'banana', done: false }
console.log(iterator.next()) // { value: 'cherry', done: false }
console.log(iterator.next()) // { value: undefined, done: true }

Using for...of Loop

typescript
import { iterate } from 'radash'

const numbers = [1, 2, 3, 4, 5]
const iterator = iterate(numbers)

for (const num of iterator) {
  console.log(num) // 1, 2, 3, 4, 5
}

Manual Iteration Control

typescript
import { iterate } from 'radash'

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
]

const iterator = iterate(users)

// Get the first element
const first = iterator.next()
console.log(first.value) // { id: 1, name: 'Alice' }

// Skip the second element
iterator.next()

// Get the third element
const third = iterator.next()
console.log(third.value) // { id: 3, name: 'Charlie' }

Conditional Iteration

typescript
import { iterate } from 'radash'

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const iterator = iterate(numbers)

let result = iterator.next()
while (!result.done && result.value <= 5) {
  console.log(result.value) // 1, 2, 3, 4, 5
  result = iterator.next()
}

Combining with Generator Functions

typescript
import { iterate } from 'radash'

function* filterEven(iterator: Iterator<number>) {
  let result = iterator.next()
  while (!result.done) {
    if (result.value % 2 === 0) {
      yield result.value
    }
    result = iterator.next()
  }
}

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const iterator = iterate(numbers)

for (const even of filterEven(iterator)) {
  console.log(even) // 2, 4, 6, 8, 10
}

Handling Empty Arrays

typescript
import { iterate } from 'radash'

const emptyArray: number[] = []
const iterator = iterate(emptyArray)

const result = iterator.next()
console.log(result) // { value: undefined, done: true }

Notes

  1. Iterator state: The iterator remembers the current position, each call to next() advances to the next element
  2. One-time use: Iterators can only traverse forward, cannot be reset or traversed backward
  3. Empty array handling: If the array is empty, the first call to next() returns { done: true }
  4. Performance: Time complexity is O(1) per call, overall O(n)

Differences from Other Methods

  • for...of: Directly traverses arrays, syntax is more concise
  • iterate(): Returns an iterator object, provides more fine-grained control
  • forEach(): Immediately executes all elements, cannot stop midway
  • map(): Returns a new array, not an iterator

Practical Application Scenarios

  1. Stream processing: Process elements one by one when handling large amounts of data
  2. Conditional traversal: Decide whether to continue traversal based on conditions
  3. Memory optimization: Avoid loading all data into memory at once
  4. Custom iteration: Implement custom iteration logic
  5. Asynchronous processing: Process elements one by one in asynchronous environments

Released under the MIT License.