KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Five easy pieces #2: getting around Re: Five easy pieces #2: getting around

  • SJT

    Member
    October 3, 2022 at 12:00 am

    Does the job! Note the use of a dictionary where another language would need a control structure.

    The each can be dispensed with. Both up and dn take vector arguments. .Q.f does not, so d[2] could be .Q.f'[nd;]:

     

    q)rnd:{[x;nd;m] d:`up`dn`nr!(up[;nd];dn[;nd];.Q.f'[nd;]); (d m) x} 
    q)rnd[q+0 1 2;3;`up] 
    "9.639" "10.639" "11.639" 
    q)rnd[q+0 1 2;3;`nr] 
    "9.639" "10.639" "11.639"

     

    Now: can we eliminate repetition? The up and dn functions differ by only a single keyword. And without delegating one of the modes to .Q.f?

    (Hint: a single expression rnd is possible, ?80 characters.)