Comparison of prog1 and prog2 using Effect.cachedFunction

I have two versions of what I thought was the same program. I am trying to use Effect.cachedFunction. In prog1, I use run-of-the-mill flatMapping. In prog2, I use a generator. Prog2 works but prog1 fails (returns two different values). Would you know why?
import { Effect, Random } from 'effect';

const randomNumber = (n: number) => Random.nextIntBetween(n, n + n + 1);

const memoized = (n: number) =>
    Effect.flatMap(Effect.cachedFunction(randomNumber), (f) => f(n));
const prog1 = Effect.zip(memoized(10), memoized(10));
const result1 = Effect.runSync(prog1);

const prog2 = Effect.gen(function* (_) {
    const memoized = yield* _(Effect.cachedFunction(randomNumber));
    return [yield* _(memoized(10)), yield* _(memoized(10))];
});
const result2 = Effect.runSync(prog2);

console.log(result1);
console.log(result2);
Was this page helpful?