Skip to content

Commit ee2f6fc

Browse files
authored
Update groovy3-syntax-sugar.md
1 parent cd4d4ca commit ee2f6fc

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

content/blog/2020/groovy3-syntax-sugar.md

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- title=More syntax sugar for Nextflow developer!
1+
<!-- title=More syntax sugar for Nextflow developers!
22
date=2020-11-02
33
type=post
44
tags=nextflow,dsl
@@ -7,23 +7,25 @@ author=Paolo Di Tommaso
77
icon=paolo.jpg
88
~~~~~~ -->
99

10-
# More syntax sugar for Nextflow developer
10+
# More syntax sugar for Nextflow developers
1111

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

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

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

2020

2121
## Improved not operator
2222

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

2626
```
27+
list = [10,20,30]
28+
2729
if( !(x in list) ) {
2830
// ..
2931
}
@@ -45,14 +47,14 @@ else if( x !instanceof String ) {
4547
}
4648
```
4749

48-
Again, this is a small syntax change which only makes the code a bit more
50+
Again, this is a small syntax change which makes the code a little more
4951
readable.
5052

5153

5254
## Elvis assignment operator
5355

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

5759
```
5860
def opts = [foo: 1]
@@ -64,8 +66,8 @@ assert opts.foo == 1
6466
assert opts.bar == 20
6567
```
6668

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

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

@@ -75,20 +77,20 @@ if( some_variable != null ) {
7577
}
7678
```
7779

78-
### Java style lambda expression
80+
### Java style lambda expressions
7981

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

85-
In terms of syntax a Groovy closure is defined as:
87+
In terms of syntax, a Groovy closure is defined as:
8688

8789
```
8890
{ it -> SOME_EXPRESSION_HERE }
8991
```
9092

91-
While Java lamba looks like:
93+
While Java lambda expression looks like:
9294

9395
```
9496
it -> { SOME_EXPRESSION_HERE }
@@ -100,11 +102,11 @@ which can be simplified to the following form when the expression is a single st
100102
it -> SOME_EXPRESSION_HERE
101103
```
102104

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

107-
For example the following Nextlow idiom:
109+
For example, the following Nextflow idiom:
108110

109111
```
110112
Channel
@@ -113,7 +115,7 @@ Channel
113115
.view { "the value is $it" }
114116
```
115117

116-
Can be rewritting using the labda syntax as:
118+
Can be rewritten using the lambda syntax as:
117119

118120
```
119121
Channel
@@ -122,7 +124,7 @@ Channel
122124
.view( it -> "the value is $it" )
123125
```
124126

125-
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
127+
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
126128
round parentheses to define the argument e.g.
127129

128130
```
@@ -133,13 +135,13 @@ Channel
133135
```
134136

135137

136-
### Fully support Java streams API
138+
### Full support for Java streams API
137139

138-
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.
140+
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.
139141

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

144146
A Java stream looks like the following:
145147

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

164166
### Java style method reference
165167

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

170172
```
171173
Channel
@@ -188,13 +190,12 @@ to each element emitted by the channel.
188190

189191
### Conclusion
190192

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

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

198-
Finally, a big thanks to the Groovy community for the big effort of developing and maintaining this
199-
awesome programming environment.
200-
200+
Finally, a big thanks to the Groovy community for their significant efforts in developing and maintaining this
201+
great programming environment.

0 commit comments

Comments
 (0)