Suppose we have a list of numbers.def numbers = [1, 2, 3, 4, 5, 6, -1] We are asked to do the followingFind out all the even numbers in the listFind out all the numbers divisible by 3Find out all the negative numbers in the listLet's write a function each…

One of the important characteristics of Groovy closure is that they are first class - closures can be passed as arguments to other closures and functions.More details on higher order functions in this post.Suppose you have a piece of functionality available as a method and you want to…

In my previous posts, I talked about collect, findAll and inject methods.Most of the time these methods get used together. Let's take an example. Suppose we want find the sum of squares of even numbers in a list of numbers.One of the approaches we can take here is…

Consider the following closure sum and it's use in inject method.def numbers = [1, 2, 3, 4, 5, 6, 7] def sum = { number1, number2 -> number1 + number2 } println numbers.inject(0, sum) // 28 If you are not familiar with Groovy closures, this post has the details.In the above…

One of the common tasks in programming is to generate a summary from a collection of data. To make the concept easy for understanding, consider the following list of numbers.def numbers = [1, 2, 3, 4, 5, 6, 7] Suppose we want to find the sum of number in the…

In my previous post, I wrote about map transformation. Similarly, another common operation is filtering values from a collection that satisfy a condition. We refer to the condition as a predicate.Consider the following list of numbers.def numbers = [1, 2, 3, 4, 5, 6, 7] Let's write a function…

Functional programming languages provide a map higher order function, which produces a new collection by applying the specified function on each element of the collection on which the map operation is invoked. Groovy uses collect instead of map.Consider the following list of numbers.def numbers = [1, 2, 3, 4]…

Suppose you have a computation heavy function.def computeHeavyFunction(def number) { println "computeHeavyFunction called with \$number" number + 1 } println computeHeavyFunction(1) println computeHeavyFunction(2) println computeHeavyFunction(1) In the absence of println statement, this function would have been pure. I have added the print statement just for the purpose of…

Let us create a function that computes the factorial of a number using recursion.def factorial(def number) { if(number == 0){ 1 } else{ number * factorial(number - 1) } } println factorial(1) // 1 println factorial(3) // 6 println factorial(10) // 3628800 What would happen if we invoke factorial function with 17…

Consider the following list of numbers.def numbers = [1, 2, 3, 4, 5] IterationLet's calculate the sum of numbers. First let us approach solution using imperative methoddef computeSumImperative(List numbers) { def sum = 0 def iterator = numbers.iterator() while(iterator.hasNext()){ sum += iterator.next() } sum } println computeSumImperative(numbers) RecursionHere we are…