Groovy every day

3,626 views
3,521 views

Published on

My slides for a talk given to the Dallas FortWorth groovy and grails user group.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,626
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Groovy every day

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

×