SlideShare a Scribd company logo
PAUL WOODS [email_address] TWITTER: @MR_PAUL_WOODS 7/7/2010 Groovy Every Day
Resources http://groovy-almanac.org/ http://mrhaki.blogspot.com/ http://gr8forums.org/index.php http://groovyconsole.appspot.com/ Manning Groovy In Action ISBN 1-932394-84-2 Programming Groovy  ISBN-10: 1-934356-09-3
List - create create a list def list = [ 'q', 'w', 'e' ] println list
List – non typed elements the items in a list are non-typed def a = [ 1, 1.1, 'a', new Date() ]
List – adding elements add elements to a list – use left shift def list = ['q', 'w', 'e'] list << 'r' list << 't' list << 'y' println list add a list to a list – use the plus list += [1,2,3] println list
List - iterating loop though the elements in a list def list = ['q', 'w', 'e', 'r', 't', 'y'] for(def item : list) { println &quot;by item : $item&quot; } for(def item in list) { println &quot;by item in : $item&quot; } list.each { item ->  println &quot;by each : $item&quot;  }
List - transform Transform a list def list = [1,2,3,4,5] def list2 = list.collect { it * 10 } println &quot;list=$list&quot; println &quot;list2=$list2&quot;
List – retrieving elements Retrieving Elements def list = ['q', 'w', 'e', 'r', 't', 'y'] println &quot;element 0 : ${list.get(0)}&quot; println &quot;element 1 : ${list[1]}&quot; println &quot;elements 1,3,5 : ${list[1,3,5]}&quot; println &quot;elements 0..3 : ${list[0..3]}&quot; println &quot;last 3 elements : ${list[-3..-1]} &quot;  println &quot;element last 3 : ${list[-3..-1]} &quot;
List – removing elements Removing Elements def list = [&quot;q&quot;, &quot;w&quot;, &quot;e&quot;, &quot;r&quot;, &quot;t&quot;, &quot;y&quot;] println list list -= &quot;q&quot; println list list -= [&quot;w&quot;, &quot;e&quot;] println list
List - sorting Sorting Lists //  note – original list is not changed def list = ['q', 'w', 'e', 'r', 't', 'y'] def sorted = list.sort() println &quot;list=$list&quot; println &quot;sorted=$sorted&quot;
List – unique elements Retrieving the unique elements // note – list  is  modified // note – list does not need to be sorted. def list = ['a', 'b', 'c', 'a', 'b', 'c' ] println &quot;list = $list&quot; list.unique() println &quot;list = $list&quot;
List - find Finding elements in Lists def list = ['q', 'w', 'e', 'r', 't', 'y'] def letter = list.find { 'q' }  println &quot;find : $letter&quot; def letters = list.findAll { it < 't' } println &quot;findAll : $letters&quot; println &quot;all items below f : ${list.every { item -> item < 'f'} }&quot; println &quot;any item below f : ${list.any { item -> item < 'f'} }&quot;
List - join convert list into a string def list = [ 'q','w','e','r','t','y'] println list.join(&quot;-&quot;)
List – advanced 1 sort a list of maps by first or last or last,first list = [  [first:&quot;paul&quot;, last:&quot;woods&quot;],  [first:&quot;linda&quot;, last:&quot;zinde&quot;],  [first:&quot;alex&quot;, last:&quot;zinde&quot;],  [first:&quot;paul&quot;, last:&quot;allen&quot;]  ] // sorting by a value in a map println &quot;sorted by first : ${list.sort { it.first } }&quot; println &quot;sorted by last  : ${list.sort { it.last } }&quot; // sorting by 2 values def sorted = list.sort { x, y ->  (x.last <=> y.last) ?: (x.first <=> y.first)  } println &quot;sort by last and first : ${sorted}&quot;
List – advanced 2 transform a list of lists to a csv string def list = [ [ &quot;first&quot;, &quot;last&quot; ], [ &quot;paul&quot;, &quot;woods&quot;],  [ &quot;linda&quot;, &quot;zinde&quot;],  [ &quot;alex&quot;, &quot;zinde&quot;],  [ &quot;paul&quot;, &quot;allen&quot;]  ] def csv = list.collect { row ->  row.collect { item ->    &quot;\&quot;$item\&quot;&quot; }.join(',') }.join('\n') println csv
List - mystery Why does this work? List<String> z = new ArrayList<String>() z << &quot;A&quot; z << 1 z << new Date() println z ? because generics in java are checked at compile time, and groovy doesn't check
 
Map - create Create map def map = [ first : &quot;Paul&quot;, last : &quot;Woods&quot; ] println map Tip – if you need iterate through your keys in order... def map = new TreeMap<String,String>()
Map – adding elements
Map - iterating looping through maps def map = [ first : &quot;Paul&quot;, last : &quot;Woods&quot; ] for(keyValue in map) { println &quot;keyValue=$keyValue&quot; } for(keyValue in map) { println &quot;key=$keyValue.key, value=$keyValue.value&quot; } map.each { kv -> println &quot;kv=$kv&quot; } map.each { k, v -> println &quot;k=$k, v=$v&quot; }
Map – retrieving elements retrieving elements def map = [ first : &quot;Paul&quot;, last : &quot;Woods&quot; ] println &quot;map.first = $map.first&quot; println &quot;map['first'] = ${map['first']}&quot; def key = &quot;first&quot; def value = map.&quot;$key&quot; println &quot;def key=\&quot;$key\&quot;; map.\&quot;\$key\&quot; = ${value}&quot;
Map – removing elements removing elements from a map def map = [ first : &quot;Paul&quot;, last : &quot;Woods&quot; ] map.remove('first') println map
Map – find finding elements def map = [ first : &quot;Paul&quot;, last : &quot;Woods&quot;] def result1 = map.find { kv -> kv.value == &quot;Woods&quot; } println result1.getClass() println result1 def result2 = map.findAll { kv -> kv.key != &quot;last&quot; } println result2.getClass() println result2
 
Range – the basics Basic range operations def range = (1..5) println range println range.class range.each { n -> println n } println &quot;contains 5 : &quot; + range.contains(5) println &quot;contains 7 : &quot; + range.contains(7) range.step(2) { println it } def range2 = (new Date()-7 .. new Date()) range2.each { date -> println date }
 
Operation – ?. subscript The method will not be called if the object is null. def list = [ 'a', 'b', null, 'c', 'd' ] // list.each { item -> println item.toUpperCase() } list.each { item -> println item?.toUpperCase() }
Operation – ?: conditional if object is false, return another object. else return the object def q = null println &quot;null : &quot; + (q ?: &quot;it is false&quot;) q = &quot;&quot; println &quot;empty : &quot; + (q ?: &quot;it is false&quot;) q = &quot;abc&quot; println &quot;value : &quot; + (q ?: &quot;it is false&quot;)
Operation - <=> - spaceship calls the .compareTo method  returns -1 if a < b returns +1 if a > b returns 0 if a == b println &quot;1 <=> 2 : &quot; + (1 <=> 2) println &quot;2 <=> 1 : &quot; + (2 <=> 1) println &quot;1 <=> 1 : &quot; + (1 <=> 1)
 
Closures - introduction A block of executable code, similar to a method, but it can be easily assigned to a variable, and passed to other methods. def add = { a, b ->   a+b } println add(1,2)
Closure - example A method that takes a closure class Names { def name1 def name2 def name3 def capitalize(Closure c) { c.call name1.capitalize() c.call name2.capitalize() c.call name3.capitalize() } } def names = new Names(name1:'paul', name2:'mike', name3:'eric') def greeting = { name -> println &quot;Hello, &quot; + name } names.capitalize greeting
Closure - syntax Syntax for zero, 1 and 2+ parameter closures def zero = { -> println &quot;zero parameters&quot; } def one_a = {  println &quot;one parameter : $it&quot; } def one_b = { a->  println &quot;one parameter : $a&quot; } def two = { a, b ->  println &quot;two parameters : $a $b&quot; } zero.call() one_a.call('1a') one_b.call('1b') two.call('22', '2222')
MultiAssign initialize or assign multiple variables with values from a list. def a  def b (a, b) = [ 1, 2] println &quot;a=$a&quot; println &quot;b=$b&quot; def (c, d) = [ 3 , 4 ] println &quot;c=$c&quot; println &quot;d=$d&quot;
 
Optional parenthesis, semicolons, and returns In some situations, groovy allows you to remove parenthesis, semicolons and return statements.
Optional – Parenthesis 1 No Arguments and no ‘get’ prefix – () mandatory class Name { def first, last def print() { println first + &quot; &quot; + last } def printDelim(delim) { println first + delim + last } def getFullName() { return first + &quot; &quot; + last } def getTotal(delim) { return first + delim + last } } def name = new Name(first:&quot;Paul&quot;, last:&quot;Woods&quot;) name.print()
Optional – Parenthesis 2 One or more arguments and not referencing the return value – () optional class Name { def first, last def print() { println first + &quot; &quot; + last } def printDelim(delim) { println first + delim + last } def getFullName() { return first + &quot; &quot; + last } def getTotal(delim) { return first + delim + last } } def name = new Name(first:&quot;Paul&quot;, last:&quot;Woods&quot;) name.printDelim &quot; &quot;
Optional – Parenthesis 3 The method has a ‘get’ prefix, and no arguments. () optional class Name { def first, last def print() { println first + &quot; &quot; + last } def printDelim(delim) { println first + delim + last } def getFullName() { return first + &quot; &quot; + last } def getTotal(delim) { return first + delim + last } } def name = new Name(first:&quot;Paul&quot;, last:&quot;Woods&quot;) println name.fullName
Optional – Parenthesis 4 method has ‘get’ and client has 1 or more arguments – () mandatory class Name { def first, last def print() { println first + &quot; &quot; + last } def printDelim(delim) { println first + delim + last } def getFullName() { return first + &quot; &quot; + last } def getTotal(delim) { return first + delim + last } } def name = new Name(first:&quot;Paul&quot;, last:&quot;Woods&quot;) println name.getTotal(&quot;,&quot;)
Optional – semicolons Semicolons are almost always optional Must be used if multiple statements on a single line. def a = 1 def b = 2 println a println b println a; println b
Optional – returns – 1 Returns are optional when the value to be returned is the last line of the method. def sum(a, b) { a + b } def sub(a, b) { def total = a + b total }
Optional – returns – 2 Returns are optional when the method is a if/else method. The value to be returned is the last line of each block. def choose(a, b, c) { if(a > 0) { b } else if(a < 0) { c } else { 0 } } println &quot; 1 : &quot; + choose( 1, 10, 20) println &quot;-1 : &quot; + choose(-1, 10, 20) println &quot; 0 : &quot; + choose( 0, 10, 20)
Optional – returns – 3
 
PowerAssert Power Assert – in a failed assert statement, groovy shows you the values of the objects. def map = [a : [ b : [ c : 2 ] ] ] assert 3 == map.a.b.c |  |  | | | |  |  | | 2 |  |  | {c=2} |  |  {b={c=2}} |  {a={b={c=2}}} false
PowerAssert - gotcha PowerAssert Gotcha – whitespace def a = &quot;a&quot; def b = &quot;a\r\n&quot; assert a == b Assertion failed:  assert a == b | |  | a |  a false May not tell you about whitespace
 
Get the current groovy Version import  org.codehaus.groovy.runtime.InvokerHelper      println InvokerHelper.version  
 
Groovy A Dynamic scripting language similar to the Java language Executes on the JVM (1.5 and 1.6) Can be used stand-alone, or can be added to a Java application.
Download and Install http://groovy.codehaus.org Download .zip Extract to c:\tools Set groovy_home = c:\tools\groovy-1.7.3 Add to your path: %groovy_home%\bin http://groovyconsole.appspot.com

More Related Content

PDF
Simple Ways To Be A Better Programmer (OSCON 2007)
PDF
2014 database - course 2 - php
PDF
Perl Bag of Tricks - Baltimore Perl mongers
PDF
Good Evils In Perl
PDF
Parsing JSON with a single regex
KEY
Refactor like a boss
PDF
Perl6 Regexen: Reduce the line noise in your code.
Simple Ways To Be A Better Programmer (OSCON 2007)
2014 database - course 2 - php
Perl Bag of Tricks - Baltimore Perl mongers
Good Evils In Perl
Parsing JSON with a single regex
Refactor like a boss
Perl6 Regexen: Reduce the line noise in your code.

What's hot (19)

PDF
Perl.Hacks.On.Vim
PDF
DBIx::Class introduction - 2010
PDF
The Magic Of Tie
PDF
Perl6 grammars
PDF
The Joy of Smartmatch
PPT
PDF
Perl6 in-production
PDF
Abuse Perl
PPTX
07 php
PPTX
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
PDF
Bag of tricks
PDF
20191116 custom operators in swift
PPT
Class 4 - PHP Arrays
ODP
Advanced Perl Techniques
ODP
ABC of Perl programming
PPT
Php Using Arrays
PDF
DBIx::Class beginners
PDF
Text in search queries with examples in Perl 6
Perl.Hacks.On.Vim
DBIx::Class introduction - 2010
The Magic Of Tie
Perl6 grammars
The Joy of Smartmatch
Perl6 in-production
Abuse Perl
07 php
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
Bag of tricks
20191116 custom operators in swift
Class 4 - PHP Arrays
Advanced Perl Techniques
ABC of Perl programming
Php Using Arrays
DBIx::Class beginners
Text in search queries with examples in Perl 6
Ad

Viewers also liked (20)

PPTX
Zelena čistka
PDF
PPT
Focus on What Matters
PPTX
Presentation1
PDF
Examenopleiding energieconsulent mfl
PDF
Bibliografia soundscape, sound, landscape
PPSX
The best power diy marketing tips for entrepreneurs
PPT
Marketing strategies to increase the ROI on mobile
PDF
Слайды к конференции "Маркетинг Финансовых Услуг" 24.06.10
PPS
How To Keep Men Women Happy
PPT
Integracija poslovnega sistema
PPT
Central Oh Prsa Socmed 10 3 09
PPS
Eski fotoğraf ve kartpostallar
PPTX
Continuous deployments in Azure websites (by Anton Vidishchev)
PPT
1.2 Estimating With Whole #S And Decimals
PDF
The CLAS APP
PPT
Iatefl 2013 titova
PDF
TEL4Health research at University College Cork (UCC)
PDF
Bezalel: Introduction to Interactive Design: ב4 - מבוא לעיצוב אינטראקטיבי - ה...
PPT
Votingsystems110607public
Zelena čistka
Focus on What Matters
Presentation1
Examenopleiding energieconsulent mfl
Bibliografia soundscape, sound, landscape
The best power diy marketing tips for entrepreneurs
Marketing strategies to increase the ROI on mobile
Слайды к конференции "Маркетинг Финансовых Услуг" 24.06.10
How To Keep Men Women Happy
Integracija poslovnega sistema
Central Oh Prsa Socmed 10 3 09
Eski fotoğraf ve kartpostallar
Continuous deployments in Azure websites (by Anton Vidishchev)
1.2 Estimating With Whole #S And Decimals
The CLAS APP
Iatefl 2013 titova
TEL4Health research at University College Cork (UCC)
Bezalel: Introduction to Interactive Design: ב4 - מבוא לעיצוב אינטראקטיבי - ה...
Votingsystems110607public
Ad

Similar to Groovy every day (20)

PDF
SeaJUG March 2004 - Groovy
PPTX
Scala en
PPTX
A Brief Intro to Scala
PPTX
Scala: Devnology - Learn A Language Scala
ODP
Scala introduction
PPTX
Switching from java to groovy
PPT
Scala for Java Developers
PDF
Introductionto fp with groovy
PPTX
Practically Functional
PPT
2007 09 10 Fzi Training Groovy Grails V Ws
PDF
Scala collections api expressivity and brevity upgrade from java
PPT
JBUG 11 - Scala For Java Programmers
PPTX
Groovy
PDF
03 Geographic scripting in uDig - halfway between user and developer
PPT
Scala presentation by Aleksandar Prokopec
PPT
Groovy unleashed
PPTX
Groovy Api Tutorial
PPTX
Start Writing Groovy
PPTX
Ruby Language: Array, Hash and Iterators
SeaJUG March 2004 - Groovy
Scala en
A Brief Intro to Scala
Scala: Devnology - Learn A Language Scala
Scala introduction
Switching from java to groovy
Scala for Java Developers
Introductionto fp with groovy
Practically Functional
2007 09 10 Fzi Training Groovy Grails V Ws
Scala collections api expressivity and brevity upgrade from java
JBUG 11 - Scala For Java Programmers
Groovy
03 Geographic scripting in uDig - halfway between user and developer
Scala presentation by Aleksandar Prokopec
Groovy unleashed
Groovy Api Tutorial
Start Writing Groovy
Ruby Language: Array, Hash and Iterators

Groovy every day

  • 1. PAUL WOODS [email_address] TWITTER: @MR_PAUL_WOODS 7/7/2010 Groovy Every Day
  • 2. Resources http://groovy-almanac.org/ http://mrhaki.blogspot.com/ http://gr8forums.org/index.php http://groovyconsole.appspot.com/ Manning Groovy In Action ISBN 1-932394-84-2 Programming Groovy ISBN-10: 1-934356-09-3
  • 3. List - create create a list def list = [ 'q', 'w', 'e' ] println list
  • 4. List – non typed elements the items in a list are non-typed def a = [ 1, 1.1, 'a', new Date() ]
  • 5. List – adding elements add elements to a list – use left shift def list = ['q', 'w', 'e'] list << 'r' list << 't' list << 'y' println list add a list to a list – use the plus list += [1,2,3] println list
  • 6. List - iterating loop though the elements in a list def list = ['q', 'w', 'e', 'r', 't', 'y'] for(def item : list) { println &quot;by item : $item&quot; } for(def item in list) { println &quot;by item in : $item&quot; } list.each { item -> println &quot;by each : $item&quot; }
  • 7. List - transform Transform a list def list = [1,2,3,4,5] def list2 = list.collect { it * 10 } println &quot;list=$list&quot; println &quot;list2=$list2&quot;
  • 8. List – retrieving elements Retrieving Elements def list = ['q', 'w', 'e', 'r', 't', 'y'] println &quot;element 0 : ${list.get(0)}&quot; println &quot;element 1 : ${list[1]}&quot; println &quot;elements 1,3,5 : ${list[1,3,5]}&quot; println &quot;elements 0..3 : ${list[0..3]}&quot; println &quot;last 3 elements : ${list[-3..-1]} &quot; println &quot;element last 3 : ${list[-3..-1]} &quot;
  • 9. List – removing elements Removing Elements def list = [&quot;q&quot;, &quot;w&quot;, &quot;e&quot;, &quot;r&quot;, &quot;t&quot;, &quot;y&quot;] println list list -= &quot;q&quot; println list list -= [&quot;w&quot;, &quot;e&quot;] println list
  • 10. List - sorting Sorting Lists // note – original list is not changed def list = ['q', 'w', 'e', 'r', 't', 'y'] def sorted = list.sort() println &quot;list=$list&quot; println &quot;sorted=$sorted&quot;
  • 11. List – unique elements Retrieving the unique elements // note – list is modified // note – list does not need to be sorted. def list = ['a', 'b', 'c', 'a', 'b', 'c' ] println &quot;list = $list&quot; list.unique() println &quot;list = $list&quot;
  • 12. List - find Finding elements in Lists def list = ['q', 'w', 'e', 'r', 't', 'y'] def letter = list.find { 'q' } println &quot;find : $letter&quot; def letters = list.findAll { it < 't' } println &quot;findAll : $letters&quot; println &quot;all items below f : ${list.every { item -> item < 'f'} }&quot; println &quot;any item below f : ${list.any { item -> item < 'f'} }&quot;
  • 13. List - join convert list into a string def list = [ 'q','w','e','r','t','y'] println list.join(&quot;-&quot;)
  • 14. List – advanced 1 sort a list of maps by first or last or last,first list = [ [first:&quot;paul&quot;, last:&quot;woods&quot;], [first:&quot;linda&quot;, last:&quot;zinde&quot;], [first:&quot;alex&quot;, last:&quot;zinde&quot;], [first:&quot;paul&quot;, last:&quot;allen&quot;] ] // sorting by a value in a map println &quot;sorted by first : ${list.sort { it.first } }&quot; println &quot;sorted by last : ${list.sort { it.last } }&quot; // sorting by 2 values def sorted = list.sort { x, y -> (x.last <=> y.last) ?: (x.first <=> y.first) } println &quot;sort by last and first : ${sorted}&quot;
  • 15. List – advanced 2 transform a list of lists to a csv string def list = [ [ &quot;first&quot;, &quot;last&quot; ], [ &quot;paul&quot;, &quot;woods&quot;], [ &quot;linda&quot;, &quot;zinde&quot;], [ &quot;alex&quot;, &quot;zinde&quot;], [ &quot;paul&quot;, &quot;allen&quot;] ] def csv = list.collect { row -> row.collect { item -> &quot;\&quot;$item\&quot;&quot; }.join(',') }.join('\n') println csv
  • 16. List - mystery Why does this work? List<String> z = new ArrayList<String>() z << &quot;A&quot; z << 1 z << new Date() println z ? because generics in java are checked at compile time, and groovy doesn't check
  • 17.  
  • 18. Map - create Create map def map = [ first : &quot;Paul&quot;, last : &quot;Woods&quot; ] println map Tip – if you need iterate through your keys in order... def map = new TreeMap<String,String>()
  • 19. Map – adding elements
  • 20. Map - iterating looping through maps def map = [ first : &quot;Paul&quot;, last : &quot;Woods&quot; ] for(keyValue in map) { println &quot;keyValue=$keyValue&quot; } for(keyValue in map) { println &quot;key=$keyValue.key, value=$keyValue.value&quot; } map.each { kv -> println &quot;kv=$kv&quot; } map.each { k, v -> println &quot;k=$k, v=$v&quot; }
  • 21. Map – retrieving elements retrieving elements def map = [ first : &quot;Paul&quot;, last : &quot;Woods&quot; ] println &quot;map.first = $map.first&quot; println &quot;map['first'] = ${map['first']}&quot; def key = &quot;first&quot; def value = map.&quot;$key&quot; println &quot;def key=\&quot;$key\&quot;; map.\&quot;\$key\&quot; = ${value}&quot;
  • 22. Map – removing elements removing elements from a map def map = [ first : &quot;Paul&quot;, last : &quot;Woods&quot; ] map.remove('first') println map
  • 23. Map – find finding elements def map = [ first : &quot;Paul&quot;, last : &quot;Woods&quot;] def result1 = map.find { kv -> kv.value == &quot;Woods&quot; } println result1.getClass() println result1 def result2 = map.findAll { kv -> kv.key != &quot;last&quot; } println result2.getClass() println result2
  • 24.  
  • 25. Range – the basics Basic range operations def range = (1..5) println range println range.class range.each { n -> println n } println &quot;contains 5 : &quot; + range.contains(5) println &quot;contains 7 : &quot; + range.contains(7) range.step(2) { println it } def range2 = (new Date()-7 .. new Date()) range2.each { date -> println date }
  • 26.  
  • 27. Operation – ?. subscript The method will not be called if the object is null. def list = [ 'a', 'b', null, 'c', 'd' ] // list.each { item -> println item.toUpperCase() } list.each { item -> println item?.toUpperCase() }
  • 28. Operation – ?: conditional if object is false, return another object. else return the object def q = null println &quot;null : &quot; + (q ?: &quot;it is false&quot;) q = &quot;&quot; println &quot;empty : &quot; + (q ?: &quot;it is false&quot;) q = &quot;abc&quot; println &quot;value : &quot; + (q ?: &quot;it is false&quot;)
  • 29. Operation - <=> - spaceship calls the .compareTo method returns -1 if a < b returns +1 if a > b returns 0 if a == b println &quot;1 <=> 2 : &quot; + (1 <=> 2) println &quot;2 <=> 1 : &quot; + (2 <=> 1) println &quot;1 <=> 1 : &quot; + (1 <=> 1)
  • 30.  
  • 31. Closures - introduction A block of executable code, similar to a method, but it can be easily assigned to a variable, and passed to other methods. def add = { a, b -> a+b } println add(1,2)
  • 32. Closure - example A method that takes a closure class Names { def name1 def name2 def name3 def capitalize(Closure c) { c.call name1.capitalize() c.call name2.capitalize() c.call name3.capitalize() } } def names = new Names(name1:'paul', name2:'mike', name3:'eric') def greeting = { name -> println &quot;Hello, &quot; + name } names.capitalize greeting
  • 33. Closure - syntax Syntax for zero, 1 and 2+ parameter closures def zero = { -> println &quot;zero parameters&quot; } def one_a = { println &quot;one parameter : $it&quot; } def one_b = { a-> println &quot;one parameter : $a&quot; } def two = { a, b -> println &quot;two parameters : $a $b&quot; } zero.call() one_a.call('1a') one_b.call('1b') two.call('22', '2222')
  • 34. MultiAssign initialize or assign multiple variables with values from a list. def a def b (a, b) = [ 1, 2] println &quot;a=$a&quot; println &quot;b=$b&quot; def (c, d) = [ 3 , 4 ] println &quot;c=$c&quot; println &quot;d=$d&quot;
  • 35.  
  • 36. Optional parenthesis, semicolons, and returns In some situations, groovy allows you to remove parenthesis, semicolons and return statements.
  • 37. Optional – Parenthesis 1 No Arguments and no ‘get’ prefix – () mandatory class Name { def first, last def print() { println first + &quot; &quot; + last } def printDelim(delim) { println first + delim + last } def getFullName() { return first + &quot; &quot; + last } def getTotal(delim) { return first + delim + last } } def name = new Name(first:&quot;Paul&quot;, last:&quot;Woods&quot;) name.print()
  • 38. Optional – Parenthesis 2 One or more arguments and not referencing the return value – () optional class Name { def first, last def print() { println first + &quot; &quot; + last } def printDelim(delim) { println first + delim + last } def getFullName() { return first + &quot; &quot; + last } def getTotal(delim) { return first + delim + last } } def name = new Name(first:&quot;Paul&quot;, last:&quot;Woods&quot;) name.printDelim &quot; &quot;
  • 39. Optional – Parenthesis 3 The method has a ‘get’ prefix, and no arguments. () optional class Name { def first, last def print() { println first + &quot; &quot; + last } def printDelim(delim) { println first + delim + last } def getFullName() { return first + &quot; &quot; + last } def getTotal(delim) { return first + delim + last } } def name = new Name(first:&quot;Paul&quot;, last:&quot;Woods&quot;) println name.fullName
  • 40. Optional – Parenthesis 4 method has ‘get’ and client has 1 or more arguments – () mandatory class Name { def first, last def print() { println first + &quot; &quot; + last } def printDelim(delim) { println first + delim + last } def getFullName() { return first + &quot; &quot; + last } def getTotal(delim) { return first + delim + last } } def name = new Name(first:&quot;Paul&quot;, last:&quot;Woods&quot;) println name.getTotal(&quot;,&quot;)
  • 41. Optional – semicolons Semicolons are almost always optional Must be used if multiple statements on a single line. def a = 1 def b = 2 println a println b println a; println b
  • 42. Optional – returns – 1 Returns are optional when the value to be returned is the last line of the method. def sum(a, b) { a + b } def sub(a, b) { def total = a + b total }
  • 43. Optional – returns – 2 Returns are optional when the method is a if/else method. The value to be returned is the last line of each block. def choose(a, b, c) { if(a > 0) { b } else if(a < 0) { c } else { 0 } } println &quot; 1 : &quot; + choose( 1, 10, 20) println &quot;-1 : &quot; + choose(-1, 10, 20) println &quot; 0 : &quot; + choose( 0, 10, 20)
  • 45.  
  • 46. PowerAssert Power Assert – in a failed assert statement, groovy shows you the values of the objects. def map = [a : [ b : [ c : 2 ] ] ] assert 3 == map.a.b.c | | | | | | | | | 2 | | | {c=2} | | {b={c=2}} | {a={b={c=2}}} false
  • 47. PowerAssert - gotcha PowerAssert Gotcha – whitespace def a = &quot;a&quot; def b = &quot;a\r\n&quot; assert a == b Assertion failed: assert a == b | | | a | a false May not tell you about whitespace
  • 48.  
  • 49. Get the current groovy Version import  org.codehaus.groovy.runtime.InvokerHelper      println InvokerHelper.version  
  • 50.  
  • 51. Groovy A Dynamic scripting language similar to the Java language Executes on the JVM (1.5 and 1.6) Can be used stand-alone, or can be added to a Java application.
  • 52. Download and Install http://groovy.codehaus.org Download .zip Extract to c:\tools Set groovy_home = c:\tools\groovy-1.7.3 Add to your path: %groovy_home%\bin http://groovyconsole.appspot.com