Skip to content
intermediate

Memoize a single-argument call

Complete the caching wrapper — the table that holds results, the lookup that short-circuits, and the write that pays for the next call.

/**
 * Returns a function with the same signature that calls `fn` at most once
 * per distinct argument.
 */
export function memoize<A extends string, R>(fn: (arg: A) => R): (arg: A) => R {
  const _____ = new Map<A, R>()

  return (arg: A): R => {
    if (cache.___(arg)) {
      return cache.get(arg) as R
    }

    const result = fn(arg)
    cache.___(arg, result)
    return result
  }
}
0/3 filled tab moves · Ctrl+↵ submits · Ctrl+Shift+↵ runsstarter code