Code from a Rosetta Code Task

Ali Çehreli acehreli at yahoo.com
Thu Aug 29 21:31:03 PDT 2013


On 08/29/2013 08:53 PM, Meta wrote:

 > However, after some fiddling, it seems that this is actually not 
usable with
 > anonymous functions, or at least, I couldn't find a way to do it.

Borrowing the "self" trick from the existing solution, the following 
satisfies all of the requirements:

int fib(int arg) pure {
     enforce(arg >= 0);

     return function int (int n) pure nothrow {
         auto self = __traits(parent, {});
         return (n < 2) ? n : self(n - 1) + self(n - 2);
     }(arg);
}

Ali



More information about the Digitalmars-d-learn mailing list