KX Community

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

Home Forums kdb+ rotate function differences between k and q

  • rotate function differences between k and q

    Posted by newstudent2017 on October 12, 2022 at 12:00 am

    Following is the implementation of rotate function

    Orig:

    k){$[0h>@y;'`rank;98h<@y;'`type;#y;,/|(0;mod[x;#y])_y;y]}

    Working by making slight changes in q

    {$[0h>(@)y;'`rank;$[ 98h<(@)y;'`type; [ (#)y;(,/)(|)(0;mod[x;(#)y])_y;y ]  ] ] }

     

    How is the strikethrough stuff working in k ?

    newstudent2017 replied 8 months, 2 weeks ago 3 Members · 4 Replies
  • 4 Replies
  • gyorokpeter-kx

    Member
    October 12, 2022 at 12:00 am

    Your “q version” is actually a hybrid between k and q. A more q-native interpretation would look like:

    rotate2:{$[0h>type y;'`rank;98h<type y;'`type;count y;raze reverse(0;x mod count y)_y;y]}

    The whole function is a conditional with multiple branches. The part you crossed out is just a check if the list is empty, in which case we don’t bother with calculating the rotated version, we just return the original empty list. You seem to have broken up the conditional in your “q” version even though it is still possible to use the multi-branch version.

  • Laura

    Administrator
    October 12, 2022 at 12:00 am

    Watch out The q language is implemented as a DSL embedded in the k4 programming language. The latter is exposed infrastructure. It is neither supported nor documented, and its use is deprecated and in production code, strongly deprecated.

  • newstudent2017

    Member
    October 12, 2022 at 12:00 am

    Thanks @SJT and @gyorokpeter-kx

     

    l / 2 3 5 7 11j

    2 rotate l / 5 7 11 2 3j

    {$[0h>(@)y;’`rank; 98h<(@)y;’`type; (#)y ; (,/)(|)(0;mod[x;(#)y])_y ; y ] } [2;l] /5 7 11 2 3j

    I guess the above works

  • Laura

    Administrator
    October 12, 2022 at 12:00 am

    Huh  now I see what youre up to: implementing rotate in q.

    This reminds me of a category of puzzle that used to be popular in qs ancestor language APL, called dead key questions. The premise was always that a certain key on your keyboard was not working; how do you code around it?

    APL primitives are mostly variadic: they can be applied as either unaries or binaries, usually with related semantics. And they are (almost) all single characters, which is what gives the dead key problem its bite. For example, rotate and reverse in q are in APL the binary and unary forms of ?

     

    l?2 3 5 7 11 
    ?l 11 7 5 3 2 
    2?l 5 7 11 2 3

     

    In k, operators are similarly variadic; q replaces the unary forms with keywords. Like k itself, the unary forms of the operators are considered exposed infrastructure: use the q keywords instead. The (#) you asked about is, as P鴥r advises, in q written as count.

Log in to reply.