|
1 |
| -// Type Checking |
2 |
| -// Copyright (c) 2016 Sport Trades Ltd |
| 1 | +// Type Checking and Normalisation |
| 2 | +// Copyright (c) 2016 - 2020 Sport Trades Ltd |
3 | 3 |
|
4 | 4 | // Documentation: https://github.com/BuaBook/kdb-common/wiki/type.q
|
5 | 5 |
|
| 6 | +/ All infinite values |
| 7 | +/ @see .type.isInfinite |
6 | 8 | .type.const.infinites:raze (::;neg)@\:(0Wh;0Wi;0Wj;0We;0Wf;0Wp;0Wm;0Wd;0Wz;0Nn;0Wu;0Wv;0Wt);
|
7 | 9 |
|
| 10 | +/ Mapping of type name based on index in the list (matching .Q.t behaviour) |
| 11 | +.type.const.types:`mixedList`boolean`guid``byte`short`integer`long`real`float`character`symbol`timestamp`month`date`datetime`timespan`minute`second`time; |
8 | 12 |
|
9 |
| -.type.isSymbol:{ |
10 |
| - :-11h~type x; |
11 |
| - }; |
12 |
| - |
13 |
| -.type.isString:{ |
14 |
| - :10h~type x; |
15 |
| - }; |
| 13 | +/ Function string to use for all .type.is* functions for higher performance |
| 14 | +.type.const.typeFunc:"{ --TYPE--~type x }"; |
16 | 15 |
|
17 |
| -.type.isBoolean:{ |
18 |
| - :-1h~type x; |
19 |
| - }; |
20 | 16 |
|
21 |
| -.type.isTimestamp:{ |
22 |
| - :-12h~type x; |
| 17 | +.type.init:{ |
| 18 | + types:.type.const.types where not null .type.const.types; |
| 19 | + .type.i.setCheckFuncs each types; |
23 | 20 | };
|
24 | 21 |
|
25 |
| -.type.isDate:{ |
26 |
| - :-14h~type x; |
27 |
| - }; |
28 | 22 |
|
29 |
| -.type.isTime:{ |
30 |
| - :-19h~type x; |
| 23 | +.type.isString:{ |
| 24 | + :10h~type x; |
31 | 25 | };
|
32 | 26 |
|
33 | 27 | .type.isNumber:{
|
34 | 28 | :type[x] in -5 -6 -7 -8 -9h;
|
35 | 29 | };
|
36 | 30 |
|
37 |
| -/ NOTE: This function checks for a mathematical integer (i.e. whole number) |
38 |
| -.type.isInteger:{ |
| 31 | +.type.isWholeNumber:{ |
39 | 32 | :type[x] in -5 -6 -7h;
|
40 | 33 | };
|
41 | 34 |
|
|
52 | 45 | };
|
53 | 46 |
|
54 | 47 | .type.isHostPort:{
|
55 |
| - :.type.isInteger[x] | (.type.isSymbol[x] & 2 <= count where ":" = string x); |
| 48 | + :.type.isLong[x] | (.type.isSymbol[x] & 2 <= count where ":" = string x); |
56 | 49 | };
|
57 | 50 |
|
58 | 51 | .type.isDict:.type.isDictionary:{
|
|
119 | 112 | :type[x] within 0 19h;
|
120 | 113 | };
|
121 | 114 |
|
122 |
| -.type.isMixedList:{ |
123 |
| - :0h~type x; |
124 |
| - }; |
125 |
| - |
126 | 115 | .type.isDistinct:{
|
127 | 116 | :x~distinct x;
|
128 | 117 | };
|
|
159 | 148 | '"IllegalArgumentException";
|
160 | 149 | ];
|
161 | 150 |
|
162 |
| - if[.type.isInteger x; |
| 151 | + if[.type.isLong x; |
163 | 152 | : `$"::",string x;
|
164 | 153 | ];
|
165 | 154 |
|
166 | 155 | :x;
|
167 | 156 | };
|
| 157 | + |
| 158 | + |
| 159 | +/ Builds type checking functions .type.is*Type* and .type.is*Type*List from a string template function for highest performance |
| 160 | +/ @param typeName (Symbol) The name of the type to build the functions for |
| 161 | +/ @see .type.const.types |
| 162 | +.type.i.setCheckFuncs:{[typeName] |
| 163 | + listType:`short$.type.const.types?typeName; |
| 164 | + typeName:@[string typeName; 0; upper]; |
| 165 | + |
| 166 | + atomName:`$"is",typeName; |
| 167 | + listName:`$"is",typeName,"List"; |
| 168 | + |
| 169 | + set[` sv `.type,atomName;] get ssr[.type.const.typeFunc; "--TYPE--"; .Q.s1 neg listType]; |
| 170 | + |
| 171 | + / If type 0, don't create the list version |
| 172 | + if[not listType = neg listType; |
| 173 | + set[` sv `.type,listName;] get ssr[.type.const.typeFunc; "--TYPE--"; .Q.s1 listType]; |
| 174 | + ]; |
| 175 | + }; |
| 176 | + |
0 commit comments