Have been trying to learn more about functional programming by looking at the underscore documentation and attempting to write my own versions of the more frequently used functions.
Coming across "memoize" - I was having trouble wrapping my head around it, and found some information in Crockford's 'The Good Parts'.
_.memoize = function(func) {
// 'cache' object is used to hold the result of the memoized fn's call
var cache = {};
var recur = function(n) {
var result = cache[n];
if (typeof result === 'undefined') {
result = func.apply(this, arguments);
cache[n] = result;
}
return result;
}
return recur;
};
Could you please help me understand if my use of .apply was even necessary and if there is any core improvement I can make to this code? Really appreciate the help!
funcwill have