Hyperlinkv0.8.0-beta.28

Iterable

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

Applies a function to each element in an Iterable and returns a new Iterable containing the concatenated mapped elements.

Example (Flat mapping iterables)

import { Iterable } from "effect"

// Expand each number to a range
const numbers = [1, 2, 3]
const expanded = Iterable.flatMap(numbers, (n) => Iterable.range(1, n))
console.log(Array.from(expanded)) // [1, 1, 2, 1, 2, 3]

// Split strings into characters
const words = ["hi", "bye"]
const chars = Iterable.flatMap(words, (word) => word)
console.log(Array.from(chars)) // ["h", "i", "b", "y", "e"]

// Conditional expansion with empty iterables
const values = [1, 2, 3, 4, 5]
const evenMultiples = Iterable.flatMap(
  values,
  (n) => n % 2 === 0 ? [n, n * 2, n * 3] : []
)
console.log(Array.from(evenMultiples)) // [2, 4, 6, 4, 8, 12]

// Use index in transformation
const letters = ["a", "b", "c"]
const indexed = Iterable.flatMap(
  letters,
  (letter, i) => Iterable.replicate(letter, i + 1)
)
console.log(Array.from(indexed)) // ["a", "b", "b", "c", "c", "c"]
sequencing
export const flatMap: {
  <A, B>(
    f: (a: NoInfer<A>, i: number) => Iterable<B>
  ): (self: Iterable<A>) => Iterable<B>
  <A, B>(self: Iterable<A>, f: (a: NoInfer<A>, i: number) => Iterable<B>): Iterable<B>
} = dual(
  2,
  <A, B>(self: Iterable<A>, f: (a: A, i: number) => Iterable<B>): Iterable<B> => flatten(map(self, f))
)
Referenced by 1 symbols