views:

167

answers:

1

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?

+1  A: 

An example may be something like this.

Suppose you create a ui component for example a "fashion button"

This fashion button has a method onMouseOver that receives as a parameter another method X. inside onMouseOver some preprocessing is made and then X method is called.

So the user of fashion button can create this X method and pass it to onMouseOver, so when the mouse pointer is over the button X method will be executed.

Enrique
This is quite a good example, but in my tutorial I focus on the script language rather then the GUI programming itself.
Helper Method