Groovy 3 adds a new operator to the language: elvis assignment operator (?=
). This operator is a shorthand for an assignment where we want to assign a value to a variable or property if the variable or property is null or false (following Groovy truth). If the value of the variable or property is not null or false (again apply Groovy truth rules), the value stays the same.
In the following example code we use the elvis assignment operator:
Continue reading →
Groovy 3 adds support for Java’s lambda syntax expressions. This way we can write code in Groovy using lambda expressions just like in Java. But Groovy adds an additional feature and that is default parameter values for lambda expressions.
In the following example we use a default parameter value for a lambda expression.
Continue reading →
Groovy contains lots of little gems that just make live easier and our code more expressive. Groovy 3 doesn’t disappoint by adding some nice syntax additions. For example we can now use !in
to check if an item is not in a collection, opposed to in
that checks if an item is in a collection.
In the following example we use !in
:
Continue reading →
In Java we can use Collections.shuffle
method to randomly reorder items in a list. Groovy 3.0.0 adds the shuffle
and shuffled
methods to a List
or array directly. The implementation delegates to Collections.shuffle
. The shuffle
method will reorder the original list, so there is a side effect using this method. Or we can use the shuffled
method that will return a copy of the original list where the items are randomly ordered.
In the next example we use both methods to randomly order lists:
Continue reading →
In Groovy we have useful classes to parse JSON and XML: JsonSlurper
and XmlSlurper
. Groovy 3 adds the YamlSlurper
class to read in YAML formatted strings. The result of parsing the YAML content is a Map
object.
In the next example we have a sample YAML as string that we parse using the parseText
method of YamlSlurper
:
Continue reading →
Groovy 3 adds the YamlBuilder
class to create YAML output using a Groovy syntax. The YamlBuilder
is closely related to JsonBuilder
that is described in a previous post. We define a hierarchy using a builder syntax where we can use primitive types, strings, collections and objects. Once we have build our structure we can use the toString()
method to get a string representation in YAML format.
In the following example we use YamlBuilder
to create YAML:
Continue reading →
Groovy 3 adds the average
method to collections to calculate the average of the items in the collections. When the items are numbers simply the average is calculated. But we can also use a closure as argument to transform an item into a number value and then the average on that number value is calculated.
In the following example code we use the average method on a list of numbers and strings. And we use a closure to first transform an element before calculating the average:
Continue reading →
This week at work a colleague showed a nice feature of the Pattern
class in Java: we can easily turn a Pattern
into a Predicate
with the asPredicate
and asMatchPredicate
methods. The asPredicate
method return a predicate for testing if the pattern can be found given string. And the asMatchPredicate
return a predicate for testing if the pattern matches a given string.
In the following example code we use both methods to create predicates:
Continue reading →
We can use the flatten
method in Groovy to flatten a collection that contains other collections into a single collection with all elements. We can pass a closure as extra argument to the flatten
method to transform each element that is flattened. The argument of the closure is the element from the original collection.
In the following example we first use the flatten
method without a closure argument. Then we pass a closure argument and transform the element:
Continue reading →
In a previous post we learned about callouts in Asciidoctor to add explanation to source code. While surfing the Internet I came upon the following blog post by Alex Soto: Auto-numbered Callouts in Asciidoctor. I turns out that since Asciidoctor 1.5.8 we can use a dot (.
) instead of explicit numbers to have automatic increasing numbering for the callouts.
Let’s take our example from the earlier blog post and now use auto numbered callouts:
Continue reading →
Using the Stream API and the map
method we can transform elements in a stream to another object. Instead of using the map
method we can also write a custom Collector
and transform the elements when we use the collect
method as terminal operation of the stream.
First we have an example where we transform String values using the map
method:
Continue reading →
If we want to include Asciidoc markup as source language and show the markup without transforming it we can use a listing or literal block. For example we are using Asciidoc markup to write a document about Asciidoctor and want to include some Asciidoc markup examples. If the markup contains sections like a listing or literal block and it is enclosed in a listing or literal block, the tranformation goes wrong. Because the beginning of the included listing or literal block is seen as the ending of the enclosing listing or literal block. Let’s see what goes wrong with an example where we have the following Asciidoc markup:
When we transform this to HTML we get the following output:
Continue reading →