Day 6 : ColdFusion 10 and Closure

Wow, ColdFusion now support closure. I am fan of closure and heavily using it on JavaScript specially for ajax call’s callback function. Closure is function within function which close all variables of outer function when returned/created. May be not pure theoretically true definition but this is how can I can define in practical term that I have used. 

In ColdFusion 10 documentation very simple example given to understand concept of Closure.

[code:cf]<cfscript>
function helloTranslator(String helloWord) { 
        return function(String name) { 
                return "#helloWord#, #name#"; 
             }; 

helloInHindi=helloTranslator("Namaste"); 
helloInFrench=helloTranslator("Bonjour"); 
writeoutput(helloInHindi("Anna")); 
writeoutput("<br>"); 
writeoutput(helloInFrench("John")); 
</cfscript>[/code]

Here is output of this code:
[code:cf]Namaste, Anna
Bonjour, John [/code]

helloTranslator is function which return reference of another function which you can assign it to variable and call as function. Whenever helloInHindi = helloTransalator("Namaste") statement executed function return inner function reference so you can call inner function but beauty is inner function can still use variables from outer function ‘helloTranslator’. If you have notice then return statement in inner function use "helloWorld" variables which is argument of outer function. At the time of assignment it closes all variables from outer function and can be use on all subsequent call.

P.S. Closure retains the reference to the environment at time it created not when closure called. 

Here is simple example to prove that.

[code:cf]<cfscript>
function mainFunction(){
var curtime = now();
return function(String name){
return "Hello #name#, Current time:#timeformat(curtime,’hh:mm:ss tt’)#";
};
}
closure1 = mainFunction();
sleep(2000);
closure2 = mainFunction();
writeOutput(closure1(‘Pritesh’));
writeOutput("<br/>");
writeOutput(closure2(‘World’));
</cfscript>[/code]

mainFunction which return reference of another inner function. Outer function have local variable curtime to store current time of server and inner function use same variable to generate output to say hello. We just created two closures named closure1 and closure2 but give gap of 2 seconds between creation and after creation just call both closures and here is output.

[code:text]Hello Pritesh, Current time:12:54:27 PM
Hello World, Current time:12:54:29 PM[/code]

Now let’s make slight change in above code to prove that all outer functions variable retain value at the time of closure creation. I just move sleep(2000) statement between two calls.

[code:cf]<cfscript>
function mainFunction(){
var curtime = now();
return function(String name){
return "Hello #name#, Current time:#timeformat(curtime,’hh:mm:ss tt’)#";
};
}
closure1 = mainFunction();
closure2 = mainFunction();
writeOutput(closure1(‘Pritesh’));
sleep(2000);
writeOutput("<br/>");
writeOutput(closure2(‘World’));
</cfscript>[/code]

And output is 
[code:cf]
Hello Pritesh, Current time:01:00:47 PM
Hello World, Current time:01:00:47 PM[/code]

Notice time printed on both output, in first one you will see gap of 2 seconds and in second out put both have same time as we have immediate call for creation of both closures.

That’s it for today… Tomorrow have some more fun with closures.