-
Notifications
You must be signed in to change notification settings - Fork 71
ParallelArray
The ParallelArray
is the central data structure around which River Trail programs are built. A ParallelArray
can be created either from an existing array-like object (such as a JavaScript Array
or another ParallelArray
), or by using a comprehension, as shown in the examples below.
ParallelArray
s are immutable: once created, they cannot be changed. Instead, operations such as map
return a new ParallelArray
.
ParallelArray();
ParallelArray(array);
ParallelArray(size, elementalFunction, arg1, arg2, ...);
ParallelArray(constructor, array);
ParallelArray(element0, element1, ...);
ParallelArray(canvas);
ParallelArray
objects can be created in a variety of ways, depending on the arguments passed to the ParallelArray
constructor.
-
No arguments: When called with no arguments, the constructor returns a
ParallelArray
with no elements. -
array: When called with an
array
argument, which can be any array-like object, and no other arguments, the constructor returns a newParallelArray
populated with the values inarray
. -
size, elementalFunction, arg1, arg2, ...: When called with a
size
and anelementalFunction
, and optional argumentsarg1
,arg2
, etc., the constructor returns aParallelArray
of sizesize
where each value is the result of callingelementalFunction
with the index where its result goes and any remainingarg1
,arg2
, ... arguments.elementalFunction
must be ofFunction
type. Ifsize
is a number, then the index passed to the elemental function will be a number, as well. To support the creating of multi-dimensional arrays,size
can be a vector. In that case, the index passed to the elemental function will be a vector, too. -
constructor, array: When called with an argument
constructor
of typeFunction
and an array-like objectarray
, construct a newParallelArray
populated with the values inarray
, but usingconstructor
as the constructor for theParallelArray
's internal data container. This form can be used to force aParallelArray
to internally represent its data as a typed array. -
element0, element1, ...: When called with multiple array elements as arguments, the constructor returns a new
ParallelArray
populated with those elements. -
canvas: When called with an HTML
<canvas>
object as argument, the constructor returns a newParallelArray
with shape[height, width, 4]
whereheight
andwidth
are the height and width of the canvas in pixels, and the third dimension represents the 4-byte RGBA value for each pixel.
To create a ParallelArray
whose first element is a function, one must use the elemental function form and have the elemental function return the function.
To create a ParallelArray
with a single element that is an Array
, one must use the elemental function form with a size of 1
and return the array from the elemental function.
A freshly minted ParallelArray
.
// create an empty ParallelArray with shape 0
var pa0 = new ParallelArray();
// create a ParallelArray out of a nested JS array, with shape [3, 2]
var pa1 = new ParallelArray([ [0,1], [2,3], [4,5] ]);
// create a ParallelArray from another ParallelArray
var pa2 = new ParallelArray(pa1);
// create a nested ParallelArray with shape [2, 2] from two arrays
var pa3 = new ParallelArray([0,1], [2,3]);
// create a ParallelArray using the comprehension constructor with
// shape [3, 2]
var pa4 = new ParallelArray(3, function (i) { return [i, i+1]; });
// create a ParallelArray with a shape vector with shape [3, 2]
var pa5 = new ParallelArray([3,2], function (iv) { return iv[0] * iv[1]; });
// create a ParallelArray from a canvas element
var pa6 = new ParallelArray(document.createElement("canvas"));
The following prototype methods on the Array
object are also implemented for ParallelArray
: