New to Kdb+/q, question about the tutorial

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.

Here : https://code.kx.com/q/learn/brief-introduction/#random-data-generation

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 ?

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

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.

https://code.kx.com/q/ref/key/

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

ok everything is clearer now, thank you both so much for your help.

Le jeudi 5 novembre 2020 à 18:31:40 UTC+1, george.win...@gmail.com a écrit :

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.

https://code.kx.com/q/ref/key/

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