@aws/pdk
Version:
All documentation is located at: https://aws.github.io/aws-pdk
59 lines (52 loc) • 1.55 kB
JavaScript
var _arity =
/*#__PURE__*/
require("./internal/_arity.js");
var _curry2 =
/*#__PURE__*/
require("./internal/_curry2.js");
var _has =
/*#__PURE__*/
require("./internal/_has.js");
/**
* Creates a new function that, when invoked, caches the result of calling `fn`
* for a given argument set and returns the result. Subsequent calls to the
* memoized `fn` with the same argument set will not result in an additional
* call to `fn`; instead, the cached result for that set of arguments will be
* returned.
*
* Care must be taken when implementing key generation to avoid key collision,
* or if tracking references, memory leaks and mutating arguments.
*
* @func
* @memberOf R
* @since v0.24.0
* @category Function
* @sig (*... -> String) -> (*... -> a) -> (*... -> a)
* @param {Function} fn The function to generate the cache key.
* @param {Function} fn The function to memoize.
* @return {Function} Memoized version of `fn`.
* @example
*
* let count = 0;
* const factorial = R.memoizeWith(Number, n => {
* count += 1;
* return R.product(R.range(1, n + 1));
* });
* factorial(5); //=> 120
* factorial(5); //=> 120
* factorial(5); //=> 120
* count; //=> 1
*/
var memoizeWith =
/*#__PURE__*/
_curry2(function memoizeWith(mFn, fn) {
var cache = {};
return _arity(fn.length, function () {
var key = mFn.apply(this, arguments);
if (!_has(key, cache)) {
cache[key] = fn.apply(this, arguments);
}
return cache[key];
});
});
module.exports = memoizeWith;