Arraylist full of functions?

Sam Pottinger has done an amazing job in updating processing, not only to update to OpenJDK11, but also to support java 8 syntax in the processing ide. I’m sure he would welcome your support. Here is an example using lambda syntax in the processing ide.

interface MyFunction {
  float calculate(float x);
}

float[] values = new float[2];

void setup() {
  // lambda expression to define the calculate method
  MyFunction doubling = (float x)->x * 2;
  values[0] = doubling.calculate(5);
  values[1] = doubling.calculate(12);
  display();
}

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

The code can be even more elegant in JRubyArt

attr_reader :values

def setup
  # lambda expression to define the calculate method
  doubling = ->(x) { x * 2 }
  @values = [5.0, 12.0].map { |x| doubling.call(x) }
  display
end

def display
  puts values
end
3 Likes