在JavaScript中实现自动记忆(返回一个关闭的函数) [英] Implementing Automatic Memoization (returns a closured function) in JavaScript
问题描述
我已阅读
http://www.sitepoint.com/implementing-memoization-in-javascript/
自动记忆
在前面的所有示例中,显式修改了函数以添加memoization。也可以在不修改功能的情况下实现存储器基础结构。这很有用,因为它允许函数逻辑与memoization逻辑分开实现。这是通过创建一个实用程序函数来完成的,该函数将函数作为输入并对其应用memoization。以下memoize()函数将函数func作为输入。 memoize()返回一个新函数,它围绕func包装一个缓存机制。请注意,此函数不处理对象参数。为了处理对象,需要一个循环来分别检查每个参数并根据需要进行字符串化。
function memoize(func){
var memo = {};
var slice = Array.prototype.slice;
return function(){
var args = slice.call(arguments);
if(备忘录中的args)
返回memo [args];
else
return(memo [args] = func.apply(this,args));
}
}
使用这个,我做了
var fib = function(n)
{
if(n< = 1)
{
返回1; //作为Math
中的Fib定义}
else
{
返回fib(n - 2)+ fib(n - 1); //作为Math
中的Fib定义}
};
log(memoize(fib)(43));
log(fib(43));
但是,我确认没有效果。
<为了同样的目的,我也尝试了一个npm库,
https://github.com/medikoo/memoize
和
var memoize = require('memoizee');
log(memoize(fib)(43));
log(fib(43));
结果相同。
什么我想念,以及如何修复并使其正常工作?
谢谢!
编辑
require('memoizee');
var fib = function(n)
{
if(n< = 1)
{
return 1; //作为Math
中的Fib定义}
else
{
返回fib(n - 2)+ fib(n - 1); //作为Math
中的Fib定义}
};
var generator = function(f)
{
return memoize(f);
};
var _fib = generator(fib);
console.log(_fib(40)); //无效
memoize
调用不会改变 fib
函数,但返回其新的memoized对应函数。在你的代码中,你只调用一次,原来的 fib
下次运行。您需要创建一个备忘的包装器,然后将其称为多个次:
var mFib = memoize(fib);
log(mFib(43));
log(mFib(43));
您还可以覆盖原始 fib = memoize(fib);
,这将带来额外的好处,即递归调用(有趣的调用)也将被记忆。
I've read
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() returns a new function which wraps a caching mechanism around "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,
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. In your code, you're calling that one only once, and the original fib
function the next time. You need to create one memoized "wrapper", and call that multiple times:
var mFib = memoize(fib);
log(mFib(43));
log(mFib(43));
You could also overwrite the original fib = memoize(fib);
, which would have the additional benefit that the recursive calls (which are the interesting ones) will be memoized as well.
这篇关于在JavaScript中实现自动记忆(返回一个关闭的函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!