From 37d2cb329211c2bdc5aa2ce66a5927934acc6e73 Mon Sep 17 00:00:00 2001 From: Brian Zengel Date: Fri, 18 Oct 2013 19:18:48 -0400 Subject: [PATCH] _.reduce/_.foldl/_.inject definitions --- lodash/lodash-tests.ts | 24 ++++++++++++++ lodash/lodash.d.ts | 71 +++++++++++++++++++++--------------------- 2 files changed, 60 insertions(+), 35 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index a5d8dab4a..16ce18ecf 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -279,6 +279,30 @@ result = _.map(stoogesAges, 'name'); result = _.collect({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; }); result = _.collect(stoogesAges, 'name'); +result = _.reduce([1, 2, 3], function(sum, num) { + return sum + num; +}); +result = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) { + result[key] = num * 3; + return result; +}, {}); + + result = _.foldl([1, 2, 3], function(sum, num) { + return sum + num; + }); + result = _.foldl({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) { + result[key] = num * 3; + return result; + }, {}); + + result = _.inject([1, 2, 3], function(sum, num) { + return sum + num; + }); + result = _.inject({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) { + result[key] = num * 3; + return result; + }, {}); + result = _.some([null, 0, 'yes', false], Boolean); result = _.some(foodsCombined, 'organic'); result = _.some(foodsCombined, { 'type': 'meat' }); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index a14519604..0a9d1e321 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -1247,6 +1247,42 @@ declare module _ { collection: List, pluckValue: string): TResult[]; + /** + * Reduces a collection to a value which is the accumulated result of running each + * element in the collection through the callback, where each successive callback execution + * consumes the return value of the previous execution. If accumulator is not provided the + * first element of the collection will be used as the initial accumulator value. The callback + * is bound to thisArg and invoked with four arguments; (accumulator, value, index|key, collection). + * @param collection The collection to iterate over. + * @param callback The function called per iteration. + * @param accumulator Initial value of the accumulator. + * @param thisArg The this binding of callback. + * @return Returns the accumulated value. + **/ + export function reduce( + collection: Collection, + callback: MemoIterator, + accumulator?: TResult, + thisArg?: any): TResult; + + /** + * @see _.reduce + **/ + export function inject( + collection: Collection, + callback: MemoIterator, + accumulator?: TResult, + thisArg?: any): TResult; + + /** + * @see _.reduce + **/ + export function foldl( + collection: Collection, + callback: MemoIterator, + accumulator?: TResult, + thisArg?: any): TResult; + /** * Checks if the callback returns a truey value for any element of a collection. The function * returns as soon as it finds a passing value and does not iterate over the entire collection. @@ -1330,41 +1366,6 @@ declare module _ { - - /** - * Also known as inject and foldl, reduce boils down a list of values into a single value. - * Memo is the initial state of the reduction, and each successive step of it should be - * returned by iterator. The iterator is passed four arguments: the memo, then the value - * and index (or key) of the iteration, and finally a reference to the entire list. - * @param list Reduces the elements of this array. - * @param iterator Reduce iterator function for each element in `list`. - * @param memo Initial reduce state. - * @param context `this` object in `iterator`, optional. - * @return Reduced object result. - **/ - export function reduce( - list: Collection, - iterator: MemoIterator, - memo?: TResult, - context?: any): TResult; - - /** - * @see _.reduce - **/ - export function inject( - list: Collection, - iterator: MemoIterator, - memo?: TResult, - context?: any): TResult; - - /** - * @see _.reduce - **/ - export function foldl( - list: Collection, - iterator: MemoIterator, - memo?: TResult, - context?: any): TResult; /** * The right-associative version of reduce. Delegates to the JavaScript 1.8 version of