Q 语言 - 函数
功能类型
函数可以按多种方式分类。在这里,我们根据它们采用的参数的数量和类型以及结果类型对它们进行了分类。功能可以是,
Atomics- 参数是Atomics的并产生Atomics结果
聚合- 列表中的Atomics
Uniform (list from list) - 扩展了Atomics的概念,因为它们适用于列表。参数列表的计数等于结果列表的计数。
其他- 如果函数不属于上述类别。
数学中的二元运算称为q 中的二元函数;例如,“+”。类似地,一元运算称为一元函数;例如,“abs”或“floor”。
常用功能
q编程中有很多常用的函数。在本节中,我们将看到一些流行函数的用法 -
腹肌
q) abs -9.9 / Absolute value, Negates -ve number & leaves non -ve number 9.9
全部
q) all 4 5 0 -4 / Logical AND (numeric min), returns the minimum value 0b
最大值 (&)、最小值 (|) 和非 (!)
q) /And, Or, and Logical Negation q) 1b & 1b / And (Max) 1b q) 1b|0b / Or (Min) 1b q) not 1b /Logical Negate (Not) 0b
升序
q)asc 1 3 5 7 -2 0 4 / Order list ascending, sorted list / in ascending order i s returned `s#-2 0 1 3 4 5 7 q)/attr - gives the attributes of data, which describe how it's sorted. `s denotes fully sorted, `u denotes unique and `p and `g are used to refer to lists with repetition, with `p standing for parted and `g for grouped
平均
q)avg 3 4 5 6 7 / Return average of a list of numeric values 5f q)/Create on trade table q)trade:([]time:3?(.z.Z-200);sym:3?(`ibm`msft`apple);price:3?99.0;size:3?100)
经过
q)/ by - Groups rows in a table at given sym q)select sum price by sym from trade / find total price for each sym sym | price ------ | -------- apple | 140.2165 ibm | 16.11385
列
q)cols trade / Lists columns of a table `time`sym`price`size
数数
q)count (til 9) / Count list, count the elements in a list and / return a single int value 9
港口
q)\p 9999 / assign port number q)/csv - This command allows queries in a browser to be exported to excel by prefixing the query, such as http://localhost:9999/.csv?select from trade where sym =`ibm
切
q)/ cut - Allows a table or list to be cut at a certain point q)(1 3 5) cut "abcdefghijkl" / the argument is split at 1st, 3rd and 5th letter. "bc" "de" "fghijkl" q)5 cut "abcdefghijkl" / cut the right arg. Into 5 letters part / until its end. "abcde" "fghij" "kl"
删除
q)/delete - Delete rows/columns from a table q)delete price from trade time sym size --------------------------------------- 2009.06.18T06:04:42.919 apple 36 2009.11.14T12:42:34.653 ibm 12 2009.12.27T17:02:11.518 apple 97
清楚的
q)/distinct - Returns the distinct element of a list q)distinct 1 2 3 2 3 4 5 2 1 3 / generate unique set of number 1 2 3 4 5
入伍
q)/enlist - Creates one-item list. q)enlist 37 ,37 q)type 37 / -ve type value -7h q)type enlist 37 / +ve type value 7h
填写 (^)
q)/fill - used with nulls. There are three functions for processing null values. The dyadic function named fill replaces null values in the right argument with the atomic left argument. q)100 ^ 3 4 0N 0N -5 3 4 100 100 -5 q)`Hello^`jack`herry``john` `jack`herry`Hello`john`Hello
填充
q)/fills - fills in nulls with the previous not null value. q)fills 1 0N 2 0N 0N 2 3 0N -5 0N 1 1 2 2 2 2 3 3 -5 -5
第一的
q)/first - returns the first atom of a list q)first 1 3 34 5 3 1
翻动
q)/flip - Monadic primitive that applies to lists and associations. It interchange the top two levels of its argument. q)trade time sym price size ------------------------------------------------------ 2009.06.18T06:04:42.919 apple 72.05742 36 2009.11.14T12:42:34.653 ibm 16.11385 12 2009.12.27T17:02:11.518 apple 68.15909 97 q)flip trade time | 2009.06.18T06:04:42.919 2009.11.14T12:42:34.653 2009.12.27T17:02:11.518 sym | apple ibm apple price | 72.05742 16.11385 68.15909 size | 36 12 97
机构间常设委员会
q)/iasc - Index ascending, return the indices of the ascended sorted list relative to the input list. q)iasc 5 4 0 3 4 9 2 3 1 4 0 5
伊德斯克
q)/idesc - Index desceding, return the descended sorted list relative to the input list q)idesc 0 1 3 4 3 2 1 0
在
q)/in - In a list, dyadic function used to query list (on the right-handside) about their contents. q)(2 4) in 1 2 3 10b
插入
q)/insert - Insert statement, upload new data into a table. q)insert[`trade;((.z.Z);`samsung;48.35;99)],3 q)trade time sym price size ------------------------------------------------------ 2009.06.18T06:04:42.919 apple 72.05742 36 2009.11.14T12:42:34.653 ibm 16.11385 12 2009.12.27T17:02:11.518 apple 68.15909 97 2015.04.06T10:03:36.738 samsung 48.35 99
钥匙
q)/key - three different functions i.e. generate +ve integer number, gives content of a directory or key of a table/dictionary. q)key 9 0 1 2 3 4 5 6 7 8 q)key `:c: `$RECYCLE.BIN`Config.Msi`Documents and Settings`Drivers`Geojit`hiberfil.sys`I..
降低
q)/lower - Convert to lower case and floor q)lower ("JoHn";`HERRY`SYM) "john" `herry`sym
最大值和最小值(即 | 和 &)
q)/Max and Min / a|b and a&b q)9|7 9 q)9&5 5
无效的
q)/null - return 1b if the atom is a null else 0b from the argument list q)null 1 3 3 0N 0001b
桃
q)/peach - Parallel each, allows process across slaves q)foo peach list1 / function foo applied across the slaves named in list1 'list1 q)foo:{x+27} q)list1:(0 1 2 3 4) q)foo peach list1 / function foo applied across the slaves named in list1 27 28 29 30 31
上一篇
q)/prev - returns the previous element i.e. pushes list forwards q)prev 0 1 3 4 5 7 0N 0 1 3 4 5
随机的( ?)
q)/random - syntax - n?list, gives random sequences of ints and floats q)9?5 0 0 4 0 3 2 2 0 1 q)3?9.9 0.2426823 1.674133 3.901671
夷
q)/raze - Flattn a list of lists, removes a layer of indexing from a list of lists. for instance: q)raze (( 12 3 4; 30 0);("hello";7 8); 1 3 4) 12 3 4 30 0 "hello" 7 8 1 3 4
读0
q)/read0 - Read in a text file q)read0 `:c:/q/README.txt / gives the contents of *.txt file
读1
q)/read1 - Read in a q data file q)read1 `:c:/q/t1 0xff016200630b000500000073796d0074696d6500707269636…
撤销
q)/reverse - Reverse a list q)reverse 2 30 29 1 3 4 4 3 1 29 30 2 q)reverse "HelloWorld" "dlroWolleH"
放
q)/set - set value of a variable q)`x set 9 `x q)x 9 q)`:c:/q/test12 set trade `:c:/q/test12 q)get `:c:/q/test12 time sym price size --------------------------------------------------------- 2009.06.18T06:04:42.919 apple 72.05742 36 2009.11.14T12:42:34.653 ibm 16.11385 12 2009.12.27T17:02:11.518 apple 68.15909 97 2015.04.06T10:03:36.738 samsung 48.35 99 2015.04.06T10:03:47.540 samsung 48.35 99 2015.04.06T10:04:44.844 samsung 48.35 99
超导系统
q)/ssr - String search and replace, syntax - ssr["string";searchstring;replaced-with] q)ssr["HelloWorld";"o";"O"] "HellOWOrld"
细绳
q)/string - converts to string, converts all types to a string format. q)string (1 2 3; `abc;"XYZ";0b) (,"1";,"2";,"3") "abc" (,"X";,"Y";,"Z") ,"0"
SV
q)/sv - Scalar from vector, performs different tasks dependent on its arguments. It evaluates the base representation of numbers, which allows us to calculate the number of seconds in a month or convert a length from feet and inches to centimeters. q)24 60 60 sv 11 30 49 41449 / number of seconds elapsed in a day at 11:30:49
系统
q)/system - allows a system command to be sent, q)system "dir *.py" " Volume in drive C is New Volume" " Volume Serial Number is 8CD2-05B2" "" " Directory of C:\\Users\\myaccount-raj" "" "09/14/2014 06:32 PM 22 hello1.py" " 1 File(s) 22 bytes"
桌子
q)/tables - list all tables q)tables ` `s#`tab1`tab2`trade
蒂尔
q)/til - Enumerate q)til 5 0 1 2 3 4
修剪
q)/trim - Eliminate string spaces q)trim " John " "John"
与
q)/vs - Vector from scaler , produces a vector quantity from a scaler quantity q)"|" vs "20150204|msft|20.45" "20150204" "msft" "20.45"
XASC
q)/xasc - Order table ascending, allows a table (right-hand argument) to be sorted such that (left-hand argument) is in ascending order q)`price xasc trade time sym price size ---------------------------------------------------------- 2009.11.14T12:42:34.653 ibm 16.11385 12 2015.04.06T10:03:36.738 samsung 48.35 99 2015.04.06T10:03:47.540 samsung 48.35 99 2015.04.06T10:04:44.844 samsung 48.35 99 2009.12.27T17:02:11.518 apple 68.15909 97 2009.06.18T06:04:42.919 apple 72.05742 36
xcol
q)/xcol - Renames columns of a table q)`timeNew`symNew xcol trade timeNew symNew price size ------------------------------------------------------------- 2009.06.18T06:04:42.919 apple 72.05742 36 2009.11.14T12:42:34.653 ibm 16.11385 12 2009.12.27T17:02:11.518 apple 68.15909 97 2015.04.06T10:03:36.738 samsung 48.35 99 2015.04.06T10:03:47.540 samsung 48.35 99 2015.04.06T10:04:44.844 samsung 48.35 99
xcols
q)/xcols - Reorders the columns of a table, q)`size`price xcols trade size price time sym ----------------------------------------------------------- 36 72.05742 2009.06.18T06:04:42.919 apple 12 16.11385 2009.11.14T12:42:34.653 ibm 97 68.15909 2009.12.27T17:02:11.518 apple 99 48.35 2015.04.06T10:03:36.738 samsung 99 48.35 2015.04.06T10:03:47.540 samsung 99 48.35 2015.04.06T10:04:44.844 samsung
描述
q)/xdesc - Order table descending, allows tables to be sorted such that the left-hand argument is in descending order. q)`price xdesc trade time sym price size ----------------------------------------------------------- 2009.06.18T06:04:42.919 apple 72.05742 36 2009.12.27T17:02:11.518 apple 68.15909 97 2015.04.06T10:03:36.738 samsung 48.35 99 2015.04.06T10:03:47.540 samsung 48.35 99 2015.04.06T10:04:44.844 samsung 48.35 99 2009.11.14T12:42:34.653 ibm 16.11385 12
x组
q)/xgroup - Creates nested table q)`x xgroup ([]x:9 18 9 18 27 9 9;y:10 20 10 20 30 40) 'length q)`x xgroup ([]x:9 18 9 18 27 9 9;y:10 20 10 20 30 40 10) x | y ---- | ----------- 9 | 10 10 40 10 18 | 20 20 27 | ,30
x键
q)/xkey - Set key on table q)`sym xkey trade sym | time price size --------- | ----------------------------------------------- apple | 2009.06.18T06:04:42.919 72.05742 36 ibm | 2009.11.14T12:42:34.653 16.11385 12 apple | 2009.12.27T17:02:11.518 68.15909 97 samsung | 2015.04.06T10:03:36.738 48.35 99 samsung | 2015.04.06T10:03:47.540 48.35 99 samsung | 2015.04.06T10:04:44.844 48.35 99
系统命令
系统命令控制q环境。它们的形式如下 -
\cmd [p] where p may be optional
下面讨论了一些流行的系统命令 -
\a [命名空间] – 列出给定命名空间中的表
q)/Tables in default namespace q)\a ,`trade q)\a .o / table in .o namespace. ,`TI
\b – 查看依赖关系
q)/ views/dependencies q)a:: x+y / global assingment q)b:: x+1 q)\b `s#`a`b
\B – 待处理的视图/依赖项
q)/ Pending views/dependencies q)a::x+1 / a depends on x q)\B / the dependency is pending ' / the dependency is pending q)\B `s#`a`b q)\b `s#`a`b q)b 29 q)a 29 q)\B `symbol$()
\cd – 更改目录
q)/change directory, \cd [name] q)\cd "C:\\Users\\myaccount-raj" q)\cd ../new-account q)\cd "C:\\Users\\new-account"
\d – 设置当前命名空间
q)/ sets current namespace \d [namespace] q)\d /default namespace ' q)\d .o /change to .o q.o)\d `.o q.o)\d . / return to default q)key ` /lists namespaces other than .z `q`Q`h`j`o q)\d .john /change to non-existent namespace q.john)\d `.john q.john)\d . q)\d `.
\l – 从数据库加载文件或目录
q)/ Load file or directory, \l q)\l test2.q / loading test2.q which is stored in current path. ric | date ex openP closeP MCap ----------- | ------------------------------------------------- JPMORGAN | 2008.05.23 SENSEX 18.30185 17.16319 17876 HSBC | 2002.05.21 NIFTY 2.696749 16.58846 26559 JPMORGAN | 2006.09.07 NIFTY 14.15219 20.05624 14557 HSBC | 2010.10.11 SENSEX 7.394497 25.45859 29366 JPMORGAN | 2007.10.02 SENSEX 1.558085 25.61478 20390 ric | date ex openP closeP MCap ---------- | ------------------------------------------------ INFOSYS | 2003.10.30 DOW 21.2342 7.565652 2375 RELIANCE | 2004.08.12 DOW 12.34132 17.68381 4201 SBIN | 2008.02.14 DOW 1.830857 9.006485 15465 INFOSYS | 2009.06.11 HENSENG 19.47664 12.05208 11143 SBIN | 2010.07.05 DOW 18.55637 10.54082 15873
\p – 端口号
q)/ assign port number, \p q)\p 5001i q)\p 8888 q)\p 8888i
\\ - 从 q 控制台退出
\\ - exit Exit form q.