Using first class functions
For those who are still not familiarized with FP Programming, first class functions can be called, passed as parameters and returned as any other object value.
So if you are going to use LingoF you will have two kind of functions:
- Lingo Handlers
From now on we will refer them as “Handlers”.
As every Lingo programmer knows they can be called with this syntax: {handler} ( {param 1} , {param 2} , … , {param n} )
If fewer parameters are passed void is assigned to missing parameters. If more parameters are passed they are ignored.
LingoF First Class Functions �
From now we will refer to all of them as “Function Values”.
They can be called, stored in variables, passed as paramter and returned.
The syntax to call them is:
{Function Value} [ {param 1} ] [ {param 2} ] … [ {param n} ]
If fewers parameters are passed the result will be another function, this resulting function is a partial application of the original function.
If more parameters are passed it will try to “apply” each parameter to the result. You may think this is no sense, but if the result is another function value it will work.
Examples:
Let’s suppose we have already defined the following Function Values:
map: Expect a function as first parameter and a list as second parameter and returns a list with each result of applying the function to each element from the original list.
add: Expect two numeric parameters and returns their sum.
lst = [10,15,20,30]
Add2 = add [2]
put map [add2] [lst]
-- [12, 17, 22, 32]
Or in just one line
put map [ add [2] ] [ [10,15,20,30] ]
-- [12, 17, 22, 32]
In these very basic examples we have a function value that returns a function, currying add with add [2] , and a function value that accept, moreover it expect a function as first parameter: map.
Note that is innecesary to assign add [2] to the add2 variable, because both expressions are equivalent computations and in terms of readabiliity they really reads the same.