BlackPearl
BlackPearl

Reputation: 125

read function name and args from a table row and iterate and execute it and store the output to a single table

I have a data.csv which looks like below having a function name and a dictionary.

function,args
fun1,(`startDate`endDate`sym`rollPerct`expDateThreshold`expDateThresholdExpiry)!(.z.D-5;.z.D;`AAPL;0.8;10;1)
fun2,(`startDate`endDate`sym`rollPerct`expDateThreshold`expDateThresholdExpiry)!(.z.D-5;.z.D;`MSFT`ZAK;0.8;10;1)
fun3,(`startDate`endDate`sym`rollPerct`expDateThreshold`expDateThresholdExpiry)!(.z.D-5;.z.D;`NAFK;0.8;10;1)

And If I read the data tab:("S*";enlist ",") 0:`$data.csv

enter image description here enter image description here

Now, I want to iterate all rows from the table like below and call them and save all 3 results to a single table res

fun1 [(`startDate`endDate`sym`rollPerct`expDateThreshold`expDateThresholdExpiry)!(.z.D-5;.z.D;`AAPL;0.8;10;1)]
fun2 [(`startDate`endDate`sym`rollPerct`expDateThreshold`expDateThresholdExpiry)!(.z.D-5;.z.D;`MSFT`ZAK;0.8;10;1)]
fun3 [(`startDate`endDate`sym`rollPerct`expDateThreshold`expDateThresholdExpiry)!(.z.D-5;.z.D;`NAFK;0.8;10;1)]

Code snippet to iterate over f1[args], f2[args] and f3[args]. Combine all 3 results into a single table. I had used loop here, but there should be something better than loop here? let me know if any?

cnt:(count table);  //get count of table
ino:0;  //initialize out counter to 0
tab::flip (`date`sym`ric!(`date$();`symbol$();`symbol$())); //create a global table so it can hold iteration data
//perform iteration where f1[args],f2[args],f3[args]=tab
while[ino<cnt;
 data:exec .[first function;args] from table where i=ino;
 upsert[`tab;data];
 ino:ino+1
 ]
//tab now has all the itration data of f1 f2 f3
tab

Upvotes: 0

Views: 154

Answers (1)

Matthew Madill
Matthew Madill

Reputation: 880

if your inputs are correctly ordered for all functions, the following simple example should work

q)f1:{x+y+z+2};f2:{x*y*z*22};f3:{x%y%z%42};
q)tab:([]func:`f1`f2`f3;args:`x`y`z!/:3 cut til 9)
q)tab
func args
-----------------
f1   `x`y`z!0 1 2
f2   `x`y`z!3 4 5
f3   `x`y`z!6 7 8
q)update res:func .'get'[args]from tab
func args         res
---------------------------
f1   `x`y`z!0 1 2 5
f2   `x`y`z!3 4 5 1320
f3   `x`y`z!6 7 8 0.1632653

NB: if you're loaded args are strings, you'll want to parse these

for example - taking the above again

q)tab:update .Q.s1'[args]from tab
q)tab
func args
-------------------
f1   "`x`y`z!0 1 2"
f2   "`x`y`z!3 4 5"
f3   "`x`y`z!6 7 8"
q)meta tab
c   | t f a
----| -----
func| s
args| C

q)tab:update'[reval;parse]'[args]from tab
q)tab
func args
-----------------
f1   `x`y`z!0 1 2
f2   `x`y`z!3 4 5
f3   `x`y`z!6 7 8
q)meta tab
c   | t f a
----| -----
func| s
args|
q)update res:func .'get'[args]from tab
func args         res
---------------------------
f1   `x`y`z!0 1 2 5
f2   `x`y`z!3 4 5 1320
f3   `x`y`z!6 7 8 0.1632653

reval in the above will try to stop anything dodgy being ran but i would avoid parsing code straight from files where possible

Upvotes: 3

Related Questions