Laura
Forum Replies Created
-
Not entirely clear what youre looking for here. If you want to produce a 1N list for each symbol you can use
til
and avoid the sums and comparisons:q)select 1+til count i by sym from t sym | x ----| --------------- aapl| 1 2 3 4 5 goog| 1 2 3 4 5 6 7 8 msft| 1 2 3 4 5 6 7
but its hard to see a use for the lists. Perhaps you simply want to count rows for each symbol?
q)exec count i by sym from t aapl| 5 goog| 8 msft| 7 q)ts:1000 select sums i=i by sym from t 4 10720 q)ts:1000 exec count i by sym from t 1 9584
-
sums i=i by sym
did the trick
-
Laura
AdministratorApril 5, 2022 at 12:00 am in reply to: Q 4.0 can’t have space in command line argumentAs does my later version.
? q4.0 -args '12 34' KDB+ 4.0 2021.11.04 Copyright (C) 1993-2021 Kx Systems m64/ 12()core 65536MB sjt mackenzie.local 127.0.0.1 ... q).z.x "-args" "12 34"
-
that was it, thanks!
-
Yeah, I took a look at aj but ultimately wj just better suites my needs.
So I’ve got the tables sorted “sym “time but seem to be missing on the `p# part (pun maybe intended).
How does one `p# a column in a table properly?
-
To enlarge on Rians answer, the
hello1
you see is not the result. The stringhello
is written to the console. The result1
is also written to the console and followed by a newline as usual. Assigning the result makes the difference clear.q)1 “hello”
hello1
q)a:1 “hello”
helloq)
q)a 1
q)
-
Laura
AdministratorMarch 18, 2022 at 12:00 am in reply to: Have I developed the functional equivalent of a Boolean gate?Youre infested with parens! They might be confusing you.
First, those multiple right parens in the prompts: thats not q trying to look like Python. Each right paren after the initial
q)
indicates a suspended evaluation with associated state. Unless youre exploring that state, best to cut back the stack with a backslash lest you find names resolving in suspended state. (No sign of that here, but its good practice.)q){(x*y)-1 y*x}[0;0] 'type [1] {(x*y)-1 y*x} ^ q)) x / suspended state 0 q)) q)x / suspension cleared, no value of x 'x [0] x / suspension cleared, no value of x ^
Second, redundant and missing parens in your lambda indicate the syntax is new to you. The most important syntactic rule is that there is no hierarchy of functions. Operators are binary functions with infix syntax. They have short left scope and long right scope. The left argument is the value immediately on the left. The right argument of an operator, or the argument of a unary function, is the result of evaluating everything to its right. Famously:
q)3*4+5 27
Syntactically, a republic of functions. No precedence rules to remember. Just the above. (One rule to rule them all)
IO handles are integers. When you
hopen
a file or IO stream the handle for it is an integer and the integer behaves like a function. You can apply it. When you apply a file handle to an argument, the argument gets written to the file. The integers 0, 1, and 2 permanently refer respectively to the console, stdout, and stderr.So
(x*y)-1 ((y)*x)
tries to writey*x
to stdout. (Oops, type error, needs to be text.)Understanding this, we can rewrite your tests:
q){(x*y)-y*x}[0;0] 0 q){(x*y)-y*x}[0;1] 0 q){(x*y)-y*x}[1;1] 0
None of which will surprise you.
To continue your exploration you might like to use Each Right Each Left
/::
with a lambda to tabulate results:q)10 20 30 +/::1 2 3 4 5 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 q)0 1{(x*y)-y*x}/::0 1 0 0 0 0
-
Two expressions: one multiplies
0+0
by1%0&0
; the other adds them.The sum of two long zeros is long zero.
1%0
is float infinity0w
. The type of the right argument does not matter; Divide always returns a float.So the comparison is between
0*0w
and0+0w
.The latter is
0w
. Surely expected: infinity plusx
is infinity.The unexpected result then is
0*0w
. Is not0*x
always zero? But here its a float null0n
. I do not know the mathematical justification for this; perhaps someone else can shed light?BTW your lambdas could be simpler:
test1:{(x+y)*1%x&y} test2:{(x+y)+1%x&y}
-
Perhaps the author was trading memory for speed?
q)t:flip(`$'.Q.a)!26 10000000#26000000?1000 q)(cols `t)~key flip value `t 1b q)ts:10000 cols `t 12 528 q)ts:10000 key flip value `t 2 880
One of the loveliest aspects of working in q is how easy it is to experiment like this!
-
Laura
AdministratorMarch 17, 2022 at 12:00 am in reply to: Why do we put a 0# here: @[`.;t;@[;`sym;`g#]0#]TLDR: the expression initialises a list
t
of tables in the default namespace, setting the grouped attribute on the symbol column and removing all the table rows.How that? The function called is not Apply but Amend At
@
in its ternary form.The first argument is the default namespace
`.
, which, as a namespace, is also a dictionary.The second argument
t
will be a symbol vector of names of tables in the default namespace; i.e. globals.The third argument is the function to be applied to each table. Youve already spotted that the unary projection
@[;`sym;`g#]
sets the grouped attribute on thesym
column. It is composed (by juxtaposition) with the unary projection0#
. Applied to a table,0#
removes all the rows.So the function applied to each table named in
t
is the composition@[;`sym;`g#] 0#
. That is, remove the rows and set grouped on thesym
column. -
Laura
AdministratorMarch 16, 2022 at 12:00 am in reply to: Understanding this line from vanilla tick set upWhere
u
,v
,w
, etc. are unaries thenu v w@
is a unary composition equivalent to{u v w x}
.In this case two of the five unaries are projections. A little spacing clarifies:
(`time`sym~ 2# key flip value@) each t
-
Laura
AdministratorMarch 15, 2022 at 12:00 am in reply to: What does w[x;;0] mean in this line:del:{w[x]_:w[x;;0]?y};.z.pc:{del[;x]each t};The line consists of two definitions: of
del
and of.z.pc
.The former is a lambda that amends the value of an existing value
w
. We can see fromw[x;;0]
thatw
is a list with at least rank 3, i.e. three levels of indexing. Sayw
is a list in which the items are lists of lists. Thenw[x;;0]
refers to the first items of the lists in rowx
.q)show w:3 cut” “vs”The quick brown fox jumps over the lazy dog.”
“The” “quick” “brown” “fox” “jumps” “over” “the” “lazy” “dog.”
q)w[1;;0]
“fjo”
q)w[1]_:w[1;;0]?”j”
q)w (“The”;”quick”;”brown”) (“fox”;”over”) (“the”;”lazy”;”dog.”)
Then
w[x;;0]?y
finds the first occurrence ofy
among them. Where is an operator the syntaxx:y
is equivalent tox:xy
, so thew[x]_:
drops from rowx
the first list that begins withy
. -
The complete call in the white paper is
.Q.hdpf[`$":",.u.x 1;`:.;x;`sym]
.The first argument is
`$":",.u.x 1
. From the.Q.hdpf
documentation we see the first argument is a port for the HDB. It looks like.u.x. 1
is that as a string; for example"localhost:5002"
.`$":",
prefixes a colon and casts to symbol, creating a handle.