DSLs and DSVs (Domain Specific Languages and Vocabularies) can be super helpful and are not complicated to build.
Example
The mean return to a portfolio is the weighted average return of the stocks making up the portfolio.
Let’s say a portfolio consists of 30% GOOG shares with a mean daily return of 0.14% and 70% MSFT shares, mean daily return of 0.03%.
The mean daily return is:
(0.3 * .14) + (0.7 * 0.03) = 0.063
Another portfolio of 50% GOOG and 50% MSFT gives
(0.5 * .14) + (0.5 * 0.03) = 0.085
The PowerShell DSV
I’d like to represent the portfolio like the following, legal PowerShell syntax.
Let’s build up the Domain Specific Vocabulary
Lines 1-3 enable the DSV to work and prints:
Adding lines 4-8 will output the snippet below. Note line 7 uses “&”, the PowerShell call operator, to evaluate the script block.
Transform the Data into objects
Let’s transform the data into objects with properties so it prints the following table, with the added plus of addressable properties.
Calculate the Weighted Average
Adding a hash table lookup in lines 4-7 and the properties MeanDailyReturn and WeightedAverage in lines 18-24.
Lines 20 and 23 look up the MeanDailyReturn by stock name using dot notation.
In an upcoming post
The transformation of the DSV into objects and properties is finished. In an upcoming post we’ll add some more detail and play with the output.
{ 0 comments… add one now }