From 650d49286d517f832184a78e23f4a13986fe9d40 Mon Sep 17 00:00:00 2001 From: tjcrowder Date: Mon, 7 Sep 2009 15:40:24 +0100 Subject: [PATCH] doc: merged (and trimmed) old docs for Enumerable#inject. --- src/lang/enumerable.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/lang/enumerable.js b/src/lang/enumerable.js index 158772c..d819f09 100644 --- a/src/lang/enumerable.js +++ b/src/lang/enumerable.js @@ -419,10 +419,33 @@ var Enumerable = (function() { /** * Enumerable#inject(accumulator, iterator[, context]) -> accumulatedValue + * - accumulator (Object): The initial value to which the `iterator` adds. + * - iterator (Function): An iterator function used to build the accumulated + * result. + * - context (Object): An optional object to use as `this` within + * calls to the iterator. * * Incrementally builds a result value based on the successive results - * of the iterator. - * This can be used for array construction, numerical sums/averages, etc. + * of the iterator. This can be used for array construction, numerical + * sums/averages, etc. + * + * The `iterator` function is called once for each element in the + * enumeration, receiving the current value of the accumulator as its first + * argument, the element as its second argument, and the element's index as + * its third. It returns the new value for the accumulator. + * + * ### Examples + * + * $R(1,10).inject(0, function(acc, n) { return acc + n; }); + * // -> 55 (sum of 1 to 10) + * + * ['a', 'b', 'c', 'd', 'e'].inject([], function(string, value, index) { + * if (index % 2 === 0) { // even numbers + * string += value; + * } + * return string; + * }); + * // -> 'ace' **/ function inject(memo, iterator, context) { this.each(function(value, index) {