Chapter 6, Producing Functions – Higher-Order Functions
6.1 Go with arrows: Just minor changes are needed:
const addLogging = <T extends (...args: any[]) => any>(
fn: T
): ((...args: Parameters<T>) => ReturnType<T>) => {
return (...args: Parameters<T>): ReturnType<T> => {
console.log(`entering ${fn.name}(${args})`);
const valueToReturn = fn(...args);
console.log(`exiting ${fn.name}=>${valueToReturn}`);
return valueToReturn;
};
};
6.2 Mapping for memory: Let’s do this for the most complete memoizing function we wrote, memoize4(). Instead of using an object for cache, we create a map. We check whether the map has the searched strX key, we set new values after calling the original function, and we get the return value from the cache. The as part in return is to let TypeScript know...