Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. def x = 5
  2.  
  3. def clos = {println "Hello World!"}
  4. println "Executing the Closure:"
  5. clos()
  6.  
  7. def sum = {a, b -> println a+b}
  8. sum(4, 5)
  9.  
  10. def multiplyBy = {num -> num * x}
  11. println multiplyBy(10)
  12.  
  13. // If you have a Closure that takes a single argument, you may omit the
  14. // parameter definition of the Closure
  15. def printMessage = {println it}
  16. printMessage("hi")
  17.  
  18. /*
  19.  Groovy can memoize closure results [1][2][3]
  20. */
  21. def cl = { a, b ->
  22. sleep(3000)
  23. a + b
  24. }
  25. mem = cl.memoize()
  26.  
  27. def callClosure(a,b) {
  28. def start = System.currentTimeMillis()
  29. mem(a,b)
  30. println "Inputs(a = $a, b = $b)-took ${System.currentTimeMillis() - start}msecs."
  31. }
  32.  
  33. callClosure(1,2)
  34. callClosure(1,2)
  35. callClosure(1,2)
  36. callClosure(1,2)
  37. callClosure(1,2)
  38. callClosure(1,2)
  39. callClosure(1,2)
  40. callClosure(1,2)
  41. callClosure(1,2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement