En palabras de la Wikipedia:
In computer science, a closure is a first-class function with free variables that are bound in the lexical environment. Such a function is said to be "closed over" its free variables. A closure is defined within the scope of its free variables, and the extent of those variables is at least as long as the lifetime of the closure itself.
Un ejemplo:
generaciondecodigos@nereida:~/src/groovy/closures$ cat -n closure.groovy 1 #!/usr/bin/env groovy 2 3 def p(x) { 4 return { y -> x + y} 5 } 6 7 p2 = p(2) 8 9 println p2(5) 10 println p2(9) 11 12 println p2(4.5) 13 14 ph = p("hola ") 15 println ph("mundo") 16Al ejecutar este programa obtenemos la salida:
generaciondecodigos@nereida:~/src/groovy/closures$ ./closure.groovy 7 11 6.5 hola mundo