简体   繁体   English

Crockford 书中 Javascript 中的记忆

[英]Memoization in Javascript from Crockford's book

Example 1.示例 1。

var fibonacci = function () {
  var memo = [0, 1];
  var fib = function (n) {
    var result = memo[n];

    if (typeof result !== 'number') {
      result = fib(n - 1) + fib(n - 2);
      memo[n] = result;
    }

    return result;
  };

  return fib;
}();
  

Example 2.示例 2。

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

Above are 2 code snippets from Crockford's book to demonstrate the concept of memoization.以上是 Crockford 书中的 2 个代码片段,用于演示记忆的概念。

Question 1. How do I invoke function fibonacci from any of 2 examples?问题 1. 如何从 2 个示例中的任何一个调用 function 斐波那契? By usual way fibonacci (5) ?按照通常的方式斐波那契 (5)

Question 2. As I can see, argument "n" is not defined anywhere when calling var fib = function (n) { or var shell = function (n) { .问题 2. 正如我所看到的,在调用var fib = function (n) {var shell = ZC1C425268E68385D145074C17A9 (F ) 时,参数“n”未在任何地方定义In the first example of fibonacci function I'd expect "n" to be defined right after the 2nd line var memo = [0, 1];在斐波那契 function 的第一个示例中,我希望在第二行var memo = [0, 1];之后立即定义“n” , and I'd expect "n" to be defined as follows: n = arguments[0]; ,我希望“n”定义如下: n = arguments[0]; . . However since this doesn't seem to be the case, so I have to ask: how is "n" determined when fibonacci is invoked?然而,由于情况似乎并非如此,所以我不得不问:当调用斐波那契时,“n”是如何确定的?

Thanks.谢谢。

Page from Crockford's books克罗克福德书中的一页

Question 1: How do I call each one?问题1:我如何称呼每个人?

The first example, note the }() at the end.第一个示例,请注意末尾的}() This is invoking the function, which in turn gives the fib function back.这是调用 function,这反过来又返回了fib function。 So fibonacci() in the first example is really fib() with it's own internal references (eg memo and fib ), memo only being accessible by fib but lasting as a reference as long as a reference to fib exists (the variable holding fibonacci 's return).所以第一个例子中的fibonacci()实际上是fib()具有它自己的内部引用(例如memofib ), memo只能由fib访问,但只要对fib的引用存在(变量持有fibonacci '返回)。

The second example is similar, except it's substituting a memoizer that accepts an array of items and returns shell , which has access to the array it was passed.第二个示例类似,只是它替换了一个接受项目数组并返回shell的记忆器,它可以访问它传递的数组。

Question 2: What is n as an argument doing?问题 2:作为参数的n是做什么的?

Since you're really calling a reference to an internally created but externally available fib() and shell() when you call either fibonacci function, you're passing in a new number, which then has it's fibonacci sequence "memoized" by storing it in the internally available memo given at the beginning.由于当您调用fibonacci那契 function 时,您实际上是在调用对内部创建但外部可用的fib()shell()的引用,因此您传入了一个新数字,然后通过存储它来“记忆”它的斐波那契数列在开头给出的内部可用memo中。


The point is that memoization is like a hash store where computations already known (since they were previously performed and "memoized") are accessible (preventing re-computation), and using Javascript's closure construct allows you to use internally-scoped variables to manage that access.关键是 memoization 就像一个 hash 存储,其中可以访问已知的计算(因为它们之前已执行并“记忆”)(防止重新计算),并且使用 Javascript 的闭包结构允许您使用内部范围的变量来管理它使用权。

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

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