Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def x = 5
- def clos = {println "Hello World!"}
- println "Executing the Closure:"
- clos()
- def sum = {a, b -> println a+b}
- sum(4, 5)
- def multiplyBy = {num -> num * x}
- println multiplyBy(10)
- // If you have a Closure that takes a single argument, you may omit the
- // parameter definition of the Closure
- def printMessage = {println it}
- printMessage("hi")
- /*
- Groovy can memoize closure results [1][2][3]
- */
- def cl = { a, b ->
- sleep(3000)
- a + b
- }
- mem = cl.memoize()
- def callClosure(a,b) {
- def start = System.currentTimeMillis()
- mem(a,b)
- println "Inputs(a = $a, b = $b)-took ${System.currentTimeMillis() - start}msecs."
- }
- callClosure(1,2)
- callClosure(1,2)
- callClosure(1,2)
- callClosure(1,2)
- callClosure(1,2)
- callClosure(1,2)
- callClosure(1,2)
- callClosure(1,2)
- callClosure(1,2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement