如何在javascript中实现下划线memoize [英] How underscore memoize is implemented in javascript

查看:108
本文介绍了如何在javascript中实现下划线memoize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发自己的函数式编程库,现在引用下划线

I'm developing my own functional-programming library, and now referring the underscore.

memoize _.memoize(功能,[hashFunction])

memoize _.memoize(function, [hashFunction])


通过缓存计算结果来记忆给定函数。对于加速慢速运行计算很有用。如果传递了一个可选的hashFunction,它将用于根据原始函数的参数计算用于存储结果的哈希键。默认的hashFunction只使用memoized函数的第一个参数作为键。

Memoizes a given function by caching the computed result. Useful for speeding up slow-running computations. If passed an optional hashFunction, it will be used to compute the hash key for storing the result, based on the arguments to the original function. The default hashFunction just uses the first argument to the memoized function as the key.



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

以上代码可以自动记忆而无需处理数组看起来很神奇,我看到了下面的源代码,但内部设计仍然不清楚。

The above code that enables automatic memorisation without dealing array looks sort of magic, and I saw the source-code below, but still the inner design is not clear to me.

 // Memoize an expensive function by storing its results.
  _.memoize = function(func, hasher) {
    var memoize = function(key) {
      var cache = memoize.cache;
      var address = hasher ? hasher.apply(this, arguments) : key;
      if (!_.has(cache, address)) cache[address] = func.apply(this, arguments);
      return cache[key];
    };
    memoize.cache = {};
    return memoize;
  };

有人能简单介绍一下发生了什么吗?

Can someone give me a brief idea of what is going on?

赞赏。

推荐答案

memoize 有一个 cache memoize.cache = {} )用于存储函数调用的结果。当它被调用时,它确定一个地址来通过两种方式存储结果:调用 hasher 函数,或参数。

memoize has a cache (memoize.cache = {}) that uses for storing the result of the function call. When it's called, it determines an address to store the result by two means: either a call to the hasher function, or the key parameter.

hasher函数的工作原理如下(来自下划线页面):

The hasher function works like this (from the underscore page):


如果传递了一个可选的hashFunction,它将用于根据
原始函数的参数计算用于存储结果的
哈希键。默认的hashFunction只使用memoized函数的第一个
参数作为键。

If passed an optional hashFunction, it will be used to compute the hash key for storing the result, based on the arguments to the original function. The default hashFunction just uses the first argument to the memoized function as the key.

然后,它调用函数你传递 func.apply(...),并将结果存储在缓存[地址]

Then, it calls the function you passed func.apply(...), and stores the result at cache[address].

第二次调用memoized函数时,结果已经在缓存中(!_。has(..)将返回 false )并且不会重复计算。

The second time you call the memoized function, the result will already be in cache (!_.has(..) will return false) and the computation won't be repeated.

我不明白为什么它返回缓存[key] 而不是缓存[地址] 艰难...在我看来 cache [address] 将是正确的选择。

I dont' understand why it returns cache[key] and not cache[address] tough...seems to me that cache[address] would be the correct choice.

As在评论中指出,您提供的代码不是 memoize 的最新实现。这是最新的实现(1.6.0):

As pointed out in the comments, the code you present is not the latest implementation of memoize. This is the latest implementation (1.6.0):

_.memoize = function(func, hasher) {
    var memo = {};
    hasher || (hasher = _.identity);
    return function() {
      var key = hasher.apply(this, arguments);
      return _.has(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
    };
  }; 

它的工作方式相同,只是因为它更优雅;如果 hasher 函数没有提供,它使用 _。identity 作为键,这是一个简单返回的函数作为参数传递的值:

It works the same way, except from the fact that it is a little more elegant; if an hasher function it's not provided, it uses _.identity as a key, that is a function that simply returns the value passed as an argument:

_.identity = function(value) { return value; }

除此之外,缓存现在是叫做备忘录,但工作方式相同。

Aside from this, cache is now called memo but works the same way.

这篇关于如何在javascript中实现下划线memoize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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