understanding scan operator

Hi,

I am getting stuck understanding the example. Could somebody explain how it works, #Q1, Q2?

https://eternallearningq.wordpress.com/2012/05/10/msum-using-sliding-window-in-kdb/

a. {1 _ x, y} - subfunction that appends two lists, removing first element of x.

b. x#0 - creates a list of 0s, x elements.

c. foo[function arguments] - function call of two arguments (dyadic). 

d. f\ applied to a list x1, x2, .. xn returns x1, f(x1,x2), f(f(x1,x2),x3) - from the documentation

Q1. How did we get first line 0 0 1 which is really 1_(0, 0) , 1. I do not what function picked y[0], y[1]. 

Is the \ scan operator overloaded to step through y? 

Q2. If I understand correctly, scan executes recursively. Is it all implemented inside ? 

call will terminate once x#0 runs out?

sw: {{1 _ x, y}[x#0;y]}

y: 1 6 21 56

sw[3;y]

0 0  1

0 1  6

1 6  21

6 21 56

Thanks

Hi,


I think you can find a good explanation of scan in the following white paper:

http://code.kx.com/db/DB_Efficient_Use_of_Adverbs.pdf


To answer your questions:

Q1. Given f:{1_x, y}; x:0 0 0; y:1 6 21 56, f[x;y] works in the following way:

- Iteration1: res1=f[x;y[0]]

- Iteration2: res2=f[res1;y[1]]

- Iteration3: res3=f[res2;y[2]]

- …


Q2. Used in this way scan terminates once it has processed y entirely. If you use it for instance in its monadic version, it terminates when two consecutive iterations produce the same result.


HTH,

Davide Corti | KDB+ Engineer | Kx | +44-7926-223284 | dcorti@kx.com

Hi,


I think you can find a good explanation of scan in the following white paper:

http://code.kx.com/db/DB_Efficient_Use_of_Adverbs.pdf


To answer your questions:

Q1. Given f:{1_x, y}; x:0 0 0; y:1 6 21 56, f[x;y] works in the following way:

- Iteration1: res1=f[x;y[0]]

- Iteration2: res2=f[res1;y[1]]

- Iteration3: res3=f[res2;y[2]]

- …


Q2. Used in this way scan terminates once it has processed y entirely. If you use it for instance in its monadic version, it terminates when two consecutive iterations produce the same result.


HTH,

Davide Corti | KDB+ Engineer | Kx | +44-7926-223284 | dcorti@kx.com

I find it useful to put print statements in scan/over to understand how it is operating e.g. 

q)sw:{{-1"****";-1"x is ",-3!x; -1"y is ",-3!y;r:1 _ x, y; -1"result is ",-3!r; r}[x#0;y]} 

q)sw[3;1 6 21 56]                                                                           

****

x is 0 0 0

y is 1

result is 0 0 1

****

x is 0 0 1

y is 6

result is 0 1 6

****

x is 0 1 6

y is 21

result is 1 6 21

****

x is 1 6 21

y is 56

result is 6 21 56

0 0  1 

0 1  6 

1 6  21

6 21 56

Thanks 

Jonny

another simple thing I find useful for understanding scan/over is just simple list-creation

in this specific example:

q)(;)[3#0;1 6 21 56]

0 0 0              1

(0 0 0;1)          6

((0 0 0;1);6)      21

(((0 0 0;1);6);21) 56

this makes is obvious how the inputs are consumed and carried over

David, thank you. I’m wrapping my brain around this.

This is clever and illustrating. Thank you