Javascript Closing Issues

While I was reading the Javascript: The Good Parts book. I can not understand the piece of code below:

We can generalize this by making a function that helps us create memoirs of a function. The memoizer function will take the original memo block and the fundamental function. It returns a shell function that controls the storage record, and this calls fundamental if necessary. We pass the shell of the function and the parameters of the function to the fundamental function:

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

Now we can define fibonacci with memoizer by providing the original note with an array and the main function:

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

My question is what is a test function? When is it defined and called? I think this is very confusing. Also I think this statement is: memo[n] = result; useless. Please correct if I am wrong.

+4
source share
2 answers

It was a piece of code to read :)

You probably know that memoization stores the result of the function, so the next time the function is called, it does not need to calculate the answer, it can just view it.

So, we need to store the answers for the fibonacci function, which takes an int and returns an int.

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

Calls memoizer with the original memo array, mapping fib (0) โ†’ 0 and fib (1) โ†’ 1.

The rest defines an unnamed function that takes a function and a number. "test" is a bad name, it should be "recursive_fibonacci_helper" :)

This unnamed function becomes the "main" parameter. The memoizer function returns a function (shell) that takes an int argument. This eventually becomes a fibonacci function.

So, when someone says "fibonacci (5)". They are really called a "shell (5)." An important part of closures is that the โ€œfundamentalโ€ and โ€œmemorabiliaโ€ are already connected.

So what does a shell do?

He searches the notes table to see if he has already calculated the answer for this input. If he sees the answer (== 'number'), he returns it. Otherwise, it calculates it and saves it in the notes table. memo[n] = result actually stores the calculated result in the memoization table.

+4
source

In the statement, memo[n] = result; The newly calculated number is stored in the memoization array or cache. The test function is an argument to a function that must be memoized and defined and passed to memoizer . When it is called, it checks if the value to be calculated has been cached. If so, it returns it from the cache. Otherwise, he recounts it again.

After the above code, we get something like this in memory (but with an array memo and orig_fibonacci ):

 var memo = [0, 1]; function fibonacci(n) { var result = memo[n]; if (typeof result != 'number') { result = orig_fibonacci(n); memo[n] = result; } return result; } function orig_fibonacci(n) { return fibonacci(n - 1) + fibonacci(n - 2); } 
+1
source

Source: https://habr.com/ru/post/1307246/


All Articles