Groovy web console

subscribe to the feed Subscribe
to this
Memoization for recursive closures (via #groovywebconsole)
tweet this snippet Tweet

Memoization for recursive closures

Published 8 years ago by Michail Antonov with tags memoization recursion
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def startTime = System.currentTimeMillis()

def fact = {it -> sleep(100); it ?  it * call(it - 1g) : 1g}
fact = fact.memoize()
println fact(70)
println fact(71)

println ((System.currentTimeMillis() - startTime) / 1000.0)