Posts

Groovy Default Parameters

def  say ( msg  =  'Hello' ,  name  =  'world' )  {      "$msg $name!" }   // We can invoke 3 signatures: // say(), say(msg), say(msg, name) assert  'Hello world!'  ==  say ( ) // Right most parameter with default value is eliminated first. assert  'Hi world!'  ==  say ( 'Hi' ) assert  'Howdy, mrhaki!'  ==  say ( 'Howdy,' ,  'mrhaki' ) ​

Groovy Multiple Assignment

​ // Assign and declare variables.def (username, email) = ['mrhaki', 'email@host.com'] assert  'mrhaki'  ==  username assert  'email@host.com'  ==  email   // We can assign later than the definition of the variables. int  housenr String  streetname ( streetname ,  housenr )  =  [ 'Old Street' ,  42 ] assert  42  ==  housenr assert  'Old Street'  ==  streetname   // We can also use type declerations. def  ( String  streetName ,  int  houseNumber )  =  [ 'Old Street' ,  42 ] assert  42  ==  houseNumber assert  'Old Street'  ==  streetName   // Return value of method can be assigned to multiple variables. def  iAmHere ( )  {      [ 29.20090 ,  12.90391 ] } def  ( coordX ,  coordY )  =  iAmHere ( ) assert  coordX  ==  29.20090 assert  coordY...

Groovy Spread Operator

The spread operator ( * ) is used to tear a list apart into single elements. This can be used to invoke a method with multiple parameters and then spread a list into the values for the parameters. The spread operator can also be used to add lists or ranges to lists and to add maps to other maps. class  Simple  {      String  speak ( Integer  n ,  String  text ,  Date  date )  {          def  out  =  new  StringBuffer ( )          n . times  {              out  <<  "Say $text on ${date.format('yyyy-MM-dd')}.\n"          }          out      } }   // Spread params list for speak() method. def  params  =  [      2 ,      "hello world" ,      new  Date ( ) . parse ( "yyyy/MM/d...