FuncType - v0.8.85
    Preparing search index...

    Variable LazyList

    LazyList: <A extends unknown>(iterable: Iterable<A>) => LazyList<A> & {
        cycle: <A extends unknown>(iterable: Iterable<A>) => LazyList<A>;
        empty: <A extends unknown>() => LazyList<A>;
        from: <A extends unknown>(...values: A[]) => LazyList<A>;
        generate: <A extends unknown>(f: () => A) => LazyList<A>;
        iterate: <A extends unknown>(initial: A, f: (a: A) => A) => LazyList<A>;
        of: <A extends unknown>(value: A) => LazyList<A>;
        range: (start: number, end: number, step?: number) => LazyList<number>;
        repeat: <A extends unknown>(value: A, n?: number) => LazyList<A>;
    }

    Lazy list implementation for efficient deferred computation

    Type declaration

      • <A extends unknown>(iterable: Iterable<A>): LazyList<A>
      • Create a LazyList from an iterable

        Type Parameters

        • A extends unknown

        Parameters

        • iterable: Iterable<A>

        Returns LazyList<A>

        const lazy = LazyList([1, 2, 3, 4, 5])
        .map(x => x * x)
        .filter(x => x % 2 === 1)
        .toArray() // [1, 9, 25]
        // From generator function
        function* naturals() {
        let n = 1
        while (true) yield n++
        }
        const firstTenSquares = LazyList(naturals())
        .map(x => x * x)
        .take(10)
        .toArray() // [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    • cycle: <A extends unknown>(iterable: Iterable<A>) => LazyList<A>

      Create a LazyList that cycles through an iterable infinitely

    • empty: <A extends unknown>() => LazyList<A>

      Create an empty LazyList

      const empty = LazyList.empty<number>()
      empty.toArray() // []
    • from: <A extends unknown>(...values: A[]) => LazyList<A>

      Create a LazyList from multiple values

    • generate: <A extends unknown>(f: () => A) => LazyList<A>

      Create an infinite LazyList by repeatedly calling a function

    • iterate: <A extends unknown>(initial: A, f: (a: A) => A) => LazyList<A>

      Create an infinite LazyList by repeatedly applying a function

      // Powers of 2
      const powers = LazyList.iterate(1, x => x * 2)
      .take(10)
      .toArray() // [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
      // Fibonacci sequence
      const fib = LazyList.iterate([0, 1], ([a, b]) => [b, a + b])
      .map(([a]) => a)
      .take(8)
      .toArray() // [0, 1, 1, 2, 3, 5, 8, 13]
    • of: <A extends unknown>(value: A) => LazyList<A>

      Create a LazyList from a single value

      const single = LazyList.of(42)
      .map(x => x * 2)
      .toArray() // [84]
    • range: (start: number, end: number, step?: number) => LazyList<number>

      Create a LazyList of numbers from start to end (exclusive)

      LazyList.range(1, 6).toArray() // [1, 2, 3, 4, 5]
      LazyList.range(0, 10, 2).toArray() // [0, 2, 4, 6, 8]
      LazyList.range(10, 0, -1).toArray() // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
      // Sum of squares from 1 to 100
      const sum = LazyList.range(1, 101)
      .map(x => x * x)
      .reduce((a, b) => a + b, 0) // 338350
    • repeat: <A extends unknown>(value: A, n?: number) => LazyList<A>

      Create a LazyList that repeats a value n times (or infinitely if n is not provided)

    // Process large datasets efficiently
    const result = LazyList.range(1, 1000000)
    .filter(x => x % 2 === 0)
    .map(x => x * x)
    .take(5)
    .toArray() // [4, 16, 36, 64, 100]
    // Infinite sequences
    const primes = LazyList.iterate(2, n => n + 1)
    .filter(isPrime)
    .take(10)
    .toArray() // First 10 prime numbers
    // Combining operations
    const evens = LazyList.range(0, 100, 2)
    const odds = LazyList.range(1, 100, 2)
    const combined = evens.zip(odds)
    .map(([e, o]) => e + o)
    .take(5)
    .toArray() // [1, 5, 9, 13, 17]