2022.10.12 03:35 AM - edited 2022.10.12 03:40 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 ?
2022.10.12 03:53 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.
2022.10.12 04:28 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.
2022.10.12 08:09 AM - edited 2022.10.12 08:24 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
2022.10.12 09:00 AM - edited 2022.10.12 09:03 AM
Huh – now I see what you’re up to: implementing rotate
in q.
This reminds me of a category of puzzle that used to be popular in q’s 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éter advises, in q written as count
.
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.