Function as argument of another function

Thanks everyone, the following is working great for me:

interface Function {
  float applyFunction(float a);
}

Function myFunction = new Function() {
  public float applyFunction(float a) {
    return 2 * a;
  }
};


void setup() {
  MyVector vec = new MyVector(5, 12);
  vec.display();
  vec.applyFunction(myFunction);
  vec.display();
}




class MyVector {
  float[] values; 

  MyVector(float a, float b) {
    values = new float[2];
    values[0] = a;
    values[1] = b;
  }

  void applyFunction(Function f) {
    values[0] = f.applyFunction(values[0]);
    values[1] = f.applyFunction(values[1]);
  }

  void display() {
    println(values[0], values[1]);
  }
}
1 Like