https://learninghub.kx.com/forums/topic/help-understanding-a-function
hello everyone!
i am not sure to really understand the operations order in this function .
.quantQ.io.saveSplayedTab:{[tabPath;pvar;table]
// tabPath – path where to store the table
// pvar – variable to sort and index on for fast querying
// table – name of the table to save
:@[;pvar;p#] pvar xasc (
sv (tabPath;;table;
)) set .Q.en[tabPath] get table;
};
i am under the impression that because of left to right evaluation, it is saved before it is ordered. can someone explain ?
thank you!!
and also would this not make more sense: ( sv (tabPath;
;table;)) set @[; pvar;
p#] pvar xasc .Q.en[tabPath] get table
Hey fav_pa,
I believe there are a few more mistakes in the code you posted, can you share the source where you have it from?
First, the part to create the path to save down is missing some code
q) ` sv (`$":path/toTable";`tableName;`)
`:path/toTable/tableName/
Then you also miss the `p in
@[;pvar;`p#]
Other than that, the second code you posted makes sense
`(` sv (tabPath;; table; )) set @[; pvar;`p#] pvar xasc .Q.en[tabPath] get table
Yes with right to left evaluation it is being saved then sorted.
The reason this may have been done is that sorting in memory will temporarily use extra memory to perform the operation.
https://code.kx.com/q/ref/asc/#sorting-data-on-disk
Sorting on disk is very memory efficient as it does not bring the full table in to memory.
The trade off is it is slower as each column gets written twice.
q)tab:flip (`$/:.Q.an)!{1000000?10000.0}each .Q.an
q)\ts `a xasc `:tab/ set tab
q)
1824 25168800
q)\ts `:tab/ set `a xasc tab
1165 536874192
q)536874192%25168800
21.33094 //Sorting in memory uses 21x memory than sorting on disk
Extremely interesting, thank you very much for your answer!