Groovy is
is an optionally typed dynamic language for the Java Virtual Machine
builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby, and Smalltalk
makes modern programming features available to Java developers with an almost-zero learning curve
provides the ability to statically type check and statically compile your code for robustness and performance
supports Domain-Specific Languages and other compact syntax so your code is easy to read and maintain
makes writing shell and build scripts easy with its powerful processing primitives, OO abilities, and an Ant DSL
increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications
simplifies testing by supporting unit testing and mocking out-of-the-box
seamlessly integrates with all existing Java classes and libraries
compiles straight to Java bytecode so you can use it anywhere you use Java
In most cases, the spread operator *.
is identical to calling .collect { it.________ }
.
def animals = ['cat', 'dog', 'fish']
assert animals*.length() == animals.collect { it.length() }
But if the subject is null, they behave a differently:
def animals = null
assert animals*.length() == null
assert animals.collect { it.length() } == []
Groovy has two string types the java java.lang.String
and groovy.lang.GString
, as well as multiple forms of string literals (see syntax and examples).
The main difference between the two types of strings is that GString supports string interpolation.
The Elvis operator evaluates based on Groovy-Truth of the condition-part.
Currying a closure produces a new closure with one or more of it's parameters having a fixed value
Left or right currying a closure that has no parameters or index based currying a closure that has less than two parameters throws an IllegalArgumentException
Groovy evaluates conditions in if, while and for statements the same way as Java does for standard Java conditions : in Java you must provide a boolean expression (an expression that evaluates to a boolean) and the result is the result of the evaluation.
In Groovy , the result is the same as in Java for thoses conditions (no examples provided, this is standard Java).
The other truthfulness evaluation mechanism shown by examples can be summarized as:
The asBoolean method can be overriden in a user defined class to provide custom boolean evaluation.
Memoization is a method of caching the result of a closure invocation. The memoize function applied to a closure returns a new closure whose return value is cached according to its input parameters. The caches used for the three tweaked variants of memoization methods are LRU caches, that is the least recently used element is removed from the cache first.