groovy

Topics related to groovy:

Getting started with groovy

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

Closures

Spread Operator

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() } == []

String Interpolation

Strings and GString literals

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.

Ternary and Elvis Operators

The Elvis operator evaluates based on Groovy-Truth of the condition-part.

Spaceship Operator

Currying

  • 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

AST Transformations

Collection Operators

Safe Navigation Operator

Groovy Truth (true-ness)

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:

  • numbers: a zero value evaluates to false, non zero to true.
  • objects: a null object reference evaluates to false, a non null reference to true.
  • Character : a character with a zero value evaluates to false, true otherwise.
  • String : a string evaluates to true if not null and not empty, false if null or empty (applies to GStrings and CharSequences too).
  • Collections and Maps (including subclasses List, Map, Set, HashSet ...) : also takes into account the size, evaluates to true if the collection is not null and not empty, false if null or empty.
  • Enumerations and Iterators evaluates to true if not null and they are some more elements (groovy evaluates hasMoreElements or hasNext on the object), false if null or no more elements.
  • Matcher : a matcher evaluates to true if there is at least one match, false if not match is found.
  • Closure : a closure evaluates to the evaluation of the result returned by the closure.

The asBoolean method can be overriden in a user defined class to provide custom boolean evaluation.

JSON

Domain Specific Languages

Memoized Functions

Closure Memoize Methods

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.

Memoized Functions

Visiblity

Traits

Use ConfigSluper (instead of property files)

RESTClient

Ways of Iteration in Groovy

Groovy code golfing