export to javascript a program using collections, iterator, etc..

edited February 2014 in JavaScript Mode

Hi

I have a program in processing that uses the libraries Collections, Iterator, Hashset, Comparator, and therefore it does not run in JS. Is there a possibility to use similar libraries that would work in JS? Or do I have to replace all "by hand"?

Answers

  • edited February 2014

    Anything not in http://processing.org/reference/ is bound to fail in JS Mode! [..]

    • Instead of Iterator, use enhanced for loop.
    • HashSet is nothing more than a class which checks whether a value already exists before inserting. Do it manually!
    • I believe you're using Collections for its sort() method w/ Comparator, right?
    • I dunno how to do sorting in JS yet! My advice is to use CoffeeScript mode in order to use both Java & JS modes' APIs!
  • edited February 2014

    Ok thank you I will try that. Yes i'm using collections with comparator. The truth is that I'm using the library rtiangulate which uses all these libraries.

  • HashSet is actually implemented with a simple HashMap in Java, so you can do the same in PJS.

  • edited February 2014

    Wouldn't be that difficult to include HashSet & LinkedList &/or ArrayDeque officially in Processing? [-O<
    Along w/ Comparator, Comparable, Iterator, Iterable? 8->

    Many sketches are stuck in Java Mode due to lack of things that are natively available in JS, but in another incompatible syntax!
    If JS Mode devs were able to push HashMap into JS, some other useful classes/interfaces wouldn't be that difficult, right? :-\"

  • Example of using a hash map as a hash set:

    http://jsfiddle.net/PhiLho/V8psX/1/

  • Honestly, LinkedList and ArrayDeque are rarely used, and can be simulated with an ArrayList. PJS maintainer won't recreate the whole Collection API!

    Comparator & Compable are more useful, indeed. Iterator isn't much used in modern Java, since the for-each syntax is supported. Iterable is mostly useful to people doing custom collections, which is rare, particularly by newbies. Experienced users know how to workaround the JS limitations, I suppose.

    I would appreciate to have support for List, Map and Set (as synonyms of their implementations), too.

  • We can sort using JavaScript's Array.sort([comparator]), but calling JS from PJS is quite tricky, I think, particularly if we need to pass a PJS function! So a good implementation of sort() compatible with ArrayList and any class would be great.

  • edited February 2014

    I would appreciate to have support for List, Map and Set (as synonyms of their implementations), too.

    JS doesn't care which interface/class we've declared variables with! So this line is valid in JS:

    static final List<PVector> vectors = new ArrayList();
    

    For Processing 2+ we'd need to import java.util.List in order for that to work. While JS is cool either way! :-bd

    PJS maintainer won't recreate the whole Collection API!

    Of course not. But there are many interfaces/classes that can be abstract away.
    For example, Vector is a slower ArrayList version. It's as easy to alias that into JS for its conversion to work! \m/

    LinkedList and ArrayDeque are rarely used, and can be simulated with an ArrayList.

    But an ArrayList isn't the ideal due to its big left-shifts when removing entries.
    And a native JS array has a range of methods very similar to a LinkedList.
    Why not make ArrayDeque, and any other classes which implement Deque, alias to 1 structure appropriate for such operations?!

  • "But an ArrayList isn't the ideal due to its big left-shifts when removing entries."
    Well, ArrayDeque is efficient only when removing a first or last entry, otherwise, delete(int i) just does System.arraycopy like ArrayList!

    I wouldn't encourage to support Vector and Hashtable, as it would encourage to use them in Java! :-)

  • edited November 2014

    Well, ArrayDeque is efficient only when removing a first or last entry,
    otherwise, remove(int i) just does System.arraycopy() like ArrayList!

    I really don't get why you're complaining about that a Deque would lose its efficacy if remove() is used w/ index! @-)
    The whole idea of queues & stacks is to add() & remove() by their end points!!! Otherwise we'd use a List or something else!

    Besides, ArrayDeque doesn't allow indexed remove() or peek() operations anyways! [-X
    A LinkedList allows such, but w/ the highest time cost of any structure! [..]

    I wouldn't encourage to support Vector and Hashtable, as it would encourage to use them in Java!

    I'm talking about flexible conversion rates from Java to JS.
    If something is as easy as placing an alias for a class, why refuse to do so?

    We can advice not to use them. No need for cheap censorship! Only technical ground reasons for no implementation! :-B
    Many times deprecated classes are still used for easy & fast porting of old codes! :-&

    And in JS's point-of-view, a Vector & an ArrayList would end up being 1 and the same anyways! >:P

  • Back to more down-to-earth subjects :-D

    I indeed handled iterator and HashSet as suggested by GoToLoop, and I did a sort method by hand, I copy the code below in case it interests anyone.

    My last problems concern the core.PApplet class. The function uses EPSILON, abs and sqrt, and JS says "PApplet is not defined". Any idea how to overcome this?

    code of the method sorting an Array List of pvectors according to their x values:

    ArrayList<PVector> xsort(ArrayList<PVector> p) {
    
      int m=p.size();
      int i=0;
    
      PVector temp=new PVector(0, 0);
    
      while (i<m-1) {  
        if (p.get(i).x>p.get(i+1).x) { 
    
          //   switch(i,i+1); 
          temp=p.get(i);
          p.set(i, p.get(i+1));
          p.set(i+1, temp);
    
          i=max(0, i-1);
        }
        else { 
          i++;
        };
      }
      return p;
    }
    
  • edited February 2014

    My last problems concern the core.PApplet class. The function uses EPSILON, abs and sqrt, and JS says "PApplet is not defined".

    It doesn't make any sense! Such errors happen only when we use separate ".java" or ".js" tabs! :-??
    If a function is within the confines of the Processing's class, all should work seamlessly! O:-)

    Go to http://www.openprocessing.org/sketch/create and paste the code below there:

    static final float myFunction (float f) {
      return sqrt(abs(f*EPSILON));
    }
    
    void setup() {
      println(myFunction(random(100)));
      exit();
    }
    
  • Hi all, Funny I was looking for exactly the same thing, which is having the Triangulate work in JS. After following every piece of advice you provided, I still couldn't have my sketch run in JS mode 3:-O What is even more troubling is that for some people, for some reason, there doesn't seem to be a problem - at all! See http://www.openprocessing.org/sketch/117808 for example. But this is where it gets even better: this online sketch works perfectly fine on my computer. But after downloading it, I can't run in local JS mode (Java mode works fine mind you). For the sake of it, I even tried to upload this sketch but this wouldn't work either. Same thing happens with this sketch http://www.openprocessing.org/sketch/10728 =)) this is me getting crazy, not me getting happy

    So @rafalo did you eventually solve this issue? And do you guys have any hints on how I could have any Triangulate class work in JS mode?

  • @oggy, you should create your own forum thread for your Triangulate!
    But I'm afraid if you use things like Comparator, JS Mode won't be able to transpile it to JS!
    I advise you to write your sketch in P5.JS or in CoffeeScript mode!

Sign In or Register to comment.