_.reduce/_.foldl/_.inject definitions

This commit is contained in:
Brian Zengel
2013-10-18 19:18:48 -04:00
parent 4060e36cb3
commit 37d2cb3292
2 changed files with 60 additions and 35 deletions
+24
View File
@@ -279,6 +279,30 @@ result = <any[]>_.map(stoogesAges, 'name');
result = <any[]>_.collect({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
result = <any[]>_.collect(stoogesAges, 'name');
result = <any>_.reduce([1, 2, 3], function(sum, num) {
return sum + num;
});
result = <any>_.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
result[key] = num * 3;
return result;
}, {});
result = <any>_.foldl([1, 2, 3], function(sum, num) {
return sum + num;
});
result = <any>_.foldl({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
result[key] = num * 3;
return result;
}, {});
result = <any>_.inject([1, 2, 3], function(sum, num) {
return sum + num;
});
result = <any>_.inject({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
result[key] = num * 3;
return result;
}, {});
result = <boolean>_.some([null, 0, 'yes', false], Boolean);
result = <boolean>_.some(foodsCombined, 'organic');
result = <boolean>_.some(foodsCombined, { 'type': 'meat' });
+36 -35
View File
@@ -1247,6 +1247,42 @@ declare module _ {
collection: List<T>,
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<T, TResult>(
collection: Collection<T>,
callback: MemoIterator<T, TResult>,
accumulator?: TResult,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
export function inject<T, TResult>(
collection: Collection<T>,
callback: MemoIterator<T, TResult>,
accumulator?: TResult,
thisArg?: any): TResult;
/**
* @see _.reduce
**/
export function foldl<T, TResult>(
collection: Collection<T>,
callback: MemoIterator<T, TResult>,
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<T, TResult>(
list: Collection<T>,
iterator: MemoIterator<T, TResult>,
memo?: TResult,
context?: any): TResult;
/**
* @see _.reduce
**/
export function inject<T, TResult>(
list: Collection<T>,
iterator: MemoIterator<T, TResult>,
memo?: TResult,
context?: any): TResult;
/**
* @see _.reduce
**/
export function foldl<T, TResult>(
list: Collection<T>,
iterator: MemoIterator<T, TResult>,
memo?: TResult,
context?: any): TResult;
/**
* The right-associative version of reduce. Delegates to the JavaScript 1.8 version of