Hyperlinkv0.8.0-beta.28

Iterable

Iterable.mapconsteffect/Iterable.ts:1487
<A, B>(f: (a: NoInfer<A>, i: number) => B): (
  self: Iterable<A>
) => Iterable<B>
<A, B>(self: Iterable<A>, f: (a: NoInfer<A>, i: number) => B): Iterable<B>

Transforms each element of an iterable using a function.

Details

This is one of the most fundamental operations for working with iterables. It applies a transformation function to each element, creating a new iterable with the transformed values. The operation is lazy, so elements are only transformed when the iterable is consumed.

Example (Mapping elements)

import { Iterable } from "effect"

// Transform numbers to their squares
const numbers = [1, 2, 3, 4, 5]
const squares = Iterable.map(numbers, (x) => x * x)
console.log(Array.from(squares)) // [1, 4, 9, 16, 25]

// Use index in transformation
const indexed = Iterable.map(["a", "b", "c"], (char, i) => `${i}: ${char}`)
console.log(Array.from(indexed)) // ["0: a", "1: b", "2: c"]

// Chain transformations
const result = Iterable.map(
  Iterable.map([1, 2, 3], (x) => x * 2),
  (x) => x + 1
)
console.log(Array.from(result)) // [3, 5, 7]
mapping
export const map: {
  <A, B>(
    f: (a: NoInfer<A>, i: number) => B
  ): (self: Iterable<A>) => Iterable<B>
  <A, B>(self: Iterable<A>, f: (a: NoInfer<A>, i: number) => B): Iterable<B>
} = dual(2, <A, B>(self: Iterable<A>, f: (a: A, i: number) => B): Iterable<B> => ({
  [Symbol.iterator]() {
    const iterator = self[Symbol.iterator]()
    let i = 0
    return {
      next() {
        const result = iterator.next()
        if (result.done) {
          return { done: true, value: undefined }
        }
        return { done: false, value: f(result.value, i++) }
      }
    }
  }
}))
Referenced by 4 symbols