Search code examples
kdb

How to convert "like each" into a functional form?


Let's say I have a column of a table whose data type is a character array. I want to pass in a functional select where clause, where the column is in a list of given strings. However, I cannot simply use (in; `col; myList) for reasons. Instead, I need to do the equivalent of:

max col like/: myList

which effectively gives the same result. However, I have tried to put this in functional form

(max; (like/:; `col; myList))

And I am getting a type error. Any ideas on how I could make this work?


Solution

  • A nice trick when dealing with this problem is using parse on a string of the select statement you want to functionalize. For example:

    q)parse"select from t where max col like/: myList"
    ?
    `t
    ,,(max;((/:;like);`col;`myList))
    0b
    ()
    

    Or specifically in your case you want the 3rd element of the result list (the where clause):

    q)(parse"select from t where max col like/: myList")2
    max ((/:;like);`col;`myList)
    

    I even think using this pattern in your actual code can be a good idea, as functionalized statements like max ((/:;like);`col;`myList) can get pretty unreadable pretty quickly!

    Hope that helps!