开发者

JavaFX - reduce() function to show how to pass functions as parameters

At the moment I'm writing a JavaFX guide for Java developers. In order to show how to pass a function to another function 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, 开发者_StackOverflow中文版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 }, 1)); // prints 3628800

Now I wonder if this example is not too hard for somebody starting to learn JavaFX. The tutorial is targeted at programmers with a solid understanding of Java, yet I'm not quite sure about the usefulness of the example. Any ideas?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜