understanding scan operator

234 views
Skip to first unread message

gekaprog

unread,
Dec 20, 2016, 5:37:18 PM12/20/16
to Kdb+ Personal Developers
Hi,

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


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

Davide Corti

unread,
Dec 21, 2016, 2:18:23 AM12/21/16
to personal...@googlegroups.com
Hi,

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

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 | dco...@kx.com

--
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-kdbplus+unsubscribe@googlegroups.com.
To post to this group, send email to personal-kdbplus@googlegroups.com.
Visit this group at https://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.

Davide Corti

unread,
Dec 21, 2016, 2:18:28 AM12/21/16
to Kdb+ Personal Developers
Hi,

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

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

Jonny Press

unread,
Dec 21, 2016, 4:20:44 AM12/21/16
to personal...@googlegroups.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





To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbpl...@googlegroups.com.
To post to this group, send email to personal...@googlegroups.com.

Attila Vrabecz

unread,
Dec 22, 2016, 8:13:12 PM12/22/16
to [kdb+] [kdb+]
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

gekaprog

unread,
Dec 26, 2016, 4:24:09 PM12/26/16
to Kdb+ Personal Developers
David, thank you. I'm wrapping my brain around this.

gekaprog

unread,
Dec 26, 2016, 4:29:42 PM12/26/16
to Kdb+ Personal Developers
This is clever and illustrating. Thank you
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbpl...@googlegroups.com.
To post to this group, send email to personal...@googlegroups.com.
Visit this group at https://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.

--
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 post to this group, send email to personal...@googlegroups.com.
Visit this group at https://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages