Reputation: 1581
Often I have to union join to append some data, using a global var result.
res();
{[someVar]
res,:: select x from y where (constraints related to someVar)
} each list of someVars
This is not exactly very functional style. Is there a different way to achieve this w/o globals?
Upvotes: 2
Views: 139
Reputation: 13572
A couple of points:
You don't need the double colon to assign globally when you're doing an append-in-place (aka a,:b
as opposed to a::a,b
). A single will work in this case since append-in-place works in the global context.
You're discounting the fact that append-in-place is very memory-efficient even if it is not "functional" and uses global context. For example:
q)\ts t:([]col1:10000000?100j)
110 134218192
q)\ts a:{x,select from y}/[t;2 cut ([]col1:til 6)]
74 268436720
q)\ts {t,:x} each 2 cut ([]col1:til 6)
0 1424
q)a~t
1b
The method of iterating using over (/) and avoiding globals creates a copy of the table thus doubling the memory usage. The append-in-place uses very little memory for the same result.
So ultimately there are some situations where having the global is a worthy trade-off, depends on the situation.
Upvotes: 4
Reputation: 141
res:(),/{[someVar]
select x from y where (constraints related to someVar)
} each someVars
will take the list of returned tables and join them back onto res with the over adverb (/)
https://code.kx.com/q/ref/adverbs/#over
Upvotes: 2