What are block expressions actually good for?

Posted by Helper Method on Stack Overflow See other posts from Stack Overflow or by Helper Method
Published on 2010-04-13T09:11:27Z Indexed on 2010/04/13 9:12 UTC
Read the original article Hit count: 241

Filed under:

I just solved the first problem from Project Euler in JavaFX for the fun of it and wondered what block expressions are actually good for? Why are they superior to functions? Is it the because of the narrowed scope? Less to write? Performance?

Here's the Euler example. I used a block here but I don't know if it actually makes sense

// sums up all number from low to high exclusive which are divisible by a or b
function sumDivisibleBy(a: Integer, b: Integer, high: Integer) {
    def low = if (a <= b) a else b;

    def sum = {
        var result = 0; 

        for (i in [low .. <high] where i mod 3 == 0 or i mod 5 == 0) {
            result += i
        }

        result
    }
}

Does a block makes sense here?

© Stack Overflow or respective owner

Related posts about javafx