ArrayList of function calls

edited November 2017 in Questions about Code

Is it possible to create an ArrayList of function calls? For example, I have

void step0(){}
void step1(){}
void step2(){}
(...)
void stepN(){}

Then to create an

ArrayList<void> myFxn = new ArrayList<void>();
myFxn.add(step0);
myFxn.add(step1);
myFxn.add(step2);
myFxn.add(stepN);

Then finally

void keyPressed(){
  if(key>='a' && key<='z')
     myFxn.get(key-'a');
}

The ArrayList has a list of functions I could access in an ordered manner. Just curious if this is something that can be implemented with current tools.

Kf

Answers

  • edited November 2016 Answer ✓

    In Java 8, JS, Python, etc., YES! However, even though P3 is bundled w/ Java 8, the way the compilation is configured and also the incomplete pre-compiler makes the PDE incompatible w/ Java 8's latest features! =((

  • Answer ✓

    There's still hope. A quick dirty approach is to rely on undocumented function method(): *-:)

    // forum.Processing.org/two/discussion/19316/
    // arraylist-of-function-calls#Item_2
    
    // GoToLoop (2016-Nov-26)
    
    void setup() {
      final int rnd = (int) random(3);
      method("step" + rnd);
      exit();
    }
    
    void step0() {
      println("step0");
    }
    
    void step1() {
      println("step1");
    }
    
    void step2() {
      println("step2");
    }
    
  • Thxs. Do you mind showing a piece of code or a link showing the concept please @GoToLoop.

    Kf

  • It could be done with the current version of Processing (JAVA mode) but it is challenging and uses the Java Reflection API.

  • edited November 2016 Answer ✓

    A more advanced technique is to create an interface or class as a wrapper for your custom functions:

    // forum.Processing.org/two/discussion/19316/
    // arraylist-of-function-calls#Item_5
    
    // GoToLoop (2016-Nov-26)
    
    import java.util.List;
    static final int QTY = 3;
    final List<Step> steps = new ArrayList<Step>(QTY);
    
    void setup() {
      noLoop();
    
      for (int i = 0; i < QTY; ++i) {
        final int idx = i;
        final Step step = new Step() {
          @ Override public void step() {
            println(NAME + idx);
          }
        };
        steps.add(step);
      }
    }
    
    void keyPressed() {
      final int idx = (int) random(steps.size());
      steps.get(idx).step();
    }
    
    void mousePressed() {
      keyPressed();
    }
    
    interface Step {
      String NAME = "step";
      void step();
    }
    
  • edited November 2016 Answer ✓

    Of course, if we already know how many we're gonna need, a regular array offers a lesser boilerplate aesthetically code: :P

    // forum.Processing.org/two/discussion/19316/
    // arraylist-of-function-calls#Item_6
    
    // GoToLoop (2016-Nov-26)
    
    static final int QTY = 3;
    final Step[] steps = new Step[QTY];
    
    void setup() {
      noLoop();
    
      for (int i = 0; i < QTY; ++i) {
        final int idx = i;
        final Step step = new Step() {
          @ Override public void step() {
            println(NAME + idx);
          }
        };
        steps[i] = step;
      }
    }
    
    void keyPressed() {
      final int idx = (int) random(QTY);
      steps[idx].step();
    }
    
    void mousePressed() {
      keyPressed();
    }
    
    interface Step {
      String NAME = "step";
      void step();
    }
    
  • Interesting @GoToLoop. I will check if it works in my code. Thanks.

    @Quark what is the Java Reflection API?

    Kf

  • Answer ✓

    This discussion demonstrates how to use reflection to execute a method whose name is stored in a string

  • Interesting... Do you have any links that would help me in learning all this? Thanks in advance.

  • Answer ✓

    Do you have any links that would help me in learning all this?

    If you mean reflection you might start here

Sign In or Register to comment.