processing / p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
http://p5js.org/
GNU Lesser General Public License v2.1
21.49k stars 3.3k forks source link

Data objects #422

Closed shiffman closed 7 years ago

shiffman commented 9 years ago

@benfry and I were just talking about the data classes from Processing like IntDict, FloatDict, StringList etc in JS and we came to a conclusion that it would be nice to implement some of these for p5. I am thinking:

Obviously without types in JavaScript there can just be a Dict and List, however, I think it would be nice to separate out Numbers and Strings. For example, there are enough API differences and reference-wise it would be confusing for it to be all in one. If you want a more advanced list or dictionary with multiple data types in JS then you would implement it yourself with native JS arrays and/or objects. But these would be simple ways to do quick operations on lists and dictionaries with basic data types. This aligns with Processing: IntDict for simple dictionary, HashMap from Java for more advanced.

@therewasaguy am I correct that these have not been started yet? I am very happy to take on working on these (likely after the ITP semester) and thought it might be nice to start the discussion here.

Thoughts?

GoToLoop commented 9 years ago

I'm all in favor. But please, make it high performance. For example, the internal array shouldn't be resized all the time, but pre-allocated to some reasonable size like Java does for an ArrayList! :D

GoToLoop commented 9 years ago

Perhaps it'd be better if NumberList be split into IntList & FloatList, just like Processing 2.

IntList would be based on Int32Array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array

And FloatList on Float32Array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array

I'd also be interested in faster ArrayList & ArrayDeque data structures for p5.js! :smile_cat: At least Map, WeakMap, Set & WeakSet already exist in JS: :joy_cat:

therewasaguy commented 9 years ago

@shiffman sounds great! I haven't worked on these, only on the Table class and Table Row. Maybe there will be insights from this that can be applied to Table, too. I like the idea of IntList and FloatList using typed arrays the way @GoToLoop suggests.

benfry commented 9 years ago

Typed arrays are likely to be slower, definitely check before getting too far into that. They're built for moving a lot of fixed binary data around, not for quick random read/write of list elements, the way that Int/FloatList work.

shiffman commented 9 years ago

I completed a first pass over NumberList in https://github.com/shiffman/p5.js/tree/dataobjects. It's not entirely clear to me whether this is entirely needed given how much functionality there is in a native JS array, but it's a good learning process for me.

@lmccart, do you think I should implement all four objects (NumberList, StringList, NumberDict, StringDict) before submitting a pull request or might it be worth bringing NumberList in with an example or two as a trial run?

Also as a follow-up to previous discussion I don't see tremendous value in separating out ints from floats at the moment, but feel free to let me know if you disagree.

shiffman commented 8 years ago

We are teaching the topic of "data" at ITP next week. One of the examples I typically demonstrate is simple "word counting." Processing has IntDict which makes this sort of task easy. JS objects can be used as dictionaries with the key being the word and the value being the count. I have examples of this for my A to Z class.

Should I implement a NumberDict (better name?) for p5.js or shall I just make an example that uses a JS object? @lmccart

lmccart commented 8 years ago

I think this makes sense. The four you list in the first post seem reasonable to me, not that you need to implement them all right now. :) If you'd like to take a pass at NumberDict, that'd be fantastic.

shiffman commented 8 years ago

Awesome, I'll work on implementing NumberDict for this week's ICM and port my Learning Processing examples. Part of me is wondering if we should just have a single Dictionary object and let the typing be flexible. But let's start this way for now and see how it goes.

lmccart commented 8 years ago

Talking about this some more today, we realized the Lists are probably not necessary. The question remains would it be useful to make (1) a NumberDict and a StringDict, (2) just a Dictionary object, or (3) nothing at all and rely on native JS objects.

Our intuition is that JS objects can be a bit confusing for the beginner and a dictionary object of some kind is very useful for word counting, a sort of prototypical creative coding exercise. So probably we want option 1 or 2.

A starting place would be to review @shiffman's code examples:

More context and use cases can be found in Learning Processing section 18-7 (text analysis), pgs 402-404: http://learningprocessing.com/pdfs/LearningProcessing_2ed_sample_1and18.pdf

lmccart commented 7 years ago

@shiffman did a NumberDict ever get created? do these two dicts still feel useful having done more examples and testing? if so, perhaps @mlarghydracept, you might take a look?

benfry commented 7 years ago

Some background about these implementations, because we waited a long time before implementing them and including them in the core.

Don't underestimate the utility of these classes, even the lists. Teaching someone to sort an array alphabetically (a sorting function? what do you mean there are two elements to compare? localeCompare()?) is an unnecessary evil to just do basics that beginners want to cover at the outset. It's why we opted to include these instead of just making people use ArrayList on the Java side.

Same goes for lists of numbers, why do you have to implement a sorting function that returns a negative number, zero, or a positive number? How do I reverse the list? These are useful CS concepts to learn later (when more invested, or when sorting in more esoteric ways), but the implementation is an artifact of the way commonly used sorting algorithms work, not a basic conceptual building block that's important for understanding other classes of problems.

lmccart commented 7 years ago

thanks for the background @benfry, very helpful. that all makes sense.

shiffman commented 7 years ago

Apologies for missing this github thread in May. I did, in fact, implement a version of NumberDict. You can see a first pass at it here in case it's helpful:

https://github.com/shiffman/LearningProcessing-p5.js/blob/master/chp18_data/example_18_05_concordance/p5.NumberDict.js

I'm really excited to follow @mlarghydracept's work in this area. I'll chime in at #1974 if I can be of any help.

stalgiag commented 7 years ago

@shiffman Thanks for the link! This will definitely be helpful. There are a lot of great ideas in this version.

lmccart commented 7 years ago

hurrah!!