This blog post “Harnessing the power of groovy” demonstrates the power of groovy language. In java, what could take 10-15 lines, in groovy we can achieve it using 1-line code.

Multiply Each Number by 2

Adding numbers in a list

Reading a File

Filtering a list

Find Min/Max in a List

The above tips were quite basic and easy to understand. I would also like to introduce some advanced concepts of groovy: memoization and trampolining.

What is Trampolining ?

It is used in case of recursive closures. It is used to avoid StackOverflow exception which occurs when there are many return values stored on the stack.

Now, let’s try the trampolined version:

In the above example there is no stack overflow exception. The trampoline method wraps our closure into a TrampolineClosure object and groovy executes factorial.trampoline(..) calls sequentially until our closure returns something else than an instance of TrampolineClosure. So, the stack is not filled up and does not result in StackOverflow exception. It is called “tail call optimization”.

What is Memoization ?

Memoization is a technique that is used to store the results of function calls and returning the cached result when the same input occurs. It results in faster processing.

It can be very useful for recursive calculations like calculating factorial, Fibonacci using recursion.

Here is an example to demonstrate it:

Non-Memoized version


We can see many duplicate calls for values like 1,2,3 etc. in the above example.

Now let’s try a memoized version:


We can now see in the above example that there is not even a single duplicate call which really saves a lot of time.

If you try fib(45) or fib(50) without memoization it will take more than 30 seconds to give the output.

That was all 🙂