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)
有帮助吗?

解决方案

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
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top