mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-09 11:13:57 +08:00
added _.memoize definition
This commit is contained in:
+17
-12
@@ -469,6 +469,23 @@ result = <number>_.defer(function() { console.log('deferred'); });
|
||||
var log = _.bind(console.log, console);
|
||||
result = <number>_.delay(log, 1000, 'logged later');
|
||||
|
||||
var fibonacci = _.memoize(function(n) {
|
||||
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
|
||||
});
|
||||
|
||||
var data = {
|
||||
'moe': { 'name': 'moe', 'age': 40 },
|
||||
'curly': { 'name': 'curly', 'age': 60 }
|
||||
};
|
||||
|
||||
var stooge = _.memoize(function(name) { return data[name]; }, _.identity);
|
||||
stooge('curly');
|
||||
|
||||
stooge['cache']['curly'].name = 'jerome';
|
||||
stooge('curly');
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
//WHAT'S LEFT
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -486,18 +503,6 @@ result = <number>_.delay(log, 1000, 'logged later');
|
||||
|
||||
|
||||
|
||||
// var buttonView = {
|
||||
// label: 'underscore',
|
||||
// onClick: function () { alert('clicked: ' + this.label); },
|
||||
// onHover: function () { console.log('hovering: ' + this.label); }
|
||||
// };
|
||||
// _.bindAll(buttonView);
|
||||
// $('#underscore_button').bind('click', buttonView.onClick);
|
||||
|
||||
var fibonacci = _.memoize(function (n) {
|
||||
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
|
||||
});
|
||||
|
||||
var updatePosition = () => alert('updating position...');
|
||||
var throttled = _.throttle(updatePosition, 100);
|
||||
$(window).scroll(throttled);
|
||||
|
||||
Vendored
+13
-17
@@ -1845,8 +1845,19 @@ declare module _ {
|
||||
wait: number,
|
||||
...args: any[]): number;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates a function that memoizes the result of func. If resolver is provided it will be
|
||||
* used to determine the cache key for storing the result based on the arguments provided to
|
||||
* the memoized function. By default, the first argument provided to the memoized function is
|
||||
* used as the cache key. The func is executed with the this binding of the memoized function.
|
||||
* The result cache is exposed as the cache property on the memoized function.
|
||||
* @param func Computationally expensive function that will now memoized results.
|
||||
* @param resolver Hash function for storing the result of `fn`.
|
||||
* @return Returns the new memoizing function.
|
||||
**/
|
||||
export function memoize(
|
||||
func: Function,
|
||||
resolver?: (n: any) => string): Function;
|
||||
|
||||
|
||||
|
||||
@@ -1918,21 +1929,6 @@ declare module _ {
|
||||
fn: Function,
|
||||
...arguments: any[]): Function;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @param fn Computationally expensive function that will now memoized results.
|
||||
* @param hashFn Hash function for storing the result of `fn`.
|
||||
* @return Memoized version of `fn`.
|
||||
**/
|
||||
export function memoize(
|
||||
fn: Function,
|
||||
hashFn?: (n: any) => string): Function;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates and returns a new, throttled version of the passed function, that, when invoked repeatedly,
|
||||
* will only actually call the original function at most once per every wait milliseconds. Useful for
|
||||
|
||||
Reference in New Issue
Block a user