UNPKG

1.4 kBJavaScriptView Raw
1var _arity =
2/*#__PURE__*/
3require("./internal/_arity");
4
5var _curry2 =
6/*#__PURE__*/
7require("./internal/_curry2");
8
9var _has =
10/*#__PURE__*/
11require("./internal/_has");
12/**
13 * Creates a new function that, when invoked, caches the result of calling `fn`
14 * for a given argument set and returns the result. Subsequent calls to the
15 * memoized `fn` with the same argument set will not result in an additional
16 * call to `fn`; instead, the cached result for that set of arguments will be
17 * returned.
18 *
19 *
20 * @func
21 * @memberOf R
22 * @since v0.24.0
23 * @category Function
24 * @sig (*... -> String) -> (*... -> a) -> (*... -> a)
25 * @param {Function} fn The function to generate the cache key.
26 * @param {Function} fn The function to memoize.
27 * @return {Function} Memoized version of `fn`.
28 * @example
29 *
30 * let count = 0;
31 * const factorial = R.memoizeWith(R.identity, n => {
32 * count += 1;
33 * return R.product(R.range(1, n + 1));
34 * });
35 * factorial(5); //=> 120
36 * factorial(5); //=> 120
37 * factorial(5); //=> 120
38 * count; //=> 1
39 */
40
41
42var memoizeWith =
43/*#__PURE__*/
44_curry2(function memoizeWith(mFn, fn) {
45 var cache = {};
46 return _arity(fn.length, function () {
47 var key = mFn.apply(this, arguments);
48
49 if (!_has(key, cache)) {
50 cache[key] = fn.apply(this, arguments);
51 }
52
53 return cache[key];
54 });
55});
56
57module.exports = memoizeWith;
\No newline at end of file