3

I am currently going through 'Javascript: The good parts' by Douglas Crockford, an there is an example demonstrating the concept of memoization.

var memoizer = function (memo, fundamental) {
  var shell = function (n) {
   var result = memo[n];
   if (typeof result !== 'number') {
     result = fundamental(shell, n);
     memo[n] = result;
   }
  return result;
  };
return shell;
};

var fibonacci = memoizer([0, 1], function (shell, n) {
   return shell(n - 1) + shell(n - 2);
});

What I don't understand is, where is the value for n coming from?

6
  • 1
    have a look here for another good example of js memoization: stackoverflow.com/questions/22578831/… Commented May 30, 2014 at 13:10
  • I would edit this to remove the tags from the title... but then it'd be empty. meta.stackexchange.com/questions/10647/… Commented May 30, 2014 at 13:11
  • 1
    @liath, sorry about the title.. You are absolutely right. Well the question is pretty stupid, and not really about memoization. However I needed help understanding what is happening in this specific memoization example.. Commented May 30, 2014 at 13:15
  • 1
    @user2678538 no worries, typically a title about the problem rather than the techs helps to attract answers. Commented May 30, 2014 at 13:17
  • 1
    possible duplicate of Javascript closure questions or Explanation on “JavaScript - the Good Parts” example (section 4.15)? Commented May 30, 2014 at 13:57

2 Answers 2

1

In the code var shell = function (n), you're specifying that when you call the function shell, you're going to provide it with an input argument n. So if you called shell(5), n would be equal to 5, or any other number that you passed in.

You need to look at what's being called and returned in each function call -- fibonacci is set to the returned value of the memoizer function, and memoizer returns the shell function which takes in n. So although it's never called in your example code, in the end you'd call, say, fibonacci(5) where 5 is your n. Just follow the function calls and returns.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes sure I am quite aware that upon invocation, the function shell would receive n value.. However, I got confused as to when the n is passed in this example. Basically, to verify that I am not loosing my mind.. So for this to work, do I need to invoke the memoizer function at some point ?
@user2678538: No, the memoized function. You are already invoking memoizer() when creating the fibonacci function.
You need to look at what's being created -- fibonacci is set to the returned value of the memoizer function, and memoizer returns the shell function which takes in n. So in the end you'd call fibonacci(5) where 5 is your n. Just follow the function calls and returns.
1

n is an input. It doesn't "come" from any where in the code you've posted, you have to supply it a value.

2 Comments

Yes sure I am quite aware that upon invocation, the function shell would receive n value.. However, I got confused as to when the n is passed in this example. Basically, to verify that I am not loosing my mind.. So for this to work, do I need to invoke the memoizer function at some point ?
@user2678538: He's trying to tell you that in this example no n is passed, since you don't call fibonacci anywhere (though shell is called twice).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.