如何创建备忘录功能 [英] How to create a memoize function

查看:104
本文介绍了如何创建备忘录功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为这个记忆问题感到困惑.我需要创建一个函数,该函数将检查是否已为给定参数计算出一个值,返回上一个结果,或运行计算并返回该值.

I am stumped with this memoize problem. I need to create a function that will check to see if a value has already been calculated for a given argument, return the previous result, or run the calculation and return that value.

当我是JS新手时,我已经花了数小时.我无法理解如何做到这一点.我无法使用任何内置函数,并且真的很想了解我需要做什么.

I have spent hours on this and while I am new to JS. I cannot get my head around how to do this. I cannot use any built in functions and would really like to understand what I need to do.

这是我到目前为止所拥有的,这是非常错误的,在这一点上感觉就像是伪代码.我已经搜索了现有的备忘问题,但似乎无法解决任何问题.非常感谢您的帮助.

Here is what I have so far, which is so wrong it feels like pseudo-code at this point. I have searched existing memoize questions out here but I cannot seem to make any solution work yet. Any help is much appreciated.

  myMemoizeFunc = function(passedFunc) {
  var firstRun = passedFunc;
  function check(passedFunc){
    if(firstRun === undefined){
        return passedFunc;
    }else{return firstRun;}
  }
  };

对不起,我应该更清楚了.这是我的具体要求: myMemoizeFunc必须返回一个函数,该函数将检查是否已经为给定的arg计算了计算,并在可能的情况下返回该val. passFunc是保存计算结果的函数. 我了解这似乎是重复的,但我要标记为不是,因为我在理解我应该在此处执行的操作时遇到了一些严重困难,并且需要比其他职位更多的帮助. 这就是我的思考过程带给我的方向,但我又遥遥领先.

Sorry, I should have been more clear. Here are my specific requirements: myMemoizeFunc must return a function that will check if the calculation has already been calculated for the given arg and return that val if possible. The passedFunc is a function that holds the result of a calculation. I understand this may seem like a duplicate, but I am marking as not so, as I am having some serious difficulty understanding what I should do here, and need further help than is given in other posts. This is what my thought process is bringing me towards but again, I am way off.

myMemoizeFunc = function(passedFunc) {
var allValues = [];
return function(){
    for(var i = 0; i < myValues.length; i++){
        if(myValues[i] === passedFunc){
            return i;
        }
        else{
            myValues.push(passedFunc);
            return passedFunc;
        }
    }
  }
};

我不应该在这里返回i或passFunc,但是在检查值时在if/else内还能做什么?我一直在研究这个问题很久了,我开始实现荒谬的代码,需要一些新的建议.

I should not be returning i or passedFunc here, but what else could I do within the if/else while checking for a value? I have been looking at this problem for so long, I am starting to implement code that is ridiculous and need some fresh advice.

推荐答案

我认为,这样做的主要技巧是使对象存储以前传递的参数作为键,并以函数的结果作为值.

I think the main trick for this is to make an object that stores arguments that have been passed in before as keys with the result of the function as the value.

对于记忆单个参数的功能,我将这样实现:

For memoizing functions of a single argument, I would implement it like so:

var myMemoizeFunc = function (passedFunc) {
    var cache = {};
    return function (x) {
        if (x in cache) return cache[x];
        return cache[x] = passedFunc(x);
    };
};

然后,您可以使用它来记住任何带有单个参数的函数,例如,用于计算阶乘的递归函数:

Then you could use this to memoize any function that takes a single argument, say for example, a recursive function for calculating factorials:

var factorial = myMemoizeFunc(function(n) {
    if(n < 2) return 1;
    return n * factorial(n-1);
});

这篇关于如何创建备忘录功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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