Skip to content

Commit

Permalink
Update groovy3-syntax-sugar.md
Browse files Browse the repository at this point in the history
  • Loading branch information
evanfloden authored Nov 3, 2020
1 parent cd4d4ca commit ee2f6fc
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions content/blog/2020/groovy3-syntax-sugar.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- title=More syntax sugar for Nextflow developer!
<!-- title=More syntax sugar for Nextflow developers!
date=2020-11-02
type=post
tags=nextflow,dsl
Expand All @@ -7,23 +7,25 @@ author=Paolo Di Tommaso
icon=paolo.jpg
~~~~~~ -->

# More syntax sugar for Nextflow developer
# More syntax sugar for Nextflow developers

Latest Nextflow version 2020.10.0 is the first stable release running on Groovy 3.
The latest Nextflow version 2020.10.0 is the first stable release running on Groovy 3.

The first benefit of this chnage is that now Nextflow can be compiled and run on any modern Java virtual machine,
from Java 8 up to latest Java 15!
The first benefit of this change is that now Nextflow can be compiled and run on any modern Java virtual machine,
from Java 8, all the way up to the latest Java 15!

Along with this, the new Groovy runtime brings a bunch of syntax enhancements that can be useful
in the everyday life of pipeline developers. Let's see them more in detail.
Along with this, the new Groovy runtime brings a whole lot of syntax enhancements that can be useful in
the everyday life of pipeline developers. Let's see them more in detail.


## Improved not operator

The `!` (not) operator can now prefix the `in` and `instaceof` keyword.
This makes a bit more concise writing some conditional expression, for example the following snippet:
The `!` (not) operator can now prefix the `in` and `instanceof` keywords.
This makes for more concise writing of some conditional expression, for example, the following snippet:

```
list = [10,20,30]
if( !(x in list) ) {
// ..
}
Expand All @@ -45,14 +47,14 @@ else if( x !instanceof String ) {
}
```

Again, this is a small syntax change which only makes the code a bit more
Again, this is a small syntax change which makes the code a little more
readable.


## Elvis assignment operator

The elvis assigment operator `?=` allows the assignment of a value only if it was
not previouvsly assigned (or it evaluets to `null`). Consider the following example:
The elvis assignment operator `?=` allows the assignment of a value only if it was not
previously assigned (or if it evaluates to `null`). Consider the following example:

```
def opts = [foo: 1]
Expand All @@ -64,8 +66,8 @@ assert opts.foo == 1
assert opts.bar == 20
```

In this snippet the assignment `opts.foo ?= 10` is ignored because the dictionary `opts` already
contains a value for the `foo` attribute, while the following is assigned as expected.
In this snippet, the assignment `opts.foo ?= 10` would be ignored because the dictionary `opts` already
contains a value for the `foo` attribute, while it is now assigned as expected.

In other words this is a shortcut for the following idiom:

Expand All @@ -75,20 +77,20 @@ if( some_variable != null ) {
}
```

### Java style lambda expression
### Java style lambda expressions

Groovy 3 supports the syntax for Java lambda expression. If you don't know what is a Java lamda expression
don't worry, it's a concept very similar to a Groovy closure, thought with slight differences
both in the syntax and the semantic (i na few words a Groovy closure can modify a variable in the outside scope,
while a Java lamda cannot).
Groovy 3 supports the syntax for Java lambda expression. If you don't know what a Java lambda expression is
don't worry; it's a concept very similar to a Groovy closure, though with slight differences
both in the syntax and the semantic. In a few words, a Groovy closure can modify a variable in the outside scope,
while a Java lambda cannot.

In terms of syntax a Groovy closure is defined as:
In terms of syntax, a Groovy closure is defined as:

```
{ it -> SOME_EXPRESSION_HERE }
```

While Java lamba looks like:
While Java lambda expression looks like:

```
it -> { SOME_EXPRESSION_HERE }
Expand All @@ -100,11 +102,11 @@ which can be simplified to the following form when the expression is a single st
it -> SOME_EXPRESSION_HERE
```

The good news is that the two syntax are interoperable in many cases and we can use the *lamda*
syntax to get rid-off of the curly bracket parentheses used by the Groovy notation and make our Nextflow
The good news is that the two syntaxes are interoperable in many cases and we can use the *lambda*
syntax to get rid-off of the curly bracket parentheses used by the Groovy notation to make our Nextflow
script more readable.

For example the following Nextlow idiom:
For example, the following Nextflow idiom:

```
Channel
Expand All @@ -113,7 +115,7 @@ Channel
.view { "the value is $it" }
```

Can be rewritting using the labda syntax as:
Can be rewritten using the lambda syntax as:

```
Channel
Expand All @@ -122,7 +124,7 @@ Channel
.view( it -> "the value is $it" )
```

Which is a bit more consistent. Note however that the `it ->` implicit argument is now mandatory (while using the closure syntax it could be omitted) and when the operator argument is not *single* value, the lambda requires the
It is a bit more consistent. Note however that the `it ->` implicit argument is now mandatory (while when using the closure syntax it could be omitted). Also, when the operator argument is not *single* value, the lambda requires the
round parentheses to define the argument e.g.

```
Expand All @@ -133,13 +135,13 @@ Channel
```


### Fully support Java streams API
### Full support for Java streams API

Java since version 8 provide a [stream library](https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/) that is very powerful and implements some concepts and operators similar to Nextflow channels.
Since version 8, Java provides a [stream library](https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/) that is very powerful and implements some concepts and operators similar to Nextflow channels.

The main differences between the two is the Nextflow channels and the corresponding operators are *non-blocking*
The main differences between the two are that Nextflow channels and the corresponding operators are *non-blocking*
i.e. their evaluation is performed asynchronously without blocking your program execution, while Java streams are
executed in a synchronus manner (at least by default).
executed in a synchronous manner (at least by default).

A Java stream looks like the following:

Expand All @@ -163,9 +165,9 @@ methods are Java stream operator not the

### Java style method reference

The new runtime allows also the use of the `::` operator to reference an object method.
This can be useful to pass a method as an argument to a Nextflow operator in a similar manner
how it was already possible using a closure. For example:
The new runtime also allows for the use of the `::` operator to reference an object method.
This can be useful to pass a method as an argument to a Nextflow operator in a similar
manner to how it was already possible using a closure. For example:

```
Channel
Expand All @@ -188,13 +190,12 @@ to each element emitted by the channel.

### Conclusion

The new Groovy runtime brings a lot syntax sugar for Nextflow pipelines and allow the use of modern Java
runtime which deliver better performances and resources usage.
The new Groovy runtime brings a lot of syntax sugar for Nextflow pipelines and allows the use of modern Java
runtime which delivers better performance and resource usage.

The ones listed above are only some of them which may be usefull to everyday Nextflow developers.
If you are curious to learn more about all the changes in the new Groovy parser you can find a
The ones listed above are only a small selection which may be useful to everyday Nextflow developers.
If you are curious to learn more about all the changes in the new Groovy parser you can find more details in
[this link](https://groovy-lang.org/releasenotes/groovy-3.0.html).

Finally, a big thanks to the Groovy community for the big effort of developing and maintaining this
awesome programming environment.

Finally, a big thanks to the Groovy community for their significant efforts in developing and maintaining this
great programming environment.

0 comments on commit ee2f6fc

Please sign in to comment.