-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttuple.d
331 lines (237 loc) · 6.67 KB
/
ttuple.d
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
struct TestMemberTuple1(A...){
A a;
auto foo()const{
static assert(is(typeof(a[0])==const(int)));
return a[0];
}
}
int testMemberTuple1(){
TestMemberTuple1!int a;
}
int testZeroArgs(){
int x;
int foo(){ return x; }
return foo((x++,Seq!())); // TODO!
}
pragma(msg, testZeroArgs());
auto seqret(int a, int b){auto x=Seq!(a,b);return x;}
auto seqtak(){auto x = seqret(1,2); return x[0];}
pragma(msg, "seqtak: ", seqtak());
//pragma(msg, seqret(1,2)); // TODO!
auto compose(alias a, alias b,T...)(T args){
return a(b(args));
}
auto seqid(T...)(T args){
return args;
}
auto add(int a, int b, int c)=>a+b+c;
pragma(msg, "multiret: ", compose!(add,seqid)(1,2,3));
int testmultirefret(){
int a, b;
ref Seq!(int, int) multirefret(){
return Seq!(a,b); // TODO
}
multirefret()=Seq!(1,2); // TODO
return a+b;
}
pragma(msg, "testmultirefret: ", testmultirefret());
T seq(T...)(T args){ return args; }
pragma(msg, (()=>[seq(1,2,3,4)])());
pragma(msg, [seq(1,2,3,4)]); // TODO
struct TupleExpand{
template Cont(R,A){ alias R delegate(R delegate(A)) Cont; }
auto callCC(T...)(T args){
Cont!(int,int) delegate(Cont!(int,int) delegate(int),int) f;
return (int delegate(int) k)=>f(a=>_=>k(a), args)(k);
}
auto testcallCC(){
assert(callCC(1)(x=>x)==1);
}
}
/+
struct Tpl{
Seq!(int, double) foo;
int a,b;
alias Seq!(a,b) c;
}
void checkTpl(){
void fun(int, double){ }
Tpl t;
fun(t.c); // TODO!
t.c[0]=2; // TODO!
t.foo[0]=2; // TODO!
}
+/
// alias Seq Seq;
auto seqparm(A,B,C...)(Seq!(A,B,C) args){
return args[0]+args[1]+args[2]+args[$-1];
}
static assert(seqparm(1,2,3.0,4,5)==11.0);
pragma(msg, "seqparm: ",seqparm(1,2,3.0,4,5));
pragma(msg, Seq!(1,2,3,4)[1..3]);
Seq!(int,double,float,long)[1..3] xx;
pragma(msg, typeof(xx));
pragma(msg, "length: ",Seq!(1,2,3).length);
pragma(msg, "$ 1: ", Seq!(1,2,3)[$-1]);
pragma(msg, "$ 2: ", Seq!(int,double)[$-1]);
pragma(msg, "$ 3: ", Seq!(1,2,3)[1..$]);
pragma(msg, "$ 4: ", Seq!(int,int,int)[1..$]);
void convErrMsg(){
Seq!(int, double) a=Seq!(1.0,2); // error
}
static assert(!is(Seq!(int, double)==int));
template Fst(T...){
alias T[0] Fst;
}
pragma(msg, "Fst: ",[Seq!(Fst!(Seq!("133",2,3,4)),"2","3","4")]);
/+int main(){
Seq!(int,int) x = Seq!(1,2);
x[1]=3;
x[0]-=11;
return x[0]+x[1];
}+/
//pragma(msg, main());
auto refe(ref Seq!(int,int,int,int) arg){
return ++arg[0]+ ++arg[1]+ --arg[2]+ --arg[3];
}
auto testt(ref int a, ref int b, ref int c, ref int d){
a++; b++; c++; d++;
}
auto testAliasTuple(){
int a,b,c,d;
int x;
//alias Seq!(a,b,c,d) tp;
//tp=++x;
Seq!(int,int,int,int) tp=++x;
//a=1;b=2;c=1;d=3;
//pragma(msg, typeof({tp;tp[0];}));
//(){testt(tp);}();
//alias f_d a_d;
//static void foo(){tp[0]=2;}
(){testt(tp);}();
x=333;
auto r=[tp]~{testt(tp); return [tp];}()~refe(tp)~[tp];
//return r[1..12];
return r;
}
static assert(testAliasTuple()==[2,3,4,5,3,4,5,6,18,4,5,4,5]);
pragma(msg, testAliasTuple());
pragma(msg, Seq!(1,2,3));
pragma(msg, typeof(Seq!(1,2,3)));
pragma(msg, Seq!(1,int,3));
pragma(msg, typeof(Seq!(1,int,3)));
pragma(msg, Seq!(float,int,double));
static assert(is(Seq!(int,double)==Seq!(int,double)));
static assert(!is(Seq!(double,int)==Seq!(int,double)));
static assert(is(Seq!(int delegate(int, int[]))==Seq!(int delegate(int,int[]))));
pragma(msg, Seq!(1,Seq!(int,double),2,Seq!(float,real,int[])));
Seq!(1,2,3)[1] aaa; // error
pragma(msg, Seq!(1,2,3,Seq!(1,2,3)));
//pragma(msg, Seq!(int,double)[0]);
Seq!(int,[2])[0] as;
pragma(msg, typeof(as));
//pragma(msg, x);
Alias!(Seq!(int,2)) a; // error
static assert(Seq!(0,"test")); // error
static assert(Seq!(1,""));
void main(){assert(Seq!(1,""));}
mixin(Seq!("int x;"));
int[Seq!(1,2)] ad; // error
alias Seq!(1,2) start;
enum Seq!(int,int) aii = 12;
//pragma(msg, aii[Seq!(start,3,5,11)]);
//Seq!(int,double) aid = Seq!(1,1,2);
void test(){
// aid = Seq!(1,1,2);
}
immutable int[2] a2=[1,2];
//pragma(msg, a[1..3]);
pragma(msg, a2[1]);
template StaticMap(alias F, a...){
static if(a.length) alias Seq!(F!(a[0]), StaticMap!(F,a[1..a.length])) StaticMap;
else alias Seq!() StaticMap;
}
template StaticFoldr(alias F, a...){
static if(a.length==2) alias F!(a[0],a[1]) StaticFoldr;
else alias F!(a[0],StaticFoldr!(F, a[1..a.length])) StaticFoldr;
}
template CommonType(A,B){ alias typeof({A a; B b; return 1?a:b;}()) CommonType; }
pragma(msg, StaticFoldr!(CommonType, int, double, short, real));
template TimesTwo(int x){ enum TimesTwo = x+x; }
template Square(int x){ enum Square = x*x; }
pragma(msg, [StaticMap!(TimesTwo,StaticIota!(1,31))]);
pragma(msg, StaticMap!(Square,StaticIota!(1,31)));
template StaticFilter(alias F, a...){
static if(!a.length) alias a StaticFilter;
else static if(F!(a[0])) alias Seq!(a[0], Rest) StaticFilter;
else alias Rest StaticFilter;
static if(a.length) alias StaticFilter!(F,a[1..a.length]) Rest;
}
bool any(alias a,R)(R r, int len){// if(is(typeof(a(r[0])) == bool)) {
for(int i=0;i<len;i++) if(a(r[i])) return true;
return false;
}
static bool lower(char s) { return 'a'<=s&&s<='z'; }
alias immutable(char)[] string;
template isUppercase(string s) if(is(typeof(s[2]))&&!is(typeof(s[3]))){
enum isUppercase = {
static bool lower(char s) { return 'a'<=s&&s<='z'; }
return !any!(lower, string)(s,3);
}();
}
//pragma(msg, isUppercase!"123");
//pragma(msg, isUppercase!"aBc");
//pragma(msg, isUppercase!"ABC");
//pragma(msg, isUppercase!"AbC");
//pragma(msg, isUppercase!"DEF");
pragma(msg, StaticFilter!(isUppercase,"123","aBc",Seq!("ABC","AbC"),"DEF"));
template StaticIota(int a, int b) if(a<=b){
static if(a==b) alias Seq!() StaticIota;
else alias Seq!(a,StaticIota!(a+1,b)) StaticIota;
}
template Divides(int a){
template Divides(int b){
enum Divides = a%b==0;
}
}
//pragma(msg, (Divides!2)!2);
template IsPrime(int p){
//enum IsPrime = StaticFilter!(Divides!p, StaticIota!(1,p+1)).length==2;
enum IsPrime = {
int r;
for(int i=1;i<=p;i++) r+=!(p%i);
return r;
}()==2;
}
pragma(msg, IsPrime!2);
pragma(msg, IsPrime!4);
pragma(msg, IsPrime!5);
pragma(msg, IsPrime!13);
//pragma(msg, IsPrime!20);
/+pragma(msg, IsPrime!53);
pragma(msg, IsPrime!70);
pragma(msg, IsPrime!103);
pragma(msg, IsPrime!103);+/
pragma(msg, ListPrimes!103);
template Primes(int i){
alias StaticFilter!(IsPrime, StaticIota!(2,i+1)) Primes;
}
template ListPrimes(int upper){
enum ListPrimes = H!2;
template H(int lower){
enum H = "";
static if(lower <= upper){
static if(IsPrime!lower){pragma(msg, lower,H!(lower+1));}
else enum next=H!(lower+1);
}
}
}
pragma(msg, StaticIota!(1,12)," ", Primes!12);
//pragma(msg, StaticIota!(2,100).V);
//pragma(msg, a[0].mangleof);
//pragma(msg, _a_field_0.mangleof);
// +/
// +/
// +/
template Seq(T...){ alias T Seq; }
template Alias(T){ alias T Alias; }