Prelude API
    Preparing search index...

    Function memoized

    • Creates a memoized generator function that caches values from an iterable. This allows multiple iterations over the same values without recomputing them. The function keeps a cache of all values produced so far, and only advances the source iterator when needed to produce new values.

      Type Parameters

      • T

        The type of elements in the iterable

      Parameters

      • iterable: Iterable<T>

        The source iterable to memoize

      Returns { values: T[]; (): Generator<T> }

      A generator function that produces the same values as the input iterable and caches them

      The same values as the input iterable

      // Create a memoized generator that remembers values
      const expensive = function*() {
      console.log('Computing 1'); yield 1;
      console.log('Computing 2'); yield 2;
      console.log('Computing 3'); yield 3;
      };

      const cached = G.memoized(expensive());

      // First run: logs 'Computing 1', 'Computing 2', 'Computing 3'
      Array.from(cached()); // [1, 2, 3]

      // Second run: no logs, values come from cache
      Array.from(cached()); // [1, 2, 3]

      // Access the cached values directly
      console.log(cached.values); // [1, 2, 3]