Shortcuts and Syntax helpers

Now you’re ready to understand why I defined some shortcuts which I name as Syntax Helpers.
Consider this problem: I have lists with number that represent measures. I want to have a function to map this numbers in percentage. So I can define a % function that takes the value that represent 100% and the second will be the particular value I want to convert.
Applying this function with only the first parameter as the first parameter for the map function will result in the desired function value.


% = fun("x,y : y*100/x")

and then

$ = LingoF().open(#Lingo,#Lists)
% = fun("x,y : y*100/x")
topValue = 30
lstFN = $.map [ % [topValue] ]
put lstFN [ [3,20,25,30,10] ]
-- [10, 66, 83, 100, 33]

Too many variables for a simple FP computation. Let’s try to reduce them:

$ = LingoF().open(#Lingo,#Lists)
lstFN = $.map [ fun("x,y : y*100/x") [30] ]
put lstFN [ [3,20,25,30,10] ]
-- [10, 66, 83, 100, 33]

It’s getting better, but we are using Lingo Expressions. What’s wrong with Lingo Expressions? Nothing except that if we are inside some kind of loop the expression will be regenerated and recompiled with each iteration.
OK, let’s define it combining existing functions, I have all Lingo Math operators already defined in Lingo module. So % can be:
x = var()
y = var()
% = fun(x,y) [ $.op_multiply [y] [$.op_div [100] [x] ] ]

Readability? Can we refer to Lingo operators with a more compact syntax rather than using alias for each one?
Yes, we can using a Syntax Helper: the $ shortcut.

Leave a Reply

Security Code: