Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

map

Lindsey Kuper edited this page · 13 revisions

Description

map applies an elemental function to every element in a ParallelArray and returns a new ParallelArray from the results.

Unlike combine, map does not take a depth argument to determine the number of dimensions the map operation iterates over. Instead, flatten can be used to collapse the outer dimensions of an array into a single dimension. A consecutive application of map then iterates over all previously collapsed dimensions. Finally, partition can be used to restore the original shape of the array. For example, instead of

var result = pa.map(2, f);

where 2 would be the depth argument, one can write

var tmp = pa.flatten();
var tmp = tmp.map(f);
var result = tmp.partition(pa.getShape()[0]);

Note that this approach does not work for combine: since combine exposes the iteration index to the elemental function, collapsing the iteration space to a single index would be observable from the elemental function.

Synopsis

myParallelArray.map(elementalFunction, arg1, arg2, ...)

Arguments

  • elementalFunction: described below.
  • arg1, arg2, ...: optional arguments, passed unchanged to elementalFunction.

Elemental Function

function(val, arg1, arg2, ...) { <body> }
  • val: An element from the original ParallelArray on which map is invoked.
  • arg1, arg2, ...: The same as the optional arguments passed to map.

The result of the elemental function is the array element to be placed in map's result at the same index position at which val appeared in the original array.

Inside the elemental function, the value of this will be the ParallelArray object on which map was invoked. For example, in the invocation of map above, this would refer to myParallelArray.

Returns

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.

Examples

// an identity function; `pa` is a ParallelArray
pa.map(function(val){ return val; })

// increment each element of a ParallelArray by 1
var source = new ParallelArray([1,2,3,4,5]);
var plusOne = source.map(function inc(v) { return v+1; });
Something went wrong with that request. Please try again.