From c9a30dc4e5ad8d5bde57c63edb186000e8b515d6 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Sun, 31 Dec 2017 12:07:56 +0000 Subject: [PATCH] Travis build: 707 [ci skip] --- README.md | 30 ++++++++++++++++++++++++++++++ docs/index.html | 10 +++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 01f416b3c..2bcb93460 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,7 @@ * [`compose`](#compose) * [`curry`](#curry) * [`functionName`](#functionname) +* [`memoize`](#memoize) * [`runPromisesInSeries`](#runpromisesinseries) * [`sleep`](#sleep) @@ -2176,6 +2177,35 @@ functionName(Math.max); // max (logged in debug channel of console)
[⬆ Back to top](#table-of-contents) +### memoize + +Returns the memoized (cached) function. + +Use `Object.create(null)` to create an empty object without `Object.prototype` (so that those properties are not resolved if the input value is something like `'hasOwnProperty'`). +Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not. + +```js +const memoize = fn => { + const cache = Object.create(null); + return value => cache[value] || (cache[value] = fn(value)); +}; +``` + +
+Examples + +```js +// See the `anagrams` snippet. +const anagramsCached = memoize(anagrams); +anagramsCached('javascript'); // takes a long time +anagramsCached('javascript'); // returns virtually instantly since it's now cached +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### runPromisesInSeries Runs an array of promises in series. diff --git a/docs/index.html b/docs/index.html index 2acf4f2bb..d10bb750a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -436,6 +436,14 @@ multiplyAndAdd5(5, 2); // 15
 curry(Math.min, 3)(10)(50)(2); // 2
 

functionName

Logs the name of a function.

Use console.debug() and the name property of the passed method to log the method's name to the debug channel of the console.

const functionName = fn => (console.debug(fn.name), fn);
 
functionName(Math.max); // max (logged in debug channel of console)
+

memoize

Returns the memoized (cached) function.

Use Object.create(null) to create an empty object without Object.prototype (so that those properties are not resolved if the input value is something like 'hasOwnProperty'). Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not.

const memoize = fn => {
+  const cache = Object.create(null);
+  return value => cache[value] || (cache[value] = fn(value));
+};
+
// See the `anagrams` snippet.
+const anagramsCached = memoize(anagrams);
+anagramsCached('javascript'); // takes a long time
+anagramsCached('javascript'); // returns virtually instantly since it's now cached
 

runPromisesInSeries

Runs an array of promises in series.

Use Array.reduce() to create a promise chain, where each promise returns the next promise when resolved.

const runPromisesInSeries = ps => ps.reduce((p, next) => p.then(next), Promise.resolve());
 
const delay = d => new Promise(r => setTimeout(r, d));
 runPromisesInSeries([() => delay(1000), () => delay(2000)]); // //executes each promise sequentially, taking a total of 3 seconds to complete