Little Groovy Example
The Disco Blog has a nice little groovy example:
That was easy enough; however, I wanted to see if I could do away with the sum variable by using one of Groovy’s many hip magic methods attached to collections. Thus, my second attempt leverages Groovy’s findAll method, which permits putting a condition in the resulting closure, which then returns a collection meeting that criteria. Accordingly, with the returned collection of numbers meeting my criteria (that is, any number that is divisible by 5 or 3), I then have to issue the sum method like so:
def val = (1..<1000).findAll{(it % 5 == 0) || (it % 3 == 0)}
assert val.sum() == /* you gotta figure it out yourself, man! */
via The Disco Blog
About this entry
You’re currently reading “Little Groovy Example,” an entry on _mindMeld
- Published:
- 7.1.09 / 3pm
- Category:
- Java
- Tags:










Comments
Jump to comment form | comments rss [?] | trackback uri [?]