Mafi:
> I am not able to instatiate ths template in any way.

You are doing it wrong. Such template tricks are not easy to get right, so 
probably the best thing you may do is to start from an empty program that 
works, and then keep adding to it, and keep it working, until you reach 
something good enough. If you write ten lines of code then later it's hard to 
make them work :-)

This is an implementation that seems to work, but it's not nice, maybe you can 
improve it, I have had to use a delegate because I have found a forward 
reference problem (I am not sure if it's worth for Bugzilla):


import std.stdio: writeln;
import std.typecons: Tuple, tuple;
import std.traits: ReturnType, ParameterTypeTuple;

struct Memoize(alias F) {
    ReturnType!F opCall(ParameterTypeTuple!F args) {
        alias ReturnType!F ResultType;
        static ResultType[Tuple!(ParameterTypeTuple!F)] cache;
        auto ptr = tuple(args) in cache;
        if (ptr == null) {
            auto result = F(args);
            cache[tuple(args)] = result;
            return result;
        } else {
            return *ptr;
        }
    }
}

pure int fib(int n) {
    if (n < 2)
        return n;
    return fib(n - 1) + fib(n - 2);
}

void main() {
    pure int delegate(int n) cfib_impl;
    Memoize!cfib_impl cfib;

    cfib_impl = (int n) {
        if (n < 2)
            return n;
        return cfib(n - 1) + cfib(n - 2);
    };

    enum int N = 40;
    writeln("fib(", N, ") = ", fib(N));
    writeln("cfib(", N, ") = ", cfib(N));
}

Bye,
bearophile

Reply via email to