c
2011 Andrei Alexandrescu
1 / 33
Generic Programming Galore Using D
Andrei Alexandrescu, PhD
Research Scientist Facebook
Generic Programming Galore Using D Andrei Alexandrescu, PhD - - PowerPoint PPT Presentation
Generic Programming Galore Using D Andrei Alexandrescu, PhD Research Scientist Facebook 1 / 33 2011 Andrei Alexandrescu c 37% off 2 / 33 2011 Andrei Alexandrescu c Generic Programming 3 / 33 2011 Andrei Alexandrescu c What is
c
2011 Andrei Alexandrescu
1 / 33
Andrei Alexandrescu, PhD
Research Scientist Facebook
c
2011 Andrei Alexandrescu
2 / 33
c
2011 Andrei Alexandrescu
3 / 33
c
2011 Andrei Alexandrescu
4 / 33
c
2011 Andrei Alexandrescu
4 / 33
c
2011 Andrei Alexandrescu
4 / 33
c
2011 Andrei Alexandrescu
4 / 33
c
2011 Andrei Alexandrescu
4 / 33
c
2011 Andrei Alexandrescu
4 / 33
etier
c
2011 Andrei Alexandrescu
5 / 33
(macro style)
derangement”
c
2011 Andrei Alexandrescu
6 / 33
code
c
2011 Andrei Alexandrescu
7 / 33
handwritten version (true genericity)
c
2011 Andrei Alexandrescu
8 / 33
c
2011 Andrei Alexandrescu
9 / 33
c
2011 Andrei Alexandrescu
10 / 33
template CommonType(T...) { static if (T.length == 1) alias T[0] CommonType; else static if (is(typeof(1 ? T[0].init : T[1].init) U)) alias CommonType!(U, T[2 .. $]) CommonType; else alias void CommonType; } // Usage static assert(is(CommonType!(int, short, long) == long));
c
2011 Andrei Alexandrescu
11 / 33
auto min(T...)(T x) if (x.length > 1 && is(typeof(CommonType!T.init < CommonType!T.init) == bool)) { static if (x.length > 2) return min(min(x[0], x[1]), x[2 .. $]); else return x[0] > x[1] ? x[1] : x[0]; }
c
2011 Andrei Alexandrescu
12 / 33
auto min(R)(R range) if (isInputRange!R && is(typeof(range.front < range.front) == bool)) { auto result = range.front; range.popFront(); foreach (e; range) { if (e < result) result = e; } return result; } auto m = [ 1, 5, 2, 0, 7, 9 ].min();
c
2011 Andrei Alexandrescu
13 / 33
auto argmin(alias fun, R)(R r) if (isInputRange!R && is(typeof(fun(r.front) < fun(r.front)) == bool)) { auto result = r.front; auto cache = fun(result); r.popFront(); foreach (e; r) { auto cand = fun(e); if (cand > cache) continue; result = e; cache = cand; } return result; }
c
2011 Andrei Alexandrescu
14 / 33
c
2011 Andrei Alexandrescu
15 / 33
c
2011 Andrei Alexandrescu
16 / 33
specialization
c
2011 Andrei Alexandrescu
17 / 33
c
2011 Andrei Alexandrescu
18 / 33
c
2011 Andrei Alexandrescu
18 / 33
c
2011 Andrei Alexandrescu
18 / 33
c
2011 Andrei Alexandrescu
18 / 33
c
2011 Andrei Alexandrescu
18 / 33
c
2011 Andrei Alexandrescu
18 / 33
c
2011 Andrei Alexandrescu
19 / 33
c
2011 Andrei Alexandrescu
20 / 33
c
2011 Andrei Alexandrescu
21 / 33
effective
c
2011 Andrei Alexandrescu
22 / 33
c
2011 Andrei Alexandrescu
23 / 33
c
2011 Andrei Alexandrescu
24 / 33
c
2011 Andrei Alexandrescu
25 / 33
c
2011 Andrei Alexandrescu
26 / 33
c
2011 Andrei Alexandrescu
27 / 33
c
2011 Andrei Alexandrescu
28 / 33
c
2011 Andrei Alexandrescu
29 / 33
compiles it
c
2011 Andrei Alexandrescu
31 / 33
c
2011 Andrei Alexandrescu
32 / 33
promise
c
2011 Andrei Alexandrescu
33 / 33