Q basics



Here is a cheat sheet to help
Here is the official Reference doc

Recall in Q we work right to left !
I am skipping over some basics as this is just a Quick refresher - see KX tutorial for full details

Some Basic Operations




q)2{2*x+y}/1 2 //comment
16



Implies (1+2)=3 * 2 = 6
Second pass as 2{…} says do it twice 6+2=8 *2 = 16



//dates from 2000.01.01 represented a as 0
2000.01.01=0 //1b ie true

0 +/ 10 20 30 40 50 //take list apply + across list or fold|reduce list using + start at 0
(+/) 10 20 30 40 50 // add across list use first value as initial value
0 +\ 10 20 30 40 50 //take list apply + across list or fold list using + start at 0 - show steps
sum 10 20 30 40 50
sums 10 20 30 40 50 //same as sum but using scan \

1 2 3 +/10 20 30 40 50 // fold 150 onto 1 then 2 then 3

(-/) 10 20 //list processed left to right

(*/) 1+til 5 //Factorial 5! need the 1 due to multiply 0

4|5 //returns larger of operands max(x,y)
4&5 //returns lesser of operands min(x,y)
(|/) 10 20 30 40 //max of list
max 10 20 30 40
maxs 10 20 30 40 //same as max but using |\
(|\) 10 20 30 40

(&/) 10 20 30 40 //min of list
min 10 20 30 40
mins 10 20 30 40 //same as min but using scan \

//same applies to boleans
1b|0b // T or 1b
1b&0b // F or 0b



Basic assignment


q)a:4
q)a
4
q)



Lists




2#10 20 30 40 50 // get 2 from front -2 gets 2 from back

10 20, 100 200 //concat 2 lists
{-2#x} 1 1 //lst 2 args of list 1 1
{sum -2#x} 1 1 // sum last 2 args
{x,sum -2#x} 1 1 // and add sum to list
{x, sum -2#x}\[10;1 1 ] // iterate 10 times starting with 1 1


Note the last one is take the last 2 of the list starting at 1 1 and sum it then iterate 10 times this is the same as the first 10 Fibonacci numbers

Dictionaries


Dictionary is just a key value map


q)show d:`a`b`c!1 2 3
a| 1
b| 2
c| 3
q)key d
`a`b`c
q)value d
1 2 3




Find item `abc in dictionary d and change value of symbol `c in dictionary d


q)d `a`c
1 3
q)d[`c]:100
q)d
a| 1
b| 2
c| 100
q)




Enumeration



q)
q)u:`red`green`blue // The enumerations 0 1 2
q)v:10000?u //10k random enumerated items
q)v
`red`red`blue`green`blue`green`blue`blue`green`red`green`blue`blue`green`red`..
q)//10k list of Red green blue now make it enum
q)k:u?v //represents the storage k is finding u in v each item
q)k
0 0 2 1 2 1 2 2 1 0 1 2 2 1 0 1 2 0 1 1 2 0 1 2 1 2 1 0 0 1 2 1 2 0 0 2 0 1 0..
q)ev:`u$v // ev now represents the enumed list and can be indexed
q)ev
`u$`red`red`blue`green`blue`green`blue`blue`green`red`green`blue`blue`green`r..
q)ev[3]
`u$`green
q)ev[4]
`u$`blue
q)




Functions


x y z implicit if not named - not its easier in functions to use dictionaries as you can have only 7 or so variables


//Functions
{[x] x*x }
{[x] x*x } 5 // apply 5 to {} substitute param [x]
{[x] x*x } [5] //also valid [] optional
//x y z implicit so
{ x*x } 5

Simple func applied immediately

q){[a;b] c:a*b; c }[2 2]
{[a;b] c:a*b; c }[2 2]


Function assigned to f to be called later


q)f:{[a;b] c:a*b; c }
q)f[2;2]
4





Tables


Create a simple table with 10 rows and and show first 5


q)sym:`a`b`c 10?3 //symbol column
q)dates:2018.01.01+10?31 //dates column random days 1 to 31
q)times:10?24:00:00.000 //times random times
q)px:10? 100 200 30 // 10 random items across list
q)t:([] date:dates; time:times; syms:sym; pxs:px) //create table t
q)t
date time syms pxs
--------------------------------
2018.01.19 20:05:21.547 a 100
2018.01.26 08:27:05.205 a 100
2018.01.13 21:04:55.764 b 200
2018.01.16 02:25:41.362 b 300
2018.01.17 19:49:18.642 a 200
2018.01.29 18:04:04.102 a 100
2018.01.19 13:17:48.416 a 300
2018.01.03 21:08:22.898 b 200
2018.01.03 21:18:18.813 c 100
2018.01.02 16:31:57.606 a 200
q)5#t
date time syms pxs
--------------------------------
2018.01.19 20:05:21.547 a 100
2018.01.26 08:27:05.205 a 100
2018.01.13 21:04:55.764 b 200
2018.01.16 02:25:41.362 b 300
2018.01.17 19:49:18.642 a 200
q)
q)t:`date`time xasc t //sort t by date and time
q)t
date time syms pxs
--------------------------------
2018.01.02 16:31:57.606 a 200
2018.01.03 21:08:22.898 b 200
2018.01.03 21:18:18.813 c 100
2018.01.13 21:04:55.764 b 200
2018.01.16 02:25:41.362 b 300
2018.01.17 19:49:18.642 a 200
2018.01.19 13:17:48.416 a 300
2018.01.19 20:05:21.547 a 100
2018.01.26 08:27:05.205 a 100
2018.01.29 18:04:04.102 a 100
q)

q)meta t //get meta off the tables for designations see kx reference
c | t f a
----| -----
date| d s
time| t
syms| s
pxs | j
q)




Keyed Tables

keyed tables are a dictionary - with the Key in [] and the values using the t defined above we have a key on date and times - because its dictionary key must be unique !


q)keyedt:([date:dates; time:times] syms:sym; pxs:px)
q)keyedt
date time | syms pxs
-----------------------| --------
2018.01.19 20:05:21.547| a 100
2018.01.26 08:27:05.205| a 100
2018.01.13 21:04:55.764| b 200
2018.01.16 02:25:41.362| b 300
2018.01.17 19:49:18.642| a 200
2018.01.29 18:04:04.102| a 100
2018.01.19 13:17:48.416| a 300
2018.01.03 21:08:22.898| b 200
2018.01.03 21:18:18.813| c 100
2018.01.02 16:31:57.606| a 200




Import/Export Files

writing and reading from disk



q) `:/Users/arifjaffer/q/test.dat set 10 20 30
`:/Users/arifjaffer/q/test.dat
q)val::get `:/Users/arifjaffer/q/test.dat
q)val
10 20 30
q)






People who enjoyed this article also enjoyed the following:


Thread Safe Java Singleton
Create a Statistics Distribution
Naive Bayes classification AI algorithm
K-Means Clustering AI algorithm
Equity Derivatives tutorial
Fixed Income tutorial


And the following Trails:

C++
Java
python
Scala
Investment Banking tutorials

HOME

homeicon

By clicking Dismiss you accept that you may get a cookie that is used to improve your user experience and for analytics.
All data is anonymised. Our privacy page is here =>
Privacy Policy
This message is required under GDPR (General Data Protection Rules ) and the ICO (Information Commissioners Office).