While looking for a script that display directory tree, I came across this Groovy script.
The code look just fine but it don't work (I test it with Groovy 1.7). After some google, I found that this is due to the closure as a first class object in Groovy, it can't be call within the closure itself.
And below is the solution:
def listRecursivly
listRecursivly = { file, arg ->
println "${'| ' * arg}`- ${file.getName()}\\"
file.eachFile() {f ->
if(f.directory) {
listRecursivly(f,(arg + 1))
} else {
println "${'| ' * (arg + 1)}`- ${f.getName()}"
}
}
}
listRecursivly (new File("."),0)
The little trick here is to define and initialize the closure in 2 separate lines.
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=63106ce6-8a28-4ad0-9616-fcfb3e859071)

No comments:
Post a Comment