-
Notifications
You must be signed in to change notification settings - Fork 0
reducePar
– 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:
- Let O be the result of calling ToObject passing the this value as the argument.
- ReturnIfAbrupt(O).
- Let lenValue be the result of Get(O, "length").
- Let len be ToLength(lenValue).
- ReturnIfAbrupt(len).
- If IsCallable(callbackfn) is false, throw a TypeError exception.
- If len is 0 throw a TypeError exception.
- If len is 1
- Let Pk1 be ToString(1)
- Let k1Present be the result of HasProperty(S, Pk1).
-
ReturnIfAbrupt(_k1Present_).
- Let result be the result of Get(O, Pk1).
- ReturnIfAbrupt(k1Value).
- Return result
-
Let _A_ be the result of the abstract operation ArrayCreate with argument 0
-
ReturnIfAbrupt(_A_)
-
Repeat in an arbitrary and implementation dependent order _len_-1 times
- Select 2 previously unselected indices, k1 and k2 from O or A
- Let Pk1 be ToString(k1).
- If k1 is in _O let S be O
- If k1 is in A let S be A
- Let k1Present be the result of HasProperty(S, Pk1).
- ReturnIfAbrupt(k1Present).
- Let Pk1 be ToString(k1).
- Let k1Value be the result of Get(S, Pk1).
- ReturnIfAbrupt(k1Value).
- Let P2 be ToString(k2).
- If k2 is in O let _S be O
- If k2 is in A let S be A
- Let k2Present be the result of HasProperty(S, Pk2).
- ReturnIfAbrupt(k2Present).
- Let Pk2 be ToString(k2).
- Let k2Value be the result of Get(S, Pk2).
- ReturnIfAbrupt(k2Value).
- 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.
- TBD check that this is what this should be…
- ReturnIfAbrupt(result).
- Push result onto A TBD make this more formal...
- Return result.
The length property of the reducePar method is 1.