Whatever message this page gives is out now! Go check it out!
<cfscript>
function helloTranslator(required String helloWord) {
return function(required String name) { return "#helloWord#, #name#"; }
;
}
helloInFrench=helloTranslator("Bonjour")
writeOutput(helloInFrench("John"))
</cfscript><cfscript>
function operation(required string operator){
return function(required numeric x, required numeric y){
if(operator == "add")
{
return x + y;
}
else if(operator == "subtract"){
return x - y;
}
}
}
myval_addition=operation("add");
myval_substraction=operation("subtract");
writeoutput(myval_addition(10,20));
writeoutput(myval_substraction(10,20));
</cfscript><cfscript>
function operation(required numeric x, required numeric y, required function logic)
{
result=logic(x,y);
return result;
}
add = operation(10,20, function(required numeric N1, required numeric N2)
{
return N1+N2;
});
subtract = operation(10,20, function(required numeric N1, required numeric N2)
{
return N1-N2;
});
</cfscript>
<cfdump var="#add#">
<cfdump var="#subtract#">var c2 = function () {..}Function function exampleClosure(arg1)
{
function exampleReturned(innerArg)
{
return innerArg + arg1;
}
/*
return a reference to the inner function defined.
*/
return exampleReturned;
}var c2 = function(arg1, arg1) {..}
c2(arg1=1, arg2=3);hello = function (arg1)
{
writeoutput("Hello " & arg1);
};
hello("Mark");var c2 = function(arg1, arg1) {..}
argsColl = structNew();
argsColl.arg1= 1;
argsColl.arg2= 3;
c2(argumentCollection = argsColl);Scenario where closure is defined | Scope |
In a CFC function | Closure argument scope, enclosing function local scope and argument scope, this scope, variable scope, and super scope |
In a CFM function | Closure argument scope, enclosing function local scope and argument scope, this scope, variable scope, and super scope |
As function argument | Closure argument scope, variable scope, and this scope and super scope (if defined in CFC component). |
isClosure(closureName)Parameter | Description |
closureName | Name of a closure. Must not be in quotation marks.Results in an error if not a defined variable or function name. |
<cfscript>
isClosure(closureName)
{
// do something
}
else
{
// do something
}
</cfscript>/**
* @name employee
* @displayname ColdFusion Closure Example
* @output false
* @accessors true
*/
component
{
property string Name;
property numeric Age;
property string designation;
property string location;
property string status;
}<!---filter.cfc--->
<cfcomponent>
<cfscript>
//Filter the array based on the logic provided by the closure.
function filterArray(Array a, function filter)
{
resultarray = arraynew(1);
for(i=1;i<=ArrayLen(a);i++)
{
if(filter(a[i]))
ArrayAppend(resultarray,a[i]);
}
return resultarray;
}
function getEmployee()
{
//Create the employee array.
empArray = Arraynew(1);
ArrayAppend(empArray,new employee(Name="Ryan", Age=24, designation="Manager", location="US"));
ArrayAppend(empArray,new employee(Name="Ben", Age=34, designation="Sr Manager", location="US"));
ArrayAppend(empArray,new employee(Name="Den", Age=24, designation="Software Engineer", location="US"));
ArrayAppend(empArray,new employee(Name="Ran", Age=28, designation="Manager", location="IND"));
ArrayAppend(empArray,new employee(Name="Ramesh", Age=31, designation="Software Engineer", location="IND"));
return empArray;
}
</cfscript>
</cfcomponent><!---arrayFilter.cfm--->
<cfset filteredArray = arraynew(1)>
<cfset componentArray = [3,6,8,2,4,7,9]>
<cfscript>
obj = CreateObject("component", "filter");
// Filters employees from India
filteredArray = obj.filterArray(obj.getEmployee(), function(a)
{
if(a.getLocation()=="IND")
return 1;
else
return 0;
});
writedump(filteredArray);
//Filters employees from india whos age is above thirty
filteredArray = obj.filterArray(obj.getEmployee(), closure(a)
{
if((a.getLocation()=="IND") && (a.getAge()>30))
return 1;
else
return 0;
});
writedump(filteredArray);
// Filters employees who are managers
filteredArray = obj.filterArray( obj.getEmployee(), function(a)
{
if((a.getdesignation() contains "Manager"))
return 1;
else
return 0;
});
writedump(filteredArray);
</cfscript><cfscript>
// Create function mul that takes arguments a and b
function mul(a,b){
return a*b
}
// store the value of function in the variable c
c=mul(3,4)
WriteOutput("The product is: " & c)
</cfscript><cfscript>
// Create function mul
mul=(a,b)=>{return a*b} // Use the arrow operator
// store the value of function in the variable c
c=mul(3,4)
WriteOutput("The product is: " & c)
</cfscript>(param1, param2, …, paramN) => { statements }<cfscript>
sumLambdaFunc = (a,b) => {
sum=a + b;
return sum;
}
writeOutput("The sum of a and b is: " & sumLambdaFunc(7,5))
</cfscript>(param1, param2, …, paramN) => return statement<cfscript>
multiplyLambdaFunc = (x,y) => x*y
writeOutput("The product of x and y is: " & multiplyLambdaFunc(7,5))
</cfscript>param1 => return statement<cfscript>
squareLamdaFunc = x => x*x
writeOutput("The square of x is: " & squareLamdaFunc(7))
</cfscript>x => x > 10 // This is interpreted as : (x) => {return x > 10 ;}<cfscript>
message=()=>return "Hello World!"
WriteOutput(message())
</cfscript><cfscript>
message=()=> "Hello World!"
WriteOutput(message())
</cfscript><cfscript>
myarray = [() => {return 1}, () => {return 2}]
writedump(myarray)
writeOutput(myarray[1]()) // value 1
writeOutput(myarray[2]()) // value 2
</cfscript><cfscript>
newArray =[(x,y) => {return x+y;}, (x,y) => {return x*y}, (x,y)=> {return x-y} ];
Writedump(newArray)
Writedump(newArray[1](6,4))
Writedump(newArray[2](6,4))
Writedump(newArray[3](6,4))
</cfscript><cfscript>
array2 = [(arg1) => { return arg1+1 }, (arg1) => { return arg1 }]
writedump(array2)
writeDump(array2[1](3))
writeDump(array2[2]())
array3 = [(String arg1=NULL) => { return arg1 }, (String arg1=2) => { return arg1 }]
writedump(array3)
writeDump(array3[2]())
</cfscript><cfscript>
// object literals
myfunction=()=>{return value= 'test'}
writedump(myfunction())
</cfscript>See the example below.
<cfscript>
function firstFunction1(function closure) {
if(isClosure(closure))
return closure();
return;
}
function secondFunction1(function closure,string arg1) {
return closure(arg1);
}
function thirdFunction1(string arg1, function closure, string arg2) {
return closure(arg1,arg2);
}
function fourthFunction1(function closure) {
return closure(insideClosure=() => {return 1;});
}
function fifthFunction1(function closure1, function closure2) {
return closure1(closure2);
}
</cfscript>
<cfscript>
output = firstFunction1(closure=() => {return "Hello";});
writeOutput(output);
</cfscript>
<cfscript>
output = secondFunction1(closure=(arg1) => {return arg1;},arg1="Hello");
writeOutput(output);
</cfscript>
<cfscript>
output = thirdFunction1(arg1="Hello",closure= (arg1,arg2) => { return arg1 & arg2;},arg2="World");
writeOutput(output);
</cfscript>
<cfscript>
output = fourthFunction1(closure = (insideClosure) => { return insideClosure();});
writeOutput(output);
</cfscript>
<cfscript>
output = fifthFunction1(closure1 = (closure2) => { return closure2();}, closure2 = () => { return 1;});
writeOutput(output);
</cfscript><cfscript>
// array map using lambda
numbers=[1,4,6,9]
double=numbers.map((numbers)=>{return numbers*2})
writedump(double)
</cfscript><cfscript>
// array filter using lambda
superheroes=[
{"name":"Iron Man","member":"Avengers"},
{"name":"Wonder Woman","member":"Justice League"},
{"name":"Hulk","member":"Avengers"},
{"name":"Thor","member":"Avengers"},
{"name":"Aquaman","member":"Justice League"}
];
writedump(superheroes)
filtered=superheroes.filter((superheroes)=>{
return (superheroes.member=="Avengers")
})
writedump(filtered)
</cfscript><cfscript>
// array reduce using lambda
numbers=[1,3,5,7,9]
sum=numbers.reduce((previous,next)=>{
return previous+next
},0)
writeOutput(sum)
</cfscript><cfscript>
// chaining of lambda functions
numbers=[1, 2, 4, 5, 6, 7, 7, 9, 11, 14, 43, 56, 89]
// find if number is even
isEven=(x)=>{return x%2==0}
// add 2 to number
addTwo=(x)=>{return x+2}
// chain the functions
result=numbers.filter(isEven).map(addTwo)
writedump(result)
</cfscript><cfset myarray=[
{name="Thomas", age="22"},
{name="Zaza", age="36"},
{name="Novak", age="136"},
{name="Marin", age="361"},
{name="Rafa", age="3"},
{name="$bl0091@", age="-23"}
]>
<!--- define closure function --->
<cfset closure=function (e1,e2){
return compare(e1.name,e2.name);
}>
<cfset ar = arraySort(myarray,closure)>
<cfdump var="#myarray#"><cfset print=()=>{
return "Hello World"
}>
<cfoutput>
#print()#
</cfoutput><cfset myClosure= (default)=> {
return (default) =>
{
return ()=> {
return "Hello World"
}
}}>
<cfoutput>#myClosure("testing arrow in nested levels")()()#</cfoutput>
Also,
<cfset myClosure= (default)=> {
return (default) =>
{
return (string s)=> {
return s
}
}}>
<cfoutput>#myClosure("testing arrow in nested levels")()("Hello World")#</cfoutput><cfscript>
myArray1 = ["CF10","Zeus","CF9","Centaur"];
resultArray = ArrayFilter(array=myArray1,callback=(any arrayEntry) =>{
if(arrayEntry == "CF9" || arrayEntry == "Centaur")
return false;
return true;
});
for(var1 in resultArray) {
writeOutput(var1 & "<br>");
}
</cfscript><cfscript>
numArray = [10,20,30,150,400,99,100];
resultArray = ArrayFilter(array=numArray,callback=(any arrayEntry) =>{
if(arrayEntry < 100)
return false;
return true;
});
for(var1 in resultArray) {
writeOutput(var1 & "<br>");
}
</cfscript><cfscript>
myArray1 = ["ColdFusion","Hello","San Jose","Adobe","Systems"];
numArray = [60,3,30,4,500,44];
</cfscript>
<cfscript>
index = ArrayFind(array=myArray1,callback=(any object1) => {
if(object1 == "Systems")
return true;
return false;
});
writeOutput("Found at " & index);
</cfscript>
<cfscript>
index = ArrayFind(myArray1,(any object1) =>{
if(object1 == "Hello")
return true;
return false;
});
writeOutput("Found at " & index);
</cfscript><cfscript>
// Define an array of structs
myArray = [
{name="Thomas", age="22"},
{name="Zaza", age="36"},
{name="Novak", age="136"},
{name="Marin", age="361"},
{name="Rafa", age="03"},
{name="$bl0091@", age="-23"}
];
// Define a closure function that sorts the names in the array of structs
callback=function (e1, e2){
return compare(e1.name, e2.name);
}
// Use the closure function
arraySort(myArray,callback);
// Display the sorted array of structs
WriteDump(myArray);
</cfscript><cfset myClosure= function() {…}><cfset myarray=[
{name="Thomas", age="22"},
{name="Zaza", age="36"},
{name="Novak", age="136"},
{name="Marin", age="361"},
{name="Rafa", age="3"},
{name="$bl0091@", age="-23"}
]>
<!--- define closure function --->
<cfset closure=function (e1,e2){
return compare(e1.name,e2.name);
}>
<cfset ar = arraySort(myarray,closure)>
<cfdump var="#myarray#">