Prelude API
    Preparing search index...

    Function flatMap

    • Creates a generator that maps each element in an iterable to another iterable, then flattens the results into a single generator. Similar to Array.prototype.flatMap(), but works with any iterable.

      Type Parameters

      • T

        Type of elements in the input iterable

      • R

        Type of elements in the resulting generator

      Parameters

      • map: (value: T, index: number) => Iterable<R>

        Function that maps each value to an iterable of new values

      Returns (values: Iterable<T>) => Generator<R>

      A function that takes an iterable and returns a flattened generator

      Values from all mapped iterables in sequence

      // Expand each number into an array of that many elements
      const expanded = [...flatMap((n: number) => Array(n).fill(n))([1, 2, 3])]
      // Result: [1, 2, 2, 3, 3, 3]
      // Map words to their characters
      const chars = [...flatMap((word: string) => word)(['hello', 'world'])]
      // Result: ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
      // With index
      const withIndices = [...flatMap((n: number, i: number) => [n, i])([10, 20, 30])]
      // Result: [10, 0, 20, 1, 30, 2]