1 module yu.functional; 2 3 public import std.functional; 4 public import std.traits; 5 import std.typecons; 6 import std.typetuple; 7 8 ///Note:from GC 9 pragma(inline) auto bind(T, Args...)(T fun, Args args) if (isCallable!(T)) { 10 alias FUNTYPE = Parameters!(fun); 11 static if (is(Args == void)) { 12 static if (isDelegate!T) 13 return fun; 14 else 15 return toDelegate(fun); 16 } else static if (FUNTYPE.length > args.length) { 17 alias DTYPE = FUNTYPE[args.length .. $]; 18 return delegate(DTYPE ars) { 19 TypeTuple!(FUNTYPE) value; 20 value[0 .. args.length] = args[]; 21 value[args.length .. $] = ars[]; 22 return fun(value); 23 }; 24 } else { 25 return delegate() { return fun(args); }; 26 } 27 } 28 29 unittest { 30 31 import std.stdio; 32 import core.thread; 33 34 class AA { 35 void show(int i) { 36 writeln("i = ", i); // the value is not(0,1,2,3), it all is 2. 37 } 38 39 void show(int i, int b) { 40 b += i * 10; 41 writeln("b = ", b); // the value is not(0,1,2,3), it all is 2. 42 } 43 44 void aa() { 45 writeln("aaaaaaaa "); 46 } 47 48 void dshow(int i, string str, double t) { 49 writeln("i = ", i, " str = ", str, " t = ", t); 50 } 51 } 52 53 void listRun(int i) { 54 writeln("i = ", i); 55 } 56 57 void listRun2(int i, int b) { 58 writeln("i = ", i, " b = ", b); 59 } 60 61 void list() { 62 writeln("bbbbbbbbbbbb"); 63 } 64 65 void dooo(Thread[] t1, Thread[] t2, AA a) { 66 foreach (i; 0 .. 4) { 67 auto th = new Thread(bind!(void delegate(int, int))(&a.show, i, i)); 68 t1[i] = th; 69 auto th2 = new Thread(bind(&listRun, (i + 10))); 70 t2[i] = th2; 71 } 72 } 73 74 // void main() 75 { 76 auto tdel = bind(&listRun); 77 tdel(9); 78 bind(&listRun2, 4)(5); 79 bind(&listRun2, 40, 50)(); 80 81 AA a = new AA(); 82 bind(&a.dshow, 5, "hahah")(20.05); 83 84 Thread[4] _thread; 85 Thread[4] _thread2; 86 // AA a = new AA(); 87 88 dooo(_thread, _thread2, a); 89 90 foreach (i; 0 .. 4) { 91 _thread[i].start(); 92 } 93 94 foreach (i; 0 .. 4) { 95 _thread2[i].start(); 96 } 97 98 } 99 }