Integration with Javascript
Disclaimer: I still didn’t find official documentation from Adobe recommending interaction between Lingo and Javascript in the same Director movie, so I can’t oficially recommend interacting LingoF with Javascript.
Lingo and Javascript shares lots of features, but Javascript supports natively functions as function values.
With the addition of LingoF there are even more common features but then a remarkable difference between LingoF and Javascript is that Javascript doesn’t have currying by default.
So integrating LingoF function values with JS functions is possible, it’s just about wrapping LingoF’s Function Values in Javascript Functions and vice versa.
Use LingoF Function Values in Javascript
In the following sample code let’s assume LingoFV is a variable containing an already defined LingoF Function Value.
1) Call a LingoF Function Value from Javascript using the invoke and the apply methods.
lingoFV.invoke( arg1 , arg2 , arg3 );
which is the same as
lingoFV.invoke( arg1 ).invoke( arg2 ).invoke( arg3 );
Also you can use the apply method the same way it is used for Javascript Functions.
lingoFV.apply( {} , [arg1 , arg2 , arg3 ] );
2) Wrap a LingoF Function Value into a Javascript Function
jsFV = JsWrapLingoFunctionValue(lingoFV);
JsWrapLingoFunctionValue is a simple Function Wrapper, if the parameter is already a Javascript function it will return it as it is.
function JsWrapLingoFunctionValue(fn)
{
if (typeof(fn)=="function") return fn;
return function() { return fn.apply(0,arguments); };
}
You can easily define your own wrapper.
3) Use a Javascript Curry function also with LingoF Function Values.
Because LingoF function values support the same apply method as Javascript functions you can use some generic functions defined for Javascript for both kind of function values.
This Javascript version of the curry function was succesfully tested, just put this script in a movie script and then you can try the following.
jsCurriedFn = curry(javascriptFV);
lfCurriedFn = curry(lingoFV);
anotherFn = jsCurriedFn( arg1 )( arg2 );
yetAnotherFn = lfCurriedFn( arg1 )( arg2 );
Define a LingoF Function Value within Javascript
1) Use any Shortcut ( except ! and remember #x should be symbol(x) )
2) Create it from a Javascript Function Value
fn = fun(jsFN)
3) Create it from a curried Javascript Function Value. Normally you will need to supply an extra parameter to specify how many parameter accepts the original Javascript Function.
argscount = javascriptFV.length;
curriedJSFunction = curry(javascriptFV);
LingoFV = fun( curriedJSFunction , argscount );
Conclusion
In the case you are using Javascript as main language but you use Lingo as well, let’s say for some UI functionalities you wouold like to wrap your LingoF function values into Javascript function values. All functions will be uncurried as this is the default behaviour in Javascript.
If you are using Lingo as main language but you want to use some functions in Javascript you can do the opposite. Wrapped Javascript functions automatically becomes curried, as any LingoF function value.
A third scenary could be the case you use both languages, fucusing in Javascript but with an even more Functional Programming approach, using currying as with LingoF. Then you can use a curry function to wrap both Javascript and LingoF function values.