Skip to content

Commit 2767bc0

Browse files
committed
type: Add is* and is*List type checking functions for all types [#29]
- Created on '.type.init' - Bug fix .type.isHostPort returned true for short and integer types which would then fail if passed into hopen (only long type supported) - Ensure '.util.isDistinct' is a copy of '.type.isDistinct'
1 parent 400303a commit 2767bc0

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

Diff for: src/ipc.q

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343

4444
/ Open a connection to the specified host and port
4545
/ @param host (Symbol) The hostname to connect to
46-
/ @param port (Integer) The post to connect to
46+
/ @param port (Short|Integer|Long) The post to connect to
4747
/ @return (Integer) The handle to that process if the connection is successful
4848
/ @see .ipc.connect
4949
.ipc.connectWithHp:{[host;port]
50-
if[(not .type.isSymbol host) | not .type.isInteger port;
50+
if[(not .type.isSymbol host) | not .type.isWholeNumber port;
5151
'"IllegalArgumentException";
5252
];
5353

Diff for: src/type.q

+36-27
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
1-
// Type Checking
2-
// Copyright (c) 2016 Sport Trades Ltd
1+
// Type Checking and Normalisation
2+
// Copyright (c) 2016 - 2020 Sport Trades Ltd
33

44
// Documentation: https://github.com/BuaBook/kdb-common/wiki/type.q
55

6+
/ All infinite values
7+
/ @see .type.isInfinite
68
.type.const.infinites:raze (::;neg)@\:(0Wh;0Wi;0Wj;0We;0Wf;0Wp;0Wm;0Wd;0Wz;0Nn;0Wu;0Wv;0Wt);
79

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;
812

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 }";
1615

17-
.type.isBoolean:{
18-
:-1h~type x;
19-
};
2016

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;
2320
};
2421

25-
.type.isDate:{
26-
:-14h~type x;
27-
};
2822

29-
.type.isTime:{
30-
:-19h~type x;
23+
.type.isString:{
24+
:10h~type x;
3125
};
3226

3327
.type.isNumber:{
3428
:type[x] in -5 -6 -7 -8 -9h;
3529
};
3630

37-
/ NOTE: This function checks for a mathematical integer (i.e. whole number)
38-
.type.isInteger:{
31+
.type.isWholeNumber:{
3932
:type[x] in -5 -6 -7h;
4033
};
4134

@@ -52,7 +45,7 @@
5245
};
5346

5447
.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);
5649
};
5750

5851
.type.isDict:.type.isDictionary:{
@@ -119,10 +112,6 @@
119112
:type[x] within 0 19h;
120113
};
121114

122-
.type.isMixedList:{
123-
:0h~type x;
124-
};
125-
126115
.type.isDistinct:{
127116
:x~distinct x;
128117
};
@@ -159,9 +148,29 @@
159148
'"IllegalArgumentException";
160149
];
161150

162-
if[.type.isInteger x;
151+
if[.type.isLong x;
163152
: `$"::",string x;
164153
];
165154

166155
:x;
167156
};
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+

Diff for: src/util.q

+1-3
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,7 @@ k).util.showNoLimit:{
153153

154154
/ @param x (List) A list to check if all values are unique
155155
/ @returns (Boolean) True if the specified list has only unique values
156-
.util.isDistinct:{
157-
:x~distinct x;
158-
};
156+
.util.isDistinct:.type.isDistinct;
159157

160158
/ String find and replace. If multiple 'find' arguments are supplied the equivalent number of
161159
/ replace arguments must also be specified

0 commit comments

Comments
 (0)