Creating a Service with Micronaut

Micronaut is a framework on JVM to develop microservices and serverless (FaaS) based applications. Micronaut support Java, Groovy and Kotlin languages.Let's quickly get our hands wet with creating a service using Micronaut.Installing Micronaut CLIIf you are are on a *nix platform and not using SDKMAN, I highly recommend…

Groovy Execute Around Pattern

In my previous post, we saw how languages with higher order functions make the implementation of Command Pattern very trivial. With a slight modification, we can achieve the Execute Around pattern.Suppose we have two types of transactions (atomic operations). Let's represent them using closures transactionOne and transactionTwo respectively. Now…

Command Pattern in Groovy using Closures

Command Pattern is one of the design patterns published in the GoF book. Command pattern is essentially encapsulating various types of request under a unified interface. The goal here is to minimise the coupling between executor and the client, with the system retaining the ability to evolve with new commands.…

Groovy - Strategy Pattern using Closures

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…

Groovy - Converting Methods to Closures

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…

Groovy Inline Closures

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…

Groovy Functional Programming - Filtering with findAll

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…

Groovy Functional Programming - Collect

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]…