What's the equivalent to the Common Lisp function (map ...) in kdb?

StackOverflow https://stackoverflow.com/questions/19664350

  •  01-07-2022
  •  | 
  •  

سؤال

I have a multivalent kdb function that I want to apply successively by taking a single atom out of each of its arguments (which are lists of the same length taken from a table) and applying the arguments to said function.

I feel like this should be easy and I'm missing something fundamental, but as is usual with kdb, it's hard to search for a solution.

The snippet below does what I want but only works on the top-level (as far as I can figure out):

a:(1 2 3);
b:(1 2 3);
{a[x]*b[x]} each til count a
هل كانت مفيدة؟

المحلول

Not sure I understood your question completely, but sounds like you want to apply a function element by element? Obviously arithmetic functions already do this in KDB+, but for the sake of illustration:

q)a
8 1 9 5 4 6 6 1 8 5
q)b
4 9 2 7 0 1 9 2 1 8
q)f:{[x;y] x*y}
q)a,'b
8 4
1 9
9 2
5 7
4 0
6 1
6 9
1 2
8 1
5 8
q)f .' a,'b
32 9 18 35 0 6 54 2 8 40

The appropriate references are:

http://code.kx.com/q/ref/errors/#signal (each both)

http://code.kx.com/q/ref/unclassified/#apply (apply)

نصائح أخرى

the idiomatic way to do that is is x f'y:

q)a:1 2 3
q)b:1 2 3
q)a*'b
1 4 9
q)

(this is of course redundant for arithmetic, but it's useful for other things)

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