Groovy web console

subscribe to the feed Subscribe
to this
site
Recursive closure with trampoline (via #groovywebconsole)
tweet this snippet Tweet
this
script

Recursive closure with trampoline

Published 7 years ago by mrhaki with tags groovy18 closure
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def sizeList
sizeList = { list, counter = 0 ->
    if (list.size() == 0) {
        counter
    } else {
        sizeList.trampoline(list.tail(), counter + 1) 
    }
}
sizeList = sizeList.trampoline()

assert sizeList(1..10000) == 10000