JavaFX - reduce() function to show how to pass functions as parameters
Posted
by Helper Method
on Stack Overflow
See other posts from Stack Overflow
or by Helper Method
Published on 2010-03-11T21:45:16Z
Indexed on
2010/03/11
21:49 UTC
Read the original article
Hit count: 267
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?
© Stack Overflow or respective owner