0

All,

I'm testing whether or not I can build a robust data model using nested dictionaries of data and f(x). The namespace/dot notation works well for what I'm attempting and we can reference the dictionary well but I'm unable to get the functions to execute when called as I thought.

I was hoping to reference the dictionary in the functions recursively to execute and recalculate when called and reduce incredible long functions.

Keeping the functions as part of the dictionary is an objective as we expect to have various functions for different dictionaries of data.

Here is a simple example of a nested dictionary and functions with the objective to calculate total rev based on the data in the inventory for prodA and prodB.

Many thanks in advance!

.inv.prodA.qty: 10 #10;
.inv.prodA.price: 10 #2.75;
.inv.prodA.cogs: 10 # 1.35;
.inv.prodA.net_price: {[x]  x[`price] - x[`cogs]};
.inv.prodA.net_price [.inv.prodA]; 
.inv.prodA.rev: {[x]  x[`qty] * (inv.net_price[ x[`price] - x[`cogs]])};

.inv.prodB.qty: 20 #2.50;
.inv.prodB.price: 20 #1.50;
.inv.prodB.cogs: 20 # 0.25;
.inv.prodB.net_price: {[x]  x[`price] - x[`cogs]};
.inv.prodB.net_price [.inv.prodB]; 
.inv.prodB.rev: {[x]  x[`qty] * (inv.net_price[ x[`price] - x[`cogs]])*.5}; //Note that the rev f(x) are unique for each prodA and prodB

.inv.total.rev: {[x] x[`prodA`rev] + x[`prodB`rev]};  

1 Answer 1

2

I think you want something like:

.inv.prodA.net_price:{.inv[x;`price] - .inv[x;`cogs]};
.inv.prodA.rev:{.inv[x;`qty] * .inv[x;`net_price]x};

.inv.prodB.net_price:{.inv[x;`price] - .inv[x;`cogs]};
.inv.prodB.rev:{.inv[x;`qty] * .inv[x;`net_price][x] *.5};

.inv.total.rev:{.inv[`prodA;`rev][`prodA] , .inv[`prodB;`rev]`prodB};  

q).inv.total.rev[]
14 14 14 14 14 14 14 14 14 14 1.5625 1.5625 1.5625 1.5625 1.5625 1.5625 1.562..

Changed your last function to an append because they're not the same length for addition. If you want to change the .inv into a variable too then you can modify the functions to pass those through as follows:

.inv.prodA.net_price:{y[x;`price] - y[x;`cogs]};
.inv.prodA.rev:{y[x;`qty] * y[x;`net_price][x;y]};

.inv.prodB.net_price:{y[x;`price] - y[x;`cogs]};
.inv.prodB.rev:{y[x;`qty] * y[x;`net_price][x;y] *.5};

.inv.total.rev:{x[`prodA;`rev][`prodA;x] , x[`prodB;`rev][`prodB;x]};  

q).inv.total.rev[`.inv]
14 14 14 14 14 14 14 14 14 14 1.5625 1.5625 1.5625 1.5625 1.5625 1.5625 1.562..
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Terry - I was having some issue's with arguments on the component variables this helps. On the second example, what would the equivalent to .inv.total.rev[] .inv.prodA.rev [`prodA] .inv.prodB.net_price [`prodB
Actually, got it sorted - had my x,y reversed.inv.total.rev[`.inv] .inv.prodA.rev[`prodA;`.inv] .inv.prodA.net_price[`prodB;`.inv] MANY THANKS AGAIN!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.