Search code examples
kdb

Code optimization for a lambda and projection combination on which each operation is applied for passed args


I'm able to achieve desired result from below code but by using lambda inside lambda:

Code:

    .up.dic:`a`b!(`ab`cd;`ef`gh);
    sd:2019.01.14;
    ed:2019.01.15;
    ({[m;d]{[m;d] 0N!m,d;}[m]@'d}@'raze .up.dic)[;sd+til(ed-sd)+1]

Result:
    (`ab;2019.01.14)
    (`ab;2019.01.15)
    (`cd;2019.01.14)
    (`cd;2019.01.15)
    (`ef;2019.01.14)
    (`ef;2019.01.15)
    (`gh;2019.01.14)
    (`gh;2019.01.15)

Is there a way that we can get rid of inner lambda and pass each dict element with each date

{[m;d] 0N!m,d;}[m]@'d}/Can we get rid of this

Tried lot of things like each, /: before [;sd+til(ed-sd)+1] in outer lambda, but it resulted in length or type error.


Solution

  • You can use cross

    q) raze[.up.dic] cross sd+til(ed-sd)+1
    
    `ab 2019.01.14
    `ab 2019.01.15
    `cd 2019.01.14
    `cd 2019.01.15
    `ef 2019.01.14
    `ef 2019.01.15
    `gh 2019.01.14
    `gh 2019.01.15
    

    Using Adverbs for custom function:

    If you want to do other operations on each pair, you could use a combination of each-left and each-right and define your function to perform the operation on each pair.

    For example, join operation using the custom defined function.

    q) my_func:{x,y} / x will be item from dict and y will be date from date list
    q) raze raze[.up.dic] my_func/:\: sd+til(ed-sd)+1