Search

Dark theme | Light theme

October 8, 2025

Nushell Niceties: Encoding And Decoding URL

Sometimes you want to transform a string value into a URL encoded value, so it can be used as part of a valid URL. For example a string with spaces or special characters can not be used in a URL as is, but needs to be URL encoded. Nushell has the url encode command to achieve this. You can simple run this command on a string value and the result is a URL encoded value. With the option --all or -a even more special characters like a dot (.) are encoded. The input of the command can be a string value or a list of string values. But it is also possible to use a record or table structure, but then you need to add as extra argument the name or names of the keys or columns of which the string values should be encoded.
Oppossed to URL encoding a value you can also decode a URL encoded value using the url decode command. This command doesn’t have a special option to run. Just like with the url encode command the url decode command works on strings, list of strings, records and tables. If the input is a record or table the name of key or column of which the values must be decoded must be passed as extra arguments.

October 5, 2025

Groovy Goodness: Interleaving Elements From Collections

Groovy 5 adds the interleave method to the Iterable class. With this method you can interleave elements from two iterables. The result is a new List with elements where the first element is the first element of the first iterable and the second element the first element of the second iterable and so on. The size of the smallest collection is used to keep interleaving elements. Elements from the larger collection are ignored in the result.
If you want to have the items from the largest collection in the resulting list you can use the value true as second argument for the interleave method.

September 26, 2025

Groovy Goodness: Grouping Iterables Using zip And zipAll

Groovy 5 adds the extension methods zip and zipAll for iterables and iterators. Using the method you can combine elements from two collections into a new collection. The new collection contains Tuple2 instances where the values come from the items at the same index from both collections. So the first item of the first collection is grouped with the first item of the second collection. The size of the resulting collection is determined by the size of the smallest collection that is zipped.
With the zipAll method you can combine iterables of different sizes and set default values for missing items. It is possible to set a default value if an item is missing from the first iterable or the second iterable.

September 17, 2025

Groovy Goodness: Getting Extension And BaseName For File And Path

Groovy 5 adds the extension methods getExtension and getBaseName to the File and Path classes. You can invoke them as properties for a File and Path objects. Also the asBoolean method is added. This mean you can use a File or Path instance in a boolean context. If the underlying file exists true is returned and false otherwise.

September 12, 2025

Groovy Goodness: Accessing Regular Expression Named Groups By Name

Groovy (and Java) support using names for groups in regular expressions. The name of the group is defined using the syntax ?<name> where name must be replaced with the actual group name. This is very useful, because you can use the group name to access the value that is captured by the defined regular expression in a java.util.regex.Matcher object. Groovy supports for a long time accessing a group using the index operator. Since Groovy 5 you can use the name of the group to access the value as well. You can specify the name between square brackets ([<name>]) or use the name as property.

September 11, 2025

Java Joy: Return Default Value For Null Value

Since Java 9 the methods requireNonNullElse and requireNonNullElseGet are part of the java.util.Objects class. The method requireNonNullElse accepts two arguments. The first argument is an object that will be returned if that object is not null. The second argument is an object to be returned when the first argument is null. With this method you can replace the following ternary operator if (a != null) ? a : b (or if (a == null) ? b : a) with Objects.requireNonNullElse(a, b).
The method requireNonNullElseGet allows to define a Supplier function as second argument. The Supplier is only invoked when the first argument is null, so it is lazy and will only be invoked when the first argument is null.

September 10, 2025

Groovy Goodness: Using Index Operator For Streams

Groovy already has a lot of extra methods for Stream classes. With Groovy 5 the getAt method is added as a terminal operation. You can now use the [] syntax to get an item from a Stream from a specific position. The argument is the index of the item or a range with the start and end index of the items to get.

September 9, 2025

Groovy Goodness: Get Next And Previous Characters

Since Groovy 1.8.2 the next and previous methods are added to the Character class. When you invoke the method next on a char or Character instance the next character is returned. And when you use the previous method the previous character is returned.
Groovy 5 adds an overloaded version of the next and previous method that accepts an int argument. With this argument you can specify the number of characters to skip before returning the next or previous character. For example 'a'.next(2) return 'c' and 'c'.previous(2) returns 'a'.

September 5, 2025

Groovy Goodness: Map Methods To Operators

Groovy supports operator overloading since the start. Operator overloading is implemented by an actual method signature that maps to an operator. For example an object with a plus method can be used with the + operator. There is a list of methods and operators available on the Groovy website.
As long as an object has a method with a name that Groovy understands the corresponding operator can be used in Groovy code. This is even true for Java objects. Since Groovy 5 you can use the groovy.transform.OperatorRename annotation on classes, methods or constructors to map other method names to the Groovy operator overloading method names. This is very useful for third-party classes that you cannot change, but still want to use simple operators in Groovy code. You can reassign a method name to the following methods so an operator can be used: plus, minus, multiply, div, remainder, power, leftShift, rightShift, rightShiftUnassigned, and, or, xor, compareTo. Suppose you use a class with an add method and want to use the + operator for this method. The following annotation can be used @OperatorRename(plus = 'add') for a method and inside the method you can use the + operator instead of the add method of the class.

September 4, 2025

Groovy Goodness: Logical Implication Operator

Since Groovy 1.8.3 Groovy has an implies() method for Boolean types. Groovy 5 adds an operator ==> for this method so you have a shorter way to express a logical implication. A logical implication is a logical function that can be expressed as P ==> Q. You can read this as P implies Q or if P than Q. This expression is true in every case except when P is true, but Q is false. The following truth table shows the interpretaton of the logical implication operator:

P Q P ==> Q

false

true

true

false

false

true

true

true

true

true

false

false