Explanation of “JavaScript - Good Details” Example (Section 4.15)?

New to JS :) needs an explanation of the code snippet from Crockford's book , section 4.15:

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); }); 

Question : how do we calculate fibonacci (15), and if this is a simple fibonacci call (15), then how does this work in detail?

Thanks for the help.

+6
javascript memoization
source share
4 answers

As the comments on your question suggest, you must go through the code in the debugger to get a good idea of ​​what will happen if you cannot follow the explanation in the book. But I will give you a brief overview of what is happening:

What is being demonstrated is a “memoir”, which is a general optimization technique used in functional programming. A function is called pure if the result depends only on the arguments passed to it. So, if the function is pure, you can cache the result based on the arguments - this method is called memoisation. You would do this if the function is expensive to calculate and is called several times.

The classic example used to demonstrate this (as here) generates Fibonacci numbers . I will not understand how they are designed, but mainly as you move to higher numbers, you repeat more and more, since each number is calculated from the previous two numbers. Memoizing each intermediate result, you only need to calculate them once, so the algorithm will be much faster (much faster when you exit above the sequence).

Regarding this code, memoizer accepts two parameters - "memo", which is the cache. In this case, this happens with the first two values ​​already filled in with "[0,1]" - these are the first two Fibonacci numbers.

The second parameter is the function to which memoisation will be applied. In this case, the recursive Fibonacci function:

function (shell, n) {return shell (n - 1) + shell (n - 2); }

i.e. the result is the sum of the two previous numbers in the sequence.

The memoiser first checks to see if it already has a cached result. If this happens, it will return immediately. If it does not calculate the result and stores it in the cache. Without doing this, it repeats itself over and over again and quickly becomes incredibly slow to get to higher numbers in the sequence.

+1
source share

Here's a console.log() an annotated version that tries to show how the stack returns and assigns the result (n-1) + (n-2) to the memo array for each corresponding recursive call. Also remember that the stack returns in the reverse order. So in the logged output you will see that the last call was returned first:

 var memoizer = function (memo, fundamental) { var shell = function (n) { var result = memo[n]; if (typeof result !== 'number') { result = fundamental(shell, n); console.log("Hence 'shell(n-1)+shell(n-2)' results in the assignment memo["+n+"]="+result); memo[n] = result; } return result; }; return shell; }; var fibonacci = memoizer([0, 1], function (shell, n) { console.log("shell is called, and 'n' is equal to --> " + n + "\n" + "At this point shell(n-1)="+shell(n-1)+" AND shell(n-2)="+shell(n-2)); return shell(n - 1) + shell(n - 2); }); 
+3
source share

To evaluate a function, you just need to call it:

 fibonacci(15); 

If you want to see the result, the easiest way:

 alert(fibonacci(15)); 

If you want to do this more often, download Firebug and do it at the bottom of the script:

 Console.log(fibonacci(15)); 

Or type this directly into the Firebug console, and then press return:

 fibonacci(15) 
+1
source share

You seem to be confused about WHY causing fibonacci(15) . Let me simplify the code (forget memoization for a second).

 var m = function () { var s = function (n) { console.log(n); }; return s; }; var f = m(); 

We basically set f to the return value of the function m() . In this case, this return value is a function. See, we could simplify it like:

 var f = function (n) { console.log(n); }; 

In other words, we set f as a function that takes one parameter. We do the same in the Fibankichchi example. This is why calling fibonacci(15) works.

+1
source share

All Articles