New to Kdb+/q, question about the tutorial

119 views
Skip to first unread message

Frédéric Delanchy

unread,
Nov 5, 2020, 11:54:24 AM11/5/20
to Kdb+ Personal Developers
Hello all,

I am completely new to Kdb+, and currently learning with the help of the tutorial.

The syntax isn't really easy, and I'm trying to understand as much as I can, but there is one line that I don't understand at all.

in the calls.q script, the column "id" of the "computer" table is defined by :

raze fc#'key startcpu

(with startcpu:(til n)!25+n?20)

I don't understand the syntax #'key , and I can't find anything about it in the documentation.

Is there an implicit column "key" in startcpu ?

Joseph King

unread,
Nov 5, 2020, 12:21:00 PM11/5/20
to Kdb+ Personal Developers
Hi,

'key' isn't a column, it's actually a keyword function that returns the keys of a dictionary(startcpu) as a list
The # is the 'take' function in this instance, meaning it will return a set number of elements from a list
Applying the each-both iterator ' will apply the take to each element in the list of keys 
So, for "raze fc#'key startcpu", going from right to left key startcpu will get a list of key values from startcpu, then fc#' will apply fc# to each value in the key list, creating a matrix.
Finally, raze will collapse that matrix into one long list.
Step-by-step:
q)freq:0D00:01
q)timerange:5D
q)fc:`long$timerange%freq
q)n:1000
q)startcpu:(til n)!25+n?20
q)startcpu
0| 37
1| 33
2| 35
3| 26
4| 34
5| 36
6| 30
..
q)key startcpu
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15..
q)fc
7200
q)fc#'key startcpu
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0..
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2..
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3..
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4..
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5..
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6..
..
q)raze fc#'key startcpu
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0..

Hope this helps!

Best,
Joseph King

George Harding

unread,
Nov 5, 2020, 12:31:40 PM11/5/20
to personal...@googlegroups.com
So firstly looking at the inputs:

To make this shorter lets say n:5; timerange:3D; freq:1D
q)startcpu:(til n)!25+n?20
q)startcpu
0| 29
1| 38
2| 34
3| 27
4| 32

startcpu is a dictionary from integers to integers.

q)fc:`long$timerange%freq
q)fc
3

fc is a long. Looking at the code from right to left we have:

q)key startcpu
0 1 2 3 4

This uses the key function to get the keys of the dictionary startcpu.

q)fc#'key startcpu
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4

The # means take, whilst ' actually means each. So this is the same as:

q){fc#x} each key startcpu
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4

So take 3 0's, 3 1's etc. This result has the  following structure:

".-------."
"|.-----.|"
"||0 0 0||"
"|'J----'|"
"|.-----.|"
"||1 1 1||"
"|'J----'|"
"|.-----.|"
"||2 2 2||"
"|'J----'|"
"|.-----.|"
"||3 3 3||"
"|'J----'|"
"|.-----.|"
"||4 4 4||"
"|'J----'|"
"'#------'"

Finally raze flattens a list of lists giving:

q)raze fc#'key startcpu
0 0 0 1 1 1 2 2 2 3 3 3 4 4 4

Best,

George

--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbpl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/personal-kdbplus/1a90b28c-418a-4789-a1fb-6b0ab9f73eaan%40googlegroups.com.

Frédéric Delanchy

unread,
Nov 5, 2020, 6:48:56 PM11/5/20
to Kdb+ Personal Developers
ok everything is clearer now, thank you both so much for your help.
Reply all
Reply to author
Forward
0 new messages