Using Groovy closures to fake Python generators

Here is a quick example of faking python generators with Groovy. Please update the comments if you have a better approach.

def (a,b,sent,acc) = [0,1,null,0]
def fib={
    (a,b) = [b,a+=b]

    currFibValue = fib()
    if( currFibValue > 4000000) break
    else if( currFibValue % 2 ==0) acc += currFibValue
println acc

Leave a Reply

Your email address will not be published.