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 |
}); |
} |