Brian's Waste of Time

Thu, 25 Mar 2004

Closure Recursion in Groovy

Wrote a fun little groovy script earlier today to copy IMAP messages to Exchange. JavaMail and Groovy made this pretty simple, so simple I had to do a bit of gold plating. Oh well =)

The fun part is in this snippet:


traverse = { folder, operation |
    traverse_subfolders = { folder, operation |
        recur = this
        folder.list().each { 
            operation(it)
            recur(it, operation)
        }
    }
    operation(folder)
    traverse_subfolders(folder, operation)
}

Groovy suffers from a cannot-call-closure-by-name-from-itself problem. Because the closure is a first class object though, you have access to it via this, so you can, in fact, recur on your closure.

writebacks...

comment...

 
Name:
URL/Email: [http://... or mailto:you@wherever] (optional)
Title: (optional)
Spam Guard, translate l33t to English: (hint, it's an Australian animal, plural form)
Comments:
Save my Name and URL/Email for next time