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.
Continue reading →
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 the next character of the ASCII table 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'.
Continue reading →
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.
Continue reading →
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.
Continue reading →
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.
Continue reading →
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.
Continue reading →
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
|
Continue reading →
Groovy 5 adds support for using an index variable in a for-in loop. You must define an extra variable as first argument of the for loop. This variable will be used as index variable. For each iteration of the code in the for-in loop the value of this index variable is incremented by 1. You can use this value in the for loop.
Continue reading →
Groovy 5 adds a new utility method to create an ascii bar chart. You can use the bar method in the org.codehaus.groovy.util.StringUtil class. You can pass a value, a minimum and maximum value and optinally specify the width of the bar chart. The result is a String value consisting of a number of "blocks". A block could be whole, but also 1/8 eights of the block are used to get a nice looking bar chart. How many of these values are needed is based on the input arguments. With this method you have a nice way to format number values on a command-line.
Continue reading →
Groovy is known for exending standard Java classes with extra methods or extra arguments for existing methods. Since Groovy 5 you can use a range as argument for the List.subList method. The range is used to determine the begin and end index of the original List instance to return.
Continue reading →
Groovy 5 introduced some extra overloaded methods for the collectEntries method, that can be used to transform an iterable object into a Map.
You can now pass a closure or function as arguments to transform the original iterable element into the key and value for the resulting Map. It is now also possible to pass a so-called collector Map that will be used to extend with new key/value pairs.
Besides extra overloaded method signatures for collectEntries Groovy 5 also adds the new methods withCollectedKeys and withCollectedValues. With the method withCollectedKeys a closure or function can passed to create the keys for the new Map based on the elements from the iterable. The value of the key/value pair is the unchanged element. You use the method withCollectedValues to pass a closure or function to create the value for the new key/value pair in the resulting Map. The key will be the original element from the iterable.
Continue reading →
The Nushell command enumerate adds an index value to each item in a list. The index value is stored in an extra column named index. You can use this column to filter data based on the index value.
Continue reading →