Skip to content
Advertisement

Tracing a recursive method in Java/Kotlin

In python, I can write a trace function like:

JavaScript

and it’d print the recursive trace for any general recursive function beautifully:

JavaScript

Is it possible to write a simple similar one in Java/Kotlin? Or is it only doable with AOP or the like?

Advertisement

Answer

EDIT: formatting does not 100% match yours, but that is easily fixable for you 🙂

Here is a code sample to accompany my comment. It is pretty ugly though but shows how its working. You can’t express it with a simple lambda as you cannot recurse lambdas in Java easily:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement