JavaFX - reduce() function to show how to pass functions as parameters
- by Helper Method
At the moment I'm writing a JavaFX guide for Java developers. In order to show how to pass a function to another funtion i adopted the reduce() function found in Effective Java:
function reduce(seq: Integer[],
f: function(: Integer, : Integer): Integer,
init: Integer) {
var result = init;
for (i in seq) {
result = f(i, result);
}
result
}
def nums = [1 .. 10];
println(reduce(nums, function(a: Integer, b: Integer) { a + b }, 0)); // prints 55
println(reduce(nums, function(a: Integer, b: Integer) { a * b }, 0)); // prints 3628800
Now I wonder if this example is not to hard for someone starting to learn JavaFX. The tutorial is targeted to programmers with a solid understanding of Java, yet I'm not quite sure about the usefulness of the example. Any ideas?