Memoize a currified function

前端 未结 3 1688
别那么骄傲
别那么骄傲 2021-01-31 15:37
const f = (arg1) => (arg2) => { /* returns something */ }

Is it possible to memoize f with regard to the 2 arguments, namely:

f(1         


        
3条回答
  •  一整个雨季
    2021-01-31 16:28

    You can not to pass map to every function. You can do like the next:

    const memoize = fn => {
      const cache = {};
      return (...args) => {
        const curriedFn = fn(...args);
        return (...next) => {
          const key = // generate your key
          if (key in cache) return cache[key];
          return (cache[key] = curriedFn(...next));
        }
      }
    }

提交回复
热议问题