سؤال

As a minimal example, for instance if I have:

q) x:flip `a`b!(enlist 1;enlist 2);
q) y:flip `c`d!(enlist 3;enlist 4);
q) (raze x), (raze y)
`a`b`c`d!1j 2j 3j 4j   # works as expected

But with peach involved,

q) {(raze x), (raze y)} peach x
enlist 1j 2j   # I was expecting `a`b`c`d!1j 2j 3j 4j

There is no 3j 4j in the output - why has my raze y been ignored?

Indeed, each also gives a different output

q) {(raze x), (raze y)} each x
({:(raze x), (raze y);}';flip `a`b!(enlist 1j;enlist 2j))

I thought peach was just a parallel version of each, so both should yield the same...

What's going on?

هل كانت مفيدة؟

المحلول

That was not an inconsistent behavior of peach and each.

First, Functions in kdb has implicit parameters as x,y,z if not specified any.

     So f:{x+y}  is equivalent to f:{[x;y] x+y}
     But f:{[a;b] a+b} will not have x,y,z as implicit parameter

For more details, see section Implicit Parameters in http://code.kx.com/q4m3/6_Functions/#617-implicit-parameters

Peach Case: When you do {(raze x), (raze y)} peach x :

i) Another way of writing this function is:

    f:{[x;y] (raze x),(raze y)} 
    And call is like:  f[;] peach x

So you are passing global x to local x of a function but nothing in y, that's why you are getting only 1 and 2 and not 3 &4 in output.

ii) Why only 1 and 2 and not ab!1 2 in output? When you pass each row of table (x in your case) to a function, it goes in form of a dictionary. And raze on dictionary gives only values.

You have to modify your function for correct working like this(Use Each Both ):

             flip {x,y}' [x;y]

' is each both which is used when you have more then one arguments and you want to apply each on all of them simultaneoulsy.

This will take one row at a time from both global x and y and copy it to local x and y in dictionary form and then join them.

Each Case:

Each is just giving you message that your function requires 2 arguments and hence it couldn't execute it.

Why Peach worked and Each didn't?

Peach and Each are not same for some scenarios. When you have dyadic function, then peach works like each prior(':) and not as Each. They are same only for monadic functions. In your case you have dyadic function.

نصائح أخرى

x and y can be implicit arguments to a function. Your use of x and y for the name of the list variables is confusing and not recommended.

To make it clear what is happening consider if I renamed the variables a,b:

q)a:flip `a`b!(enlist 1;enlist 2);
q)b:flip `c`d!(enlist 3;enlist 4);
q)(raze a), (raze b)
a| 1
b| 2
c| 3
d| 4
q){(raze a), (raze b)} peach x
a b c d
-------
1 2 3 4

You can see how peach/each handle the implicit function arguments with this example:

q)x:x q)y:y

q){(x;y)} each 1 {(x;y)}'[1] q){(x;y)} peach 1 1

I can't tell for sure what behaviour you want so all I can do is point out why there's an issue.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top