简体   繁体   English

在JavaScript中实现自动备忘(返回封闭函数)

[英]Implementing Automatic Memoization (returns a closured function) in JavaScript

I've read 我读了

http://www.sitepoint.com/implementing-memoization-in-javascript/ http://www.sitepoint.com/implementing-memoization-in-javascript/

Automatic Memoization 自动记忆

In all of the previous examples, the functions were explicitly modified to add memoization. 在所有前面的示例中,功能都被显式修改以添加备注。 It is also possible to implement a memoization infrastructure without modifying the functions at all. 也有可能实现一种回忆基础结构而根本不修改功能。 This is useful because it allows the function logic to be implemented separately from the memoization logic. 这很有用,因为它允许功能逻辑与备注逻辑分开实现。 This is done by creating a utility function which takes a function as input and applies memoization to it. 这是通过创建一个效用函数来完成的,该函数将一个函数作为输入并对其应用备注。 The following memoize() function takes a function, “func”, as input. 下面的memoize()函数将函数“ func”作为输入。 memoize() returns a new function which wraps a caching mechanism around “func”. memoize()返回一个新函数,该函数将缓存机制包装在“ func”周围。 Note that this function does not handle object arguments. 请注意,此函数不处理对象参数。 In order to handle objects, a loop is required which would inspect each argument individually and stringify as needed. 为了处理对象,需要一个循环,该循环将单独检查每个参数并根据需要进行字符串化。

function memoize(func) {
  var memo = {};
  var slice = Array.prototype.slice;

  return function() {
    var args = slice.call(arguments);

    if (args in memo)
      return memo[args];
    else
      return (memo[args] = func.apply(this, args));

  }
}

using this, I did 使用这个,我做到了

var fib = function(n)
{
  if (n <= 1)
  {
    return 1; // as the Fib definition in Math
  }
  else
  {
    return fib(n - 2) + fib(n - 1); // as the Fib definition in Math
  }
};

log(memoize(fib)(43));
log(fib(43));

However, I confirmed there's no effect. 但是,我确认没有效果。

I also tried a npm library for the same purpose, 我也出于同样的目的尝试了一个npm库,

https://github.com/medikoo/memoize https://github.com/medikoo/memoize

and

var memoize = require('memoizee');

log(memoize(fib)(43));
log(fib(43));

The result, same. 结果是一样的。

What do I miss, and how to fix and make it work? 我想念什么,如何解决并使之正常工作?

Thanks! 谢谢!

EDIT 编辑

require('memoizee');

var fib = function(n)
{
  if (n <= 1)
  {
    return 1; // as the Fib definition in Math
  }
  else
  {
    return fib(n - 2) + fib(n - 1); // as the Fib definition in Math
  }
};

var generator = function(f)
{
  return memoize(f);
};

var _fib = generator(fib);
console.log(_fib(40)); //no effect

The memoize call does not alter the fib function, but returns its new, memoized counterpart. memoize调用不会更改fib函数,但会返回其新的,已记忆的对应函数。 In your code, you're calling that one only once, and the original fib function the next time. 在您的代码中,您仅需调用一次,而下一次将调用原始的fib函数。 You need to create one memoized "wrapper", and call that multiple times: 您需要创建一个 memoized“包装”,并称之为多次

var mFib = memoize(fib);
log(mFib(43));
log(mFib(43));

You could also overwrite the original fib = memoize(fib); 您还可以覆盖原始的fib = memoize(fib); , which would have the additional benefit that the recursive calls (which are the interesting ones) will be memoized as well. ,这还有一个好处,即递归调用(很有趣)也将被记住。

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

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