Question

I have the following piece of code

def normalFactorial = { BigInteger n ->
n <= 1 ? 1 : normalFactorial(n - 1) * n
}

println normalFactorial(1)
println normalFactorial(2)

normalFactorial(1) method works fine and prints 1 as expected. The second call fails with the below exception. Any clues.. ????

May 09, 2013 10:39:23 PM org.codehaus.groovy.runtime.StackTraceUtils sanitize

WARNING: Sanitizing stacktrace:

groovy.lang.MissingMethodException: No signature of method: tailRecursion.normalFactorial() is applicable for argument types: (java.math.BigInteger) values: [1]

    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
Was it helpful?

Solution

The closure isn't defined when you define the closure (if that makes sense)

Try:

def normalFactorial
normalFactorial = { BigInteger n ->
  n <= 1 ? 1 : normalFactorial(n - 1) * n
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top