In the clojure.set
namespace we can find the intersection
function. This functions accepts one or more sets as arguments and return a new set with all elements that are present in the sets that are passed as arguments to the intersection
function. The argument must be a set, so we need to convert other collection or seq values to a set first before we use it as an argument for the function.
In the following example we use one, two or three arguments for the intersection
function and also convert other types to a set to be used as argument:
Continue reading →
We can use the join
function from the clojure.string
namespace to join elements from a collection into a string. We can optionally specify a separator that is used to separate each element in the string output. The separator is not used after the last element of the collection. If we don’t specify a separator the elements are concatenated without separation. The string representation for each element in the collection is used in the joined end result.
In the following example code we see different usages of the join
function:
Continue reading →
In the clojure.string
namespace we can find several useful function for working with strings. If we want to trim a string we can choose for the trim
, trial
, trimr
and trim-newline
functions. To trim all characters before a string we must use the triml
function. To remove all space characters after a string we use trimr
. To remove space characters both before and after a string we can use the trim
function. Finally if we only want to remove the newline and/or return characters we use the trim-newline
function.
In the following example we use the different trim functions on strings:
Continue reading →
Groovy 3 adds the takeBetween
method to the String
class. With this method we can get all the characters that are enclosed by string values. We can specify one enclosed string value and then all text between the the first occurrence of the string and the second occurrence is returned. If multiple parts are enclosed by the string values we can also specify which occurrence we want. If the text is enclosed by different string values we can use a variant of takeBetween
that takes two string values to indicate the boundaries of the text we want. Also with two different enclosed string values we can use an argument to get the n-th occurrence of the string that is found.
Since Groovy 3 we can also use takeBefore
and takeAfter
to get the string before or after a given string value. All three methods will return an empty string if no text can be found.
In the following example we use the takeBefore
, takeAfter
and takeBetween
methods with different arguments:
Continue reading →
Groovy adds a lot of methods to the Java String
class. For example we can use the take
method to get a certain number of characters from the start of a string value. With the drop
method we remove a given number of characters from the start of the string. In Groovy 3 we can now also take and drop a certain number of characters from the end of a string using the methods takeRight
and dropRight
.
In the following example we see how we can use the methods:
Continue reading →
Groovy has used the ==
operator to check if objects are equal for a long time. To test if object instances are the same we must use the is
method. Groovy 3 adds a new operator for the is
method and that is the ===
operator. And to negate the result of the is
method we can use the !==
operator.
In the following example we use the ===
and !==
operator to check if objects refer to the same instance or not:
Continue reading →
Groovy 3 adds the !instanceof
operator to check if an object is not an instance of a type. This is a shorthand for using instanceof
and then negate the result. It shows how little changes can make code easier to read.
In the following example we use the old way to check if object is not an instance of a type and the new !instanceof
operator:
Continue reading →
Groovy 3 adds the feature of safe index based access for lists, arrays and maps. This means we can use ?[index]
to get or a set a value on a list or array without getting a NullPointerException
when the list or array is not initialised. With maps we can use ?[key]
for getting a value or set a value and when the map object is not initialised we don’t get a NullPointerException
.
In the following example we see several examples of setting or getting values using indices or keys:
Continue reading →
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 →