为什么需要在记忆功能中应用? [英] Why need apply in memoization function?

查看:87
本文介绍了为什么需要在记忆功能中应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在怀念记忆的概念,显然有不同的实现方式.我把它们放在一起,似乎工作正常:

I've been toyin with concept of memoization and there are obviously different implementations of it. I put this together and it seems to work fine:

Function.prototype.memoized = function(a) {
    debugger
    if (typeof cache === "undefined") cache = [];
    if (cache[a]) {
        return cache[a];
    } else {
        cache[a] = this(a);
        return cache[a];
    }
}

Function.prototype.memoize=function() {
  t=this;
  return function() {
   // Resig seems to use this:
   return t.memoized.apply(t,arguments);
   // why not simple this:
   //return t.memoized(arguments[0]);
  }

}

myTest = (function fibonacci(n) {

    return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}).memoize();

console.log(myTest(2));

但是我的问题是为什么在某些实现中我看到了 return t.memoized.apply(t,arguments);而不是proptotype.memoize内部的简单return t.memoized(arguments[0]);?除了传递不使用的多个参数外,我看不到任何优势.我对apply有什么好处吗?

My question is however why in some implementations I see return t.memoized.apply(t,arguments); rather than simple return t.memoized(arguments[0]); inside of proptotype.memoize? I cannot see any advantage apart from passing multiple arguments which are not used anyway. I there any advantage to apply?

更新了代码,我相信这可以解决一些主要问题,因此在窗口上缓存为全局缓存(我在想什么?),而递归斐波那契不会记录对自身的调用.我的实施过程中还有其他重大问题吗?

Updated the code and I believe this takes care of major problems so cache as global on window (what was I thinking??) and recursive fibonacci not memoizing calls to itself. Are there any other major issue with my implementation?

Function.prototype.memoized = function(a) {

    if (typeof this.cache === "undefined")  this.cache = [];
    if (this.cache[a]) {
        return this.cache[a];
    } else {
        this.cache[a] = this(a);
        return this.cache[a];
    }
}

Function.prototype.memoize=function() {
  t=this;
  return function() {
   //return t.memoized.apply(t,arguments);
   return t.memoized(arguments[0]);
  }

}

myTest = (function fibonacci(n) {
    //return a * 3;

    return n < 2 ? n : fibonacci.memoized(n - 1) + fibonacci.memoized(n - 2);
}).memoize();

console.log(myTest(2));

顺便说一句,这对我来说是学习的经验,纯粹是为了娱乐而做,而不是作业或与之相关的任何事情.

BTW this is learning experience for me and doing it purely for fun, it's not assignment or anything like that uni related.

推荐答案

通常,JavaScript函数是变量,这意味着您实际上需要注意所有这些内容.某些memoize实现通过使用JSON.stringify(arguments)作为缓存键来做到这一点.

In general, JavaScript functions are variadic, which means that you would need to pay attention to all of them actually. Some memoize implementations do this by using JSON.stringify(arguments) for the cache key.

在这种情况下,这没有任何意义,因为memoized方法也只使用一个参数.

In this case, it does not make any sense, since the memoized method does only use a single argument as well.

但是,您的代码中存在的严重错误要多于这种小错误. cache是全局变量,而应绑定到特定的已记忆功能.另外,您的fib实现在递归调用中是未记录的,它实际上是最重要的.

However, there are more serious bugs in your code than this little inaccuracy. cache is a global variable, while it should be bound to the specific memoized function. Also, your fib implementation is not memoized in the recursive call, where it actually is the most important.

编辑后的版本看起来不错(全局t变量除外).就个人而言,我会缩短/简化代码:

The edited version looks good (except for the global t variable). In personal, I would have shortened/simplified the code a little bit however:

Function.prototype.memoize=function() {
    var t = this;
    this.cache = [];
    return function(a) {
        var cache = t.cache;
        if (typeof cache[a] == "undefined")
            cache[a] = t(a);
        return cache[a];
    };
}

var fibonacci = (function (n) {
    return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}).memoize();

console.log(fibonacci(2));

这篇关于为什么需要在记忆功能中应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆