Effect CommunityEC
Effect Community3y ago
4 replies
bigpopakap

Creating a Self-Referencing Cache for Recursive Functions

How would I go about making a cache that can reference itself? I have a recursive function that looks (in psuedocode) like this:
// Psuedocode
const intensiveWork: (
  key: string
) => Effect.Effect<never, never, string> => pipe(
  if (isBaseCase(key)) {
    return calculateValue(key); // synchronoush, not a lot of work
  }

  // Otherwise, recurse into a bunch of other cases
  return pipe(
      makeChildKeys(key), // choose all the cases to recurse into
      childKey => intensiveWork(childKey), // do the recursion
      ReadonlyArray.join(" "), // join all the results
  );
);

And during this recursion, there's going to be a ton of duplicate computations, so I'd like the recursive call to intensiveWork to also be cached
Was this page helpful?