Lindsey Kuper edited this page Feb 10, 2015 · 18 revisions


Like map, combine can be used to compute a new ParallelArray object by inspecting an existing ParallelArray object's elements, applying an elemental function to each of them, and returning a new ParallelArray containing the results of those function applications. Unlike map, the elemental function passed to combine is provided with access to the current index in the source array, along with a reference to the source array itself.


myParallelArray.combine(elementalFunction, arg1, arg2, ...)
myParallelArray.combine(depth, elementalFunction, arg1, arg2, ...)


  • depth (optional): the number of dimensions traversed to access an element in this. The default is 1.
  • elementalFunction: described below.
  • arg1, arg2, ...: optional arguments, passed unchanged to elementalFunction.

Elemental Function

function(index, arg1, arg2, ...) { <body> }
  • index: Location in combine's result array where the result of the elemental function is placed. Can be used as the first argument to get to retrieve source values.
  • arg1, arg2, ...: The same as the optional arguments passed to combine.

The result of the elemental function is the array element to be placed in combine's result at the location indicated by index.

Inside the elemental function, the value of this will be the ParallelArray object on which combine was invoked.


A freshly minted ParallelArray whose elements are the results of applying the elemental function to each element of the source array, plus any optional arguments.


// an identity function; `pa` is a ParallelArray
pa.combine(function(i){return this.get(i);})

// element-wise addition of two ParallelArrays `pa1` and `pa2`
pa1.combine(function(iv, pa2){return this.get(iv) + pa2.get(iv);}, pa2)

// addition of two ParallelArrays `p1` and `p2` of the same shape
pa1.combine(pa1.getShape().length, function(iv, pa2){return this.get(iv) + pa2.get(iv);}, pa2)

// increment each element of a ParallelArray by 1
var source = new ParallelArray([1,2,3,4,5]);
var plusOne = source.combine(function inc(i) { return this.get(i)+1; });

// reverse the elements in a ParallelArray
var source = new ParallelArray([1,2,3,4,5]);
var reverse = source.combine(function rev(i) { return this.get(this.length-i[0]-1); });