Calling an anonymous function from its own body
Hello,
I had a question regarding recursion calls to anonymous function.
Suppose I have an anonymous function and I want to make it recursive:
x => if x == 0 then 0 else x + callToItself(x - 1)
Obviously, callToItself
is not valid syntax, but is there a way to implement this in Scala ? I think it would be nice if we could do that without having to use named functions like this, which clutters the code in my opinion:
x =>
def f(x): Int => Int = if x == 0 then 0 else x + f(x - 1)
f(x)
I've thought of using this.apply
but it does not compile either.