1 /** 2 Copyright 2018 Mark Fisher 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy of 5 this software and associated documentation files (the "Software"), to deal in 6 the Software without restriction, including without limitation the rights to 7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 8 of the Software, and to permit persons to whom the Software is furnished to do 9 so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in all 12 copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 SOFTWARE. 21 **/ 22 module dxx.util.algo; 23 24 25 import std.traits, 26 std.algorithm, 27 std.typetuple; 28 29 version(unittest) { 30 import std.stdio, 31 std.conv; 32 import core.stdc.stdarg; 33 } 34 35 alias Identity(alias Func) = Func; 36 37 auto ref Apply(alias Func,arg ...)() { return Func(arg); } 38 39 auto ref _Apply(alias Func,Arg=Parameters!Func)(Arg a) { return Func(a); } 40 41 //auto ref Apply(alias Func,A)() { return Func(A); } 42 43 //auto ref apply(alias Next,alias Func,Arg ...)(Arg a) { 44 // //return Apply!(Next,Apply!(Func,a)); 45 // return Next(Func(a)); 46 //} 47 48 auto ref apply(alias Next,alias Func,Arg=Parameters!Func)(Arg a) { 49 return Next(Func(a)); 50 } 51 52 auto ref applyNext(alias Next,alias Func,Arg=Parameters!Func)(Arg a) { 53 Func(a); 54 return Next(); 55 } 56 57 template lambda(alias Func) { 58 alias lambda = ()=>Func; 59 } 60 61 unittest { 62 writeln("Test apply"); 63 static int f(int a) { return a + 1; } 64 assert(Apply!(f,10)==11); 65 static int g() { return 1; } 66 assert(Apply!(g)==1); 67 static int h(int a,int b) { return a+b; } 68 assert(Apply!(h,2,3)==5); 69 assert(_Apply!h(2,3)==5); 70 } 71 72 void Assign(alias F,alias T)() { F=T; } 73 74 void AssignTo(alias F,T=typeof(F))(T t) { F=t; } 75 76 void assignTo(F,T=F)(ref F f,T t) { f=t; } 77 78 void AssignTrue(alias F)() { Assign!(F,true); } 79 80 void AssignFalse(alias F)() { Assign!(F,false); } 81 82 auto ref Append(alias F,alias T)() { return F~T; } 83 84 auto ref _Append(alias F,T=typeof(F))(T t) { return F~t; } 85 86 auto ref Prepend(alias F,alias T)() { return T~F; } 87 88 auto ref _Prepend(alias F,T=typeof(F))(T t) { return t~F; } 89 90 91 template Map(alias Func,args...) { 92 static if (args.length > 1) 93 alias Map = TypeTuple!(Apply!(Func,args[0]),Map!(Func,args[1..$])); 94 else 95 alias Map = Apply!(Func,args[0]); 96 } 97 98 unittest { 99 static void test(int x,int y,int z) { 100 assert(x==4 && y==5 && z==6); 101 } 102 static int add2(int a) { return a+2; } 103 int a=2,b=3,c=4; 104 test(Map!(add2,a,b,c)); 105 test(Map!(add2,2,3,4)); 106 alias x = Map!(add2,2,3,4); 107 test(x); 108 } 109 unittest { 110 static int square(int arg) 111 { 112 return arg * arg; 113 } 114 115 static int refSquare(ref int arg) 116 { 117 arg *= arg; 118 return arg; 119 } 120 121 static ref int refRetSquare(ref int arg) 122 { 123 arg *= arg; 124 return arg; 125 } 126 127 static void test(int a, int b) 128 { 129 assert(a == 4); 130 assert(b == 16); 131 } 132 133 static void testRef(ref int a, ref int b) 134 { 135 assert(a++ == 16); 136 assert(b++ == 256); 137 } 138 139 static int a = 2; 140 static int b = 4; 141 142 test(Map!(square, a, b)); 143 144 test(Map!(refSquare, a, b)); 145 assert(a == 4); 146 assert(b == 16); 147 148 testRef(Map!(refRetSquare, a, b)); 149 assert(a == 17); 150 assert(b == 257); 151 } 152 153 //template Reduce(alias Func,alias Param,args...) { 154 //static if (args.length > 1) 155 // alias Reduce = Apply!(Func,args[0],Reduce!(Func,args[1..$])); 156 //else 157 // alias Reduce = Apply!(Func,args[0],Param); 158 //} 159 // 160 template removeElement(T,alias V) { 161 auto ref removeElement(ref T t) { return t.remove(a => a is V); } 162 } 163 164