On Thursday, 13 April 2017 at 11:16:46 UTC, crimaniak wrote:
On Thursday, 13 April 2017 at 05:51:27 UTC, Dukc wrote:
auto use(alias F, T)(T t){return F(t);}

void main()
{   import std.stdio;
    foreach(i; 1 .. 11)
    {   foreach(j; 1 .. 11) write((i * j).use!(x => x*x), " ");
        writeln;
    }
}

This way, you can avoid writing long expressions twice with UFCS.
If fact you don't need any template to do this. Try the next:

    foreach(i; 1 .. 11)
    {   foreach(j; 1 .. 11) write((x => x*x)(i * j), " ");
        writeln;
    }

Thats a good one, wrote that down for next time. With that I'll share another one I read about that I thought was really cool:

import std.stdio;
import std.functional: memoize;

ulong fib(ulong n) {
    alias mfib = memoize!(fib);
    return n < 2 ? 1 : mfib(n-2) + mfib(n-1);
}

void main() {
    foreach (x; 1..45) {
        writefln("%s", x.fib);
    }
}

What this does is make calculating the recursive fibonacci sequence much faster. I don't know too much about the technique, but having it in my optimizations.txt is always a good thing. Now that I think of it, I wonder if there is a page on this website that lists common optimization techniques.

Reply via email to