Post tagged: groovy

Euler in Groovy 1: sum of numbers

Print the sum of numbers less than 1000 that are divisble by 5 or 3.

The problem is:

Print the sum of numbers less than 1000 that are divisble by 5 or 3.

Already, we're exposed to a few nifty features of Groovy - ranges, and the functional methods on arrays like findAll:

// print the sum of numbers less than 1000 that are divisble by 5 …

Gotcha: map keys in Groovy

Key equality is tricky, let's go shopping.

Maps are great quick-and-dirty data structures or caches in many programming languages. But there's a least one way that Groovy maps aren't so friendly. Observe:

groovysh> m1 = [1:1]
===> {1=1}
groovysh> m1[1]
===> 1
groovysh> m1[(int) 1]
===> 1
groovysh> m1[(long) 1]
===> null

So, map keys are using …