Pascal's Triangle

caching decorator

// โญ๏ธ caching decorator
//    use a closure to ecapsulate 'f'
export function caching(f) {
  // make a new cache for this 'f'
  let cache = new Map();

  // returns a function that can remember
  return function (...args) {
    // if not in cache, cache it.
    const key = `${args}`;
    if (!cache.has(key)) {
      //          โ•ญ- call a method -โ•ฎ  ('f' could be a method)
      let value = f.apply(this, args); // โญ๏ธ with "this" context
      cache.set(key, value);
      // log(`(${key}, ${value}) cached ...`);
    }
    // get function value from cache
    return cache.get(key);
  };
}

Last updated