Groovy - List Sum -


i trying sum numbers in range (0 9) can divided 3 or 5.

approach 1:

    def result = (0..9).findall {          (it % 3 == 0 || % 5 == 0)     }.sum()     println result 

prints 23 expected.

approach 2: same above. trying ignore temp variable result & print directly.

    println (0..9).findall {         (it % 3 == 0 || % 5 == 0)     }.sum() 

prints [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

what going on here? why showing entire list instead of sum in approach 2.

approach 3: same approach 2. printing directly. moved range variable.

    def lst = 0..9     println lst.findall {         (it % 3 == 0 || % 5 == 0)     }.sum() 

prints 23 again.

does groovy expect me have temp variable :( ??

the groovy parser thinks you're doing

println (0..9) 

and doing rest of result of println

just give parser helping hand outer set of parentheses

println( (0..9).findall {     (it % 3 == 0 || % 5 == 0) }.sum() ) 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -