Skip to content
Richard L. Hudson edited this page Feb 24, 2014 · 1 revision

Array.prototype.reducePar ( callbackfn )

– TBD make sure depth of 1 is what we want here. --

callbackfn should be a function that takes two arguments. reducePar calls the callbackfn, as a function, using two elements selected from the original array or from elements produced by previous invocation of callbackfn. Each element is used as an argument to callbackfn once and only once. Each call to callbackfn will reduce the number of unused elements by 1 for an array of N elements we will invoke callbackfn N-1 times. The calls are repeated until only one element remains. This element is the result. It is a TypeError if the array contains no elements. reducePar is free to group calls to the callbackfn and reorder the calls. For a callbackfn that is associative the final result will be the same as reducing from left to right. If the callbackfn is not associative reducePar is only required to return a result consistent with some call ordering and is not required to choose the same call ordering on subsequent calls. Furthermore, reducePar does not magically resolve problems related to overflow and the well documented fact that some numbers, such as floating point numbers are not represented exactly in EcmaScript and the underlying hardware so addition and multiplication are not guarenteed to be associative in these cases.

reducePar does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn. Such mutation as well as any mutation that is visible to other calls to callbackfn will defeat the parallel optimization and a sequential schedule will be used. The range of array elements processed by reducePar is set before the first call to callbackfn. Elements that are appended to the array after the call to reducePar begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callbackfn will be the value at the time reducePar visits them; elements that are deleted after the call to reducePar begins and before being visited are not visited. Each of these conditions will defeat the parallel optimization and a sequential schedule will be used.

When the reducePar method is called with one argument, the following steps are taken:

  1. Let O be the result of calling ToObject passing the this value as the argument.
  2. ReturnIfAbrupt(O).
  3. Let lenValue be the result of Get(O, "length").
  4. Let len be ToLength(lenValue).
  5. ReturnIfAbrupt(len).
  6. If IsCallable(callbackfn) is false, throw a TypeError exception.
  7. If len is 0 throw a TypeError exception.
  8. If len is 1
    1. Let Pk1 be ToString(1)
    2. Let k1Present be the result of HasProperty(S, Pk1).
    3.    ReturnIfAbrupt(_k1Present_). 
      
    4. Let result be the result of Get(O, Pk1).
    5. ReturnIfAbrupt(k1Value).
    6. Return result
  9. Let _A_ be the result of the abstract operation ArrayCreate with argument 0
    
  10. ReturnIfAbrupt(_A_)
    
  11. Repeat in an arbitrary and implementation dependent order _len_-1 times
    
    1. Select 2 previously unselected indices, k1 and k2 from O or A
    2. Let Pk1 be ToString(k1).
    3. If k1 is in _O let S be O
    4. If k1 is in A let S be A
    5. Let k1Present be the result of HasProperty(S, Pk1).
    6. ReturnIfAbrupt(k1Present).
    7. Let Pk1 be ToString(k1).
    8. Let k1Value be the result of Get(S, Pk1).
    9. ReturnIfAbrupt(k1Value).
    10. Let P2 be ToString(k2).
    11. If k2 is in O let _S be O
    12. If k2 is in A let S be A
    13. Let k2Present be the result of HasProperty(S, Pk2).
    14. ReturnIfAbrupt(k2Present).
    15. Let Pk2 be ToString(k2).
    16. Let k2Value be the result of Get(S, Pk2).
    17. ReturnIfAbrupt(k2Value).
    18. Let result be the result of calling the Call internal method of callbackfn with undefined as thisArgument and a list containing k1Value, k2Value as argumentsList.
      1. TBD check that this is what this should be…
    19. ReturnIfAbrupt(result).
    20. Push result onto A TBD make this more formal...
  12. Return result.

The length property of the reducePar method is 1.

Clone this wiki locally