Reputation: 19486
I'm trying to understand this:
100+\ 1 2 3
101 103 106
Which works fine.
Question 1:
When I wrap this in brackets, I get an error I wasn't expecting:
(100+\) 1 2 3
'Cannot write to handle 100. OS reports: Bad file descriptor
What am I doing wrong here? It doesn't look like I'm writing a file to me.
Question 2:
Given the +[1;2] = 3
, I believe this:
+[100;]\ 1 2 3
'
[0] +[100;]\ 1 2 3
(or perhaps +[;100]\ 1 2 3
) should also work with projection, but it doesn't. What am I doing wrong here?
Upvotes: 3
Views: 302
Reputation: 1097
When an iterator (here \
) is applied postfix (as is usual) to a function (here +
) it derives a function (here +\
) that is both variadic (as per @mturkington) and has infix syntax. You can apply it as a unary or as a binary. Your example 100+\1 2 3
applies it as a binary.
The parser needs a clue if you want to apply +\
as a unary. You can apply any function using bracket notation. Or you can parenthesise it: (+\)
has noun syntax, as does the list (+;-;*;%)
. You can apply or index a noun with prefix syntax.
q)100+\1 2 3 / binary application, infix syntax
101 103 106
q)+\[100;1 2 3] / binary application, bracket syntax
101 103 106
q)+\[1 2 3] / unary application, bracket syntax
1 3 6
q)(+\)1 2 3 / unary application, prefix syntax
1 3 6
You don’t say what result you expect from using the projection. I’ll assume you’re exploring a different way of getting the same result as in Q1.
The key issue here is that the projection of binary Add on 100 is a unary +[;100]
(or +[100]
or just 100+
), and the accumulators \
and /
applied to a unary are the Converge, Do and While iterators.
None of these gives you the Q1 result. For unary f
, the derived function f\
just keeps applying f
successively.
q)5 +[100]\ 1 2 3 / do 100+ five times
1 2 3
101 102 103
201 202 203
301 302 303
401 402 403
501 502 503
Upvotes: 1
Reputation: 119
In this case, +\
is the underlying code for the sums
keyword. This is one of several keywords that are known as variadic because their rank is not fixed. When you try (100+\) 1 2 3
, kdb is actually applying the equivalent of sums
to your input list, then trying to write that to handle 100, which of course doesn't exist. So that's why you get the error you get.
As for the syntax in Question 2, the following should work (adapted from this page on the variadic syntax)
q)+\[100;1 2 3]
101 103 106
Upvotes: 0
Reputation: 1121
Question 1:
Use parse to determine order of execution
q)show pt:parse "(100+\\)1 2 3"; // need to escape \
((\;+);100)
1 2 3
q)eval each pt // should be clearer now
100
1 2 3
q)
q)value eval each pt // attempting to apply 100 to list which cannot be done
'Cannot write to handle 100. OS reports: Bad file descriptor
[0] value eval each pt
^
Question 2:
The projection is unary & is applied to the entire right argument. With unary application, evaluations will (attempt to) continue until convergence - https://code.kx.com/q/ref/accumulators/#unary-values
q)(neg\)1 2 3
1 2 3
-1 -2 -3
q)+[100]\[1 2 3]
'wsfull
m 0 68157440
Upvotes: 4