From eb1de7e5af2d556a73bb02c87abdb126f3325a22 Mon Sep 17 00:00:00 2001 From: Brian Zengel Date: Fri, 18 Oct 2013 23:48:08 -0400 Subject: [PATCH] added _.memoize definition --- lodash/lodash-tests.ts | 29 +++++++++++++++++------------ lodash/lodash.d.ts | 30 +++++++++++++----------------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index b10f9e84e..c2cb46646 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -469,6 +469,23 @@ result = _.defer(function() { console.log('deferred'); }); var log = _.bind(console.log, console); result = _.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 = _.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); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 8712be050..4c0dd7a4a 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -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