The Apply Function and the $ Shortcut
The Apply function is a function value that simply applies the second parameter to the first one.
So the effect of Apply [p1] [p2] is the same as p1 [p2].
If no parameter is specified for the $ shortcut, it returns the Apply function value.
op_multiply = LingoF().open(#Lingo).op_multiply
apply = $()
put apply [op_multiply] [8] [5]
-- 40
If parameters are specified for the $ shortcut, they will be applied one by one.
For example
$ = LingoF().open(#Lists)
take = $.take -- take the first n-elements from a list
put take [3] [ [3,90,2,5,57] ]
-- [3, 90, 2]
Here the last line could be replaced by
put $(take,3,[3,90,2,5,57])
If the first parameter is a symbol, it will automatically look into the Lingo Module for a standard Lingo function or operator with that name.
put $(#sin) [1]
-- 0.8415
put $(#*) [8] [5]
-- 40
If more parameters are specified they will be applied one by one to this function value.
So for example $(#+) is our add function, $(#+,2) or just $(#+2) (*) is our Add2 function.
The previuos example could be written as:
put $(#*) [8] [5]
or
put $(#*,8) [5]
or
put $(#*,8,5)
There is another “hidden” functionality in the $ shortcut.
There are some Lingo functions that expects a variable number of parameters, from a FP approach that’s something called a variadic function and is not supported by LingoF function values, but it is supported natively for standard Lingo functions.
Those functions, as for example List and the PropList are implemented in the Lingo Module as a function that expect a Linked List as parameter.
You can create quickly a Linked List with the ! shortcut
but if you are already using the $ shortcut that’s not necessary.
When calling Standard Lingo Functions that accepts a variable number of parameters as LingoF shortcuts are not function values, just specify as many parameters as needed.
For example to create a Property List you can do:
put $(#proplist,#x,50,#y,100)
– [#x: 50, #y: 100]
This is very useful to create a function value that will return a list using lambda expressions.
Here is a simple example:
p = var()
pntFN = fun(p) [ $(#proplist,#x,p[1],#y,p[2]) ] — converts a point in a property list
put pntFN[point(23,63)]
– [#x: 23, #y: 63]
Now going back to our already discussed % function, we can express it as:
fun(x,y) [ $(#/, $(#*,y,100) ,x) ]
Readability? If you are not familiar with Lisp related languages you will say “zero”.
If you are already familiarized with this kind of syntax I’ll get maybe a better score.
Because LingoF is implemented over Lingo it uses the Lingo Parser and Evaluator that’s why we arrive to this kind of syntax limitations.
$ = LingoF().open(#Lingo,#Lists)
x = var()
y = var()
lstFN = $.map [ fun(x,y) [ $(#/, $(#*,y,100) ,x) ] [30] ]
put lstFN [ [3,20,25,30,10] ]
-- [10, 66, 83, 100, 33]
Anyway if you get used to this syntax it is still compact, powerful and you can achieve great goals with few code programming this way.
(*) Lingo has a strange behavior when two expression are specified it automatically separates them as exp1,exp2 . But it works only with two expressions.
Hi…
http://www.webcamgirls4.com/...