Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KDB+/Q: About unused parameters in inner functions

Tags:

k

kdb

I have this function f

f:{{z+x*y}[x]/[y]}

I am able to call f without a 3rd parameter and I get that, but how is the inner {z+x*y} able to complete without a third parameter?

like image 575
zrb Avatar asked Dec 25 '22 03:12

zrb


2 Answers

kdb will assume, if given a single list to a function which takes two parameters, that you want the first one to be x and the remainder to be y (within the context of over and scan, not in general). For example:

q){x+y}/[1;2 3 4]
10

can also be achieved by:

q){x+y}/[1 2 3 4]
10

This is likely what's happening in your example.

EDIT:

In particular, you would use this function like

q){{z+x*y}[x]/[y]}[2;3 4 5 6]
56

which is equivalent to (due to the projection of x):

q){y+2*x}/[3 4 5 6]
56

which is equivalent to (due to my original point above):

q){y+2*x}/[3;4 5 6]
56

Which explains why the "third" parameter wasn't needed

like image 109
terrylynch Avatar answered Dec 28 '22 15:12

terrylynch


You need to understand 2 things: 'over' behavior with dyadic functions and projection.

1. Understand how over/scan works on dyadic function: http://code.kx.com/q/ref/adverbs/#over

If you have a list like (x1,x2,x3) and funtion 'f' then

f/(x1,x2,x3) ~  f[ f[x1;x2];x3]

So in every iteration it takes one element from list which will be 'y' and result from last iteration will be 'x'. Except in first iteration where first element will be 'x' and second 'y'.

Ex:

  q)   f:{x*y}   / call with -> f/ (4 5 6)

first iteration : x=4, y=5, result=20

second iteration: x=20, y=6, result=120

2. Projection:

Lets take an example funtion f3 which takes 3 parameters:

   q) f3:{[a;b;c] a+b+c}

now we can project it to f2 by fixing (passing) one parameter

  q)  f2:f3[4]   / which means=> f2={[b;c] 4+b+c}

so f2 is dyadic now- it accepts only 2 parameters.

So now coming to your example and applying above 2 concepts, inner function will eventually become dyadic because of projection and then finally 'over' function works on this new dyadic function. We can rewrite the function as :

f:{
f3:{z+x*y};
f2:f3[x];
f2/y
}
like image 28
Rahul Avatar answered Dec 28 '22 17:12

Rahul