Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Unified Diff: src/array.js

Issue 88022: Add Array.prototype.reduce and .reduceRight. (Closed)
Patch Set: Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/messages.js » ('j') | test/mjsunit/array-reduce.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/array.js
diff --git a/src/array.js b/src/array.js
index d30a98960572726031959a3fd02e1c601e074007..9560466316a612ceb7f9f7b7842aaf2d54b4b1cd 100644
--- a/src/array.js
+++ b/src/array.js
@@ -879,6 +879,62 @@ function ArrayLastIndexOf(element, index) {
}
+function ArrayReduce(callback, current) {
+ if (!IS_FUNCTION(callback)) {
+ throw MakeTypeError('called_non_callable', [ callback ]);
+ }
+ // Pull out the length so that modifications to the length in the
+ // loop will not affect the looping.
+ var length = this.length;
+ var i = 0;
+
+ find_initial: if (%_ArgumentsLength() < 2) {
Dean McNamee 2009/04/21 09:19:50 I am missing why you need the label.
Christian Plesner Hansen 2009/04/21 09:39:17 I think the code style is find_initial: if (%_
Lasse Reichstein 2009/04/21 09:53:50 I use the label to have a location to break to, to
Lasse Reichstein 2009/04/21 09:53:50 Done.
+ for (;i < length; i++) {
Dean McNamee 2009/04/21 09:19:50 space after ;
Lasse Reichstein 2009/04/21 09:53:50 Done.
+ current = this[i];
+ if (!IS_UNDEFINED(current) || i in this) {
+ i++;
+ break find_initial;
+ }
+ }
+ throw MakeTypeError('reduce_no_initial', []);
+ }
+
+ for (; i < length; i++) {
+ var element = this[i];
+ if (!IS_UNDEFINED(element) || i in this) {
+ current = callback.call(null, current, element, i, this);
+ }
+ }
+ return current;
+}
+
+function ArrayReduceRight(callback, current) {
+ if (!IS_FUNCTION(callback)) {
+ throw MakeTypeError('called_non_callable', [ callback ]);
Dean McNamee 2009/04/21 09:19:50 no spaces around [ ] (consistency with [])
Lasse Reichstein 2009/04/21 09:53:50 Done.
+ }
+ var i = this.length - 1;
+
+ find_initial: if (%_ArgumentsLength() < 2) {
+ for (;i >= 0; i--) {
+ current = this[i];
+ if (!IS_UNDEFINED(current) || i in this) {
+ i--;
+ break find_initial;
+ }
+ }
+ throw MakeTypeError('reduce_no_initial', []);
+ }
+
+ for (; i >= 0; i--) {
+ var element = this[i];
+ if (!IS_UNDEFINED(element) || i in this) {
+ current = callback.call(null, current, element, i, this);
+ }
+ }
+ return current;
+}
+
+
// -------------------------------------------------------------------
@@ -917,7 +973,9 @@ function SetupArray() {
"every", ArrayEvery,
"map", ArrayMap,
"indexOf", ArrayIndexOf,
- "lastIndexOf", ArrayLastIndexOf
+ "lastIndexOf", ArrayLastIndexOf,
+ "reduce", ArrayReduce,
+ "reduceRight", ArrayReduceRight
));
// Manipulate the length of some of the functions to meet
@@ -930,7 +988,9 @@ function SetupArray() {
ArrayMap: 1,
ArrayIndexOf: 1,
ArrayLastIndexOf: 1,
- ArrayPush: 1
+ ArrayPush: 1,
+ ArrayReduce: 1,
+ ArrayReduceRight: 1
});
}
« no previous file with comments | « no previous file | src/messages.js » ('j') | test/mjsunit/array-reduce.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698