Add tags

Add tags to the following comment

テンプレート引数の推論を使って型無しラムダっぽく。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import std.stdio;

void main() {    
    auto Y = new class {
        auto opCall(_)(_ f) {
            return (new class {
                auto opCall(_)(_ g) {
                    return new class {
                        auto opCall(_)(_ x) {
                            return f(g(g))(x);
                        }
                    };
                }
            })(new class {
                auto opCall(_)(_ g) {
                    return new class {
                        auto opCall(_)(_ x) {
                            return f(g(g))(x);
                        }
                    };
                }
            });
        };
    };
    
    auto F = new class {
        auto opCall(_)(_ f) {
            return new class {
                auto opCall(_)(_ x) {
                    if (x == 0) {
                        return cast(_)1;
                    } else {
                        return x * f(x - 1);
                    }
                }
            };
        }
    };

    auto factorial = Y(F);
    writeln("fact(5) = ", factorial(10));
    writeln("fact(5) = ", factorial(20L));
}

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...