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
Post a Comment