Posted By

hansamann on 06/14/07


Tagged

closures


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

jsbournival
bootz15
hendersk
mbanfi
adaptives
michaelss


Groovy Series: Closures


 / Published in: Groovy
 

URL: http://hansamann.podspot.de/files/grails_podcast_episode_41.mp3

This snippet gives you some interesting examples about Groovy Closures. Listen to the audio to get the best learning experience. Simply right-click on the title above to download the mp3 file of this part of the series.

The Groovy Series is part of the Grails Podcast and can be subscribed to via: http://hansamann.podspot.de/rss. The series is produced by Dierk K├Ânig and Sven Haiges, further information about the topic of this episode can be found in the book

Groovy in Action - http://groovy.canoo.com/gina

  1. 3.times { println "Hello" }
  2.  
  3. //Whenever you see the curly braces of a closure think: "new Closure(){}".
  4. def c = { println 'I am a closure' }
  5. c.call()
  6. //.call or just closureName()
  7. c()
  8.  
  9. //as the output of this demo gets messy, we use a closure to print some separators
  10. def sep = { println '-'*45 }
  11. sep()
  12.  
  13. //the nice thing is you can pass closures around, just as parameters
  14. //this will print 'I am a closure' three times, as it is called
  15. //for each element.
  16. [1,2,3].each(c)
  17. sep()
  18.  
  19. //right, above code does not really make a lot of sense. The nice
  20. //thing is you can pass values to the closure.
  21. //Same example, slightly modified. It will print 1, 4, 9
  22. def square = { value -> println value * value }
  23. [1,2,3].each(square)
  24. sep()
  25.  
  26. //as you can leave out the parenthesis and directly pass a unnamed closure, the following is identical
  27. [1,2,3].each { value -> println value * value }
  28. sep()
  29.  
  30. //Groovy provides a default name for a single parameter passed to a closure: it
  31. //this prints 1,2,3
  32. [1,2,3].each { println it }
  33. sep()
  34.  
  35. //note you can reuse existing methods as closure. Methods are similar to closures, they just got a "name". They can be used as
  36. //closures via the method closure operator .&
  37. class SomeClass {
  38. void printSomeStuff(userName) {
  39. println "Stuff for " + userName;
  40. }
  41. }
  42. def myClass = new SomeClass()
  43. def closureFromClass = myClass.&printSomeStuff
  44.  
  45. //prints Stuff for Dierk
  46. closureFromClass("Dierk")
  47. sep()
  48.  
  49. //prints Stuff for Dierk, Sven, Wilma, Bill
  50. ['Dierk','Sven','Wilma','Bill'].each(closureFromClass)
  51. sep()
  52.  
  53. //default values
  54. def greeting = { first, last='Mueller' -> println "$first $last" }
  55. greeting("Hans")
  56. greeting("Michael Schumacher")
  57. sep()
  58.  
  59. //closure.getParameterTypes().size()
  60. def nameMap = ['Udo':'Mueller','Tanja':'Fleisch','Horst':'Hummel']
  61. nameMap.each {first, last='Mueller' -> println "$first $last"}
  62. sep()
  63. nameMap.each { entry -> println "${entry.key} ${entry.value}" }
  64. sep()
  65.  
  66. //Resource handling
  67. def outFile = new File("file.txt")
  68. outFile.withWriter { out -> out << "This is a line" }
  69. outFile.each { println it }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: adaptives on February 20, 2009

When a closure is given to the 'times' method, does the 'times' method internally use a 'for' or 'while' loop to invoke the piece of code?

You need to login to post a comment.