Processing should define Array just like it has Vector

Working with 2-dimensional is a very common scenario in creative coding.

Processing should define an Array and how it can be used (methods?) - just like it has with Vector.

The level of support without Processing mandating it is very varied - with Javascript not supporting 2-d or n-d arrays cleanly, and python only doing so via numpy. I don’t know what Java does.

Once Processing has defined this capability as part of the language specification, then the javascript, python, java, and other implementations must also support Arrays.

There are two key reasons for doing this, and they apply just as equally to Vector:

  1. Ease of use and accessibility for newcomers. (telling them to implement their own in javascript using lists of lists is not good)
  2. Conistency across implementations for a very very common requirement for creative / generative coding.

Some thought should go into the design of the API / methods to ensure the feature is consistent, logical, easy to use, and minimises cognitive load. Make the easy things easy.

For those arguing against, I would welcome an explanation of why Vector was defined but 2-d (or n-d) Array shouldn’t.

1 Like

I’d have loved to see some more concrete examples from your side, on how you’d imagine a potential implementation to look like. Would help kick-start the conversations.

As someone with no background in CS, I understand PVector to be a custom helper class made to simplify the manipulation of vectors and thus make them more accessible/easier to digest for novices. Without having any idea of the technical ins and outs and philosophical consequences for the language, I see no reason why some PArray class couldn’t also exist alongside the current array implementation. It could certainly make the use of arrays more approachable.

But how deep would that “userfrindlyficaiton” go?

I, for example, have tripped over the arrays indexing convention – starting at 0 – wayyyyy to often in my early days of writing code. Many a frustrating IndexOutOfBound was thrown. So would you also expect such an ingrained convention to be “rectified”? Because that convention, even though anchored in programming history, is just zero user-friendly.

1 Like

A simple example of a function that creates a 2D-array of PVector instances:

final PVector[][] pvArr2d = createPVecArr2d(5, 3); // 5 x 3

void setup() {
  for (final PVector[] pvArr1d : pvArr2d) println(pvArr1d);
  exit();
}

static final PVector[][] createPVecArr2d(final int rows, final int cols) {
  final PVector[][] arr2d = new PVector[rows][cols];

  for (final PVector[] arr1d : arr2d) for (int i = 0; i < arr1d.length; )
    arr1d[i++] = new PVector();

  return arr2d;
}

[ 0.0, 0.0, 0.0 ] [ 0.0, 0.0, 0.0 ] [ 0.0, 0.0, 0.0 ]
[ 0.0, 0.0, 0.0 ] [ 0.0, 0.0, 0.0 ] [ 0.0, 0.0, 0.0 ]
[ 0.0, 0.0, 0.0 ] [ 0.0, 0.0, 0.0 ] [ 0.0, 0.0, 0.0 ]
[ 0.0, 0.0, 0.0 ] [ 0.0, 0.0, 0.0 ] [ 0.0, 0.0, 0.0 ]
[ 0.0, 0.0, 0.0 ] [ 0.0, 0.0, 0.0 ] [ 0.0, 0.0, 0.0 ]

3 Likes

it should be as simple as:

create
var arr = new Array(5,5);

access
var x = arr[3,2];

update
arr[2,2] = 5;

Other possible useful methods could be: max, min, sum, … or we could do what numpy does and allow arithmetic operations to apply element-wise two arrays. Additional separate libraries could then implement FFTs, convolution etc … but that would not get in the way of the simple clear basics suitable for beginners.

Given P5 is primarily used to create designs, visualising arrays should be part of definition. eg

plot
arr.plot(location_x, location_y)

… with additional options for scale and colour mapping, now we’re getting into to more advanced optional functions. This proposal is about getting the foundations right.

I don’t think the zero-indexing thing is a major issue, but others may have more informed views on that. Processing’s top-left as the origin causes more problems than 0-indexing in my 5+ years experience of teaching 7-17 year olds to code.

For me, creating a 2d array in javascript is particularly unpleasant, complicated, and even the “one-liners” require advanced incantations.

I suggest that you rename this discussion to

p5.js should define Array …

You can see that some responders are thinking you mean Processing Java mode.

Multi-dimensional arrays are not native to Javascript or Java so syntax like

is not possible because it would mean rewriting the Javascipt or Java language.

3 Likes

I definitely do mean that Processing should define it, not p5js.

For example, Processing defines rectangle and ellipse and line and Vector, whether or not the individual implementation languages have these or not.

Another example, p5js doesn’t have Vector. Processing defines it so the javascript implementation of processing must therefore implement Vector too.

When Processing was first created it was a basic programming IDE that enabled non-programmers, graphics artists etc. an easy to introduction to creating computation art / graphics. It did not create a new language rather it provided a wrapper for Java that hid many of its complexities, in particular OO (object orientated) syntax and semantics.

So effectively it had a single mode “Java” and became thought of as a new language, the “Processing language”. Today Processing is an IDE that supports multiple modes / languages.

Available modes:

Although there is similarity between Java mode and p5js it is unrealistic for them to have an identical API since the underlying languages have different syntax and semantics…

Actually the Java mode vector is called PVector and Javascript does have a vector class p5.Vector. Personally I would refute the need for an identical API because both implementations would sink to the lowest common denominator.

Having said all that the question arises as to whether Java mode and p5js should provide an implementation of a 2D or even nD array. I think it would make a nice addition but the API would need careful planning because once released it would be problematic to change its implementation / API.

1 Like

There’s the original Processing.JS lib, which strives for API compatibility w/ Processing’s Java:
https://GotoLoop.GitHub.io/processing-js.github.io/reference/

2 Likes

What is the process for making a formal proposal for Processing to define and mandate Array?

Is there a process similar to a PEP for Python?

I’ve edited the title back to Processing not p5 because that is the central idea of this post.

AFAIK you post an ‘enhancement’ request on the appropriate repository issues page.

For Processing Java mode click here
For p5.js click here

I think you will find that each mode has it’s own development team, so if the enhancement is accepted by one team it doesn’t mean that the other team will follow suit.

1 Like

As a prototype you can just write your own class
implementing the commands you are looking for.

I did that for Table and PVector.

2 Likes

So it seems the proposal needs to first recommend Processing is a framework / standard / API definition which all implementations must conform to.

Where does that discussion need to happen?

I have no idea.

Hopefully another forum member does and responds here. :smile:

Hi @project_2501 and thanks for your question.

As quark mentioned, p5.js and Processing have distinct development paths. This is reflected in the p5.js README:

p5.js was created by Lauren Lee McCarthy in 2013 as a new interpretation of Processing for the context of the web. Since then, we have allowed ourselves space to deviate and grow, while drawing inspiration from Processing and our shared community.

Though both Processing and p5.js are supported by the Processing Foundation, each project has its own unique goals, values, and priorities, and uniformity across the ecosystem isn’t a current objective. Other active projects like py5 also implement the Processing API in their own unique way.

Any further discussions would need to happen within the respective development communities on their issue pages.

5 Likes

On the other hand, 2D-arrays are not too hard to handle.

Here is an example.



int[][] grid = new int [6][12];

size(700, 700);
background(0);

// ----------------------------------------------------------------

// make grid
for (int x = 0; x < 6; x++) {
  for (int y = 0; y < 6; y++) {
    grid[x][y] = int(random (99));
  }
}

// ----------------------------------------------------------------

// display grid
// step 1: upper und left numbers
for (int x = 0; x < 6; x++)
  text (x, x * 22 +33, 14);
for (int y = 0; y < 6; y++)
  text (y, 13, y * 22 +40);

// step 2: line --->
{
  int y=0;
  stroke(255);
  line (
    13, y * 22 +20,
    13+140, y * 22 +20);
}
// step 3: line | downwards
{
  int x=0;
  stroke(255);
  line (
    x * 22 +23, 14,
    x * 22 +23, 154);
}

// step 4: grid (main part)
for (int x = 0; x < 6; x++) {
  for (int y = 0; y < 6; y++) {
    text (grid[x][y], x * 22 +33, y * 22 +40);
  }
}

// ----------------------------------------------------------------

// show one position
int x = 4;
int y = 5;
int oneValue = grid[x][y];
//
text ("Position "
  + x
  + ", "
  + y
  + " :\n"
  + oneValue,
  x * 22 +310, y * 22 +330);
//
// ----------------------------------------------------------------
//

Even when you had a proper class encapsulating the 2D-Array you would still have commands like

gridTools.make(17,24); 
gridTools.get(4,5); 
gridTools.set(4,5); 
gridTools.display(110,200); 
gridTools.save("grid1.csv");
1 Like

Hi Chrisir - I’m pleased 2d-arrays are easy for you.

But for many young coders, and adults very new to coding, what you describe is too much of a barrier - I speak from over 5 years experience teaching both of these groups.

One of the central pillars of the Processing Foundation was inclusivity and accessibility to newcomers and non-technical artists - and that is the only reason I raised my suggestion to make arrays easier.

1 Like

Thanks sableRaph.

You have kindly explained the reality.

It means the Processing Foundation doesn’t work as I had expected or hoped.

I’m not my proposal will go anywhere as it depended on the idea of the Foundation mandating how Processing implementations worked.

Thanks anyway

1 Like
1 Like

@project_2501 it isn’t clear to me that the syntax arr[3, 2] does much to simplify the native arr[3][2]. As others have mentioned, the syntax arr[3, 2] isn’t possible without redefining the Java and JavaScript languages, or else adding some serious hacks to the PDE and p5.js Editor. Operator overloading, as in 5 * arr or arr1 + arr2, is also off the table without dark magic.

ND4J, TensorFlow.js, and d3-array might be good sources of inspiration for addon libraries.

3 Likes