curryingkdb+q-lang

[KDB+/Q]: Apply list of functions over data sequentially (pipe)


In kdb+/q, how to pipe data through a sequential list of functions so that output of previous step is the input to next step?

For example:

q)t:([]sym:`a`c`b;val:1 3 2)
q)`sym xkey `sym xasc t                / how to achieve the same result as this?

I presume some variation of over or / could work:

   ?? over (xasc;xkey)

Bonus: how to achieve the same in a way where t is piped in from the right-hand side (in the spirit of left-of-right reading of the q syntax)?

(xasc;xkey) ?? t

Solution

  • Use a lambda on the left as well as the over adverb (form of recursion) Also the dot (.) form of apply is used to apply the function to the table and the column:

      {.[y;(z;x)]}/[t;(xasc;xkey);`sym]
      sym| val
      ---| --- 
      a  | 1
      b  | 2
      c  | 3