I am unable to call recursive closures in Groovy 2.1.9
def facRec = {long n->
return n>1 ? n * facRec(n - 1) : 1
}
I'm getting a TypeMissmatch
It is used to separate where you declare bindings for your closure from the actual code, eg: def myClosure = { x, y -> x + y } the part before -> declares that the closure has two arguments named x and y while the second part is the code of the closure.
continue has nothing to do with the recursive call; its effect is to skip the rest of the contents of the while loop and go straight back to in. hasNext() .
A closure is a function bound to (or executed against) some object instances, which can be one of these three things: this, owner, and delegate. this: the instance of the enclosing class. owner: same as this, or the enclosing closure if exists. delegate: same as owner, but can be changed.
When the closure is being defined, it has no idea of the variable facRec
as it has not yet been defined...
You can do:
def facRec
facRec = {long n->
return n>1 ? n * facRec(n - 1) : 1
}
To get around this, or you can wrap the inner into another closure and call the owner of that inner closure (though I would tend to do the above as it is easier to read):
def facRec = {long n->
{ -> n > 1 ? n * owner.call( n - 1 ) : 1 }()
}
It should be noted that both of these will fail for big values of n
as you will overflow the stack
You can use trampoline to get round this:
def facRec
facRec = { n, count = 1G ->
n > 1 ? facRec.trampoline( n - 1, count * n ) : count
}.trampoline()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With