简体   繁体   English

记忆化 javascript

[英]Memoization javascript

I'm studying JavaScript from the book JavaScript: The Good Parts , in the memoization section there is an example about using memoize technique to do Fibonacci problem我正在从JavaScript: The Good Parts一书中学习 JavaScript,在记忆部分有一个关于使用记忆技术来解决斐波那契问题的例子

We create a general function call memoizer , it takes an memo array and the fundamental function, returns a shell function that manages the memo and calls fundamental function我们创建一个通用函数调用memoizer ,它接受一个备忘录数组和基本函数,返回一个管理备忘录并调用基本函数的 shell 函数

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

And then create fibonacci like this:然后像这样创建斐波那契:

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

If I run fibonacci(10), the result will be displayed exactly.如果我运行 fibonacci(10),结果将准确显示。

But the thing that makes me confused is the n parameter shell function in memoizer function.但是让我困惑的是memoizer函数中的n个参数shell函数。 I know that it is the value that we want to compute.我知道这是我们想要计算的值。 But where is it come from?但它从哪里来? How can I call fibonacci(10), for example, can pass the value 10 to n ?例如,如何调用 fibonacci(10) 可以将值 10 传递给n And what is exactly the var fibonacci ?什么是 var fibonacci Is it a function or points to a function object as memoizer ?它是一个函数还是指向一个函数对象作为memoizer

Thanks for any help!谢谢你的帮助!

So, to understand this fully, you need to understand things below as fundamental pieces.因此,要完全理解这一点,您需要将下面的内容理解为基本部分。



So, now look at the code.所以,现在看看代码。
var memoizer is assigned as a function that returns an another function inside. var memoizer被分配为一个函数,该函数返回内部的另一个函数。

var memoizer = function(memo, fundamental) {
  var shell = function(n) {
    ... do some works ...
  };
  return shell;
};

Can you see it?你能看见它吗? var shell is returned at the end of the line in memoizer var shellmemoizer中的行memoizer


And whatever the inner logic is in memoizer , the result of it is assigned to var fibonacci无论memoizer的内部逻辑是memoizer ,它的结果都会分配给var fibonacci

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


So, it means fibonacci equals the result of memoizer (Since we excuted it), and the result of memoizer equals shell function.所以,这意味着fibonacci等于memoizer的结果(因为我们执行了它), memoizer的结果等于shell函数。 If you read those links given to you by me well, you can understand what is going on behind the scenes.如果你仔细阅读我给你的那些链接,你就可以了解幕后发生的事情。

By the way, the code you've given isn't the best way.顺便说一句,您提供的代码不是最好的方法。 Because anyway closure makes an activated object alive that isn't necessary.因为无论如何,闭包都会使一个没有必要的激活对象活着。

This code below is an another way of making fibonacci, this isn't the absolute best way , however, I recommend you to compare this code with your code, and figure the differences out.下面的代码是制作斐波那契的另一种方法,这不是绝对最好的方法,但是,我建议您将此代码与您的代码进行比较,并找出差异。

 var result = []; result[0] = 1; result[1] = 1; function fibonacci(n){ var i; for(i = 2; i < n; i++){ if(!result[i]){ result[i] = result[i-1] + result[i-2]; } } return result[i-1]; } console.log(fibonacci(10));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM