• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Get started with Arrow functions in ColdFusion (2018 release)

Adobe Employee ,
Sep 25, 2019 Sep 25, 2019

Copy link to clipboard

Copied

ColdFusion (2018 release) Update 5 introduces a new way of writing functions, known as Arrow Function or Fat Arrow Function (=>).

 

Arrow functions allow you to use the fat arrow operator to define ColdFusion functions. You can either use parameters in the function definition or not use any. Arrow functions avoid the usage of curly brackets and make the return keyword optional while creating a function.

Basic syntax of the arrow operator

(param1, param2, ..., paramN) => {statements}

 Example

Traditional way of writing functions

For example, if you were to filter some data in an array using the function ArrayFilter, you would write the following code:

<cfscript>
	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"}
     ]
	 closure=function(item){
	 	return item.member=="Avengers"
	 }
     avengers=ArrayFilter(superheroes,closure)
     writeOutput("The filtered array is:")
     writeDump(avengers)
</cfscript>

In the code above, we've used a closure function, which uses the conventional way of defining functions, such as taking a parameter and then using the return keyword.

Use Arrow function

The same code can be rewritten by using the arrow function, as shown below:

<cfscript>
	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"}
     ]
     closure= item =>item.member=="Avengers"
     avengers=ArrayFilter(superheroes,closure)
     writeOutput("The filtered array is:")
     writeDump(avengers)
</cfscript>

In the code above, you can see that how the arrow function is used. In the function definition, the parameter item is defined. Then, the arrow operator (=>) ,followed by the body of the function, which is one statement that is interpreted as the return statement of the function. So, essentially, the code now looks more compact and readable.

No parameter

The most simple and straightforward function in ColdFusion uses no parameters. The following samples implement such functions using both traditional and arrow functions.

<cfscript>
	birthdayMessage=function (){
		writeOutput("Happy Birthday")
	}
	birthdayMessage()
</cfscript>

Using arrow functions, rewrite the above as,

<cfscript>
	birthdayMessage=()=> { writeOutput("Happy Birthday") }
	birthdayMessage()
</cfscript>

In the arrow function syntax, the function keyword disappears. The entire function takes only a single line of code. The empty parentheses indicate that the function doesn’t have any parameters and the fat arrow binds the function body to the parameters.

One parameter

Using a single parameter while defining a function, you could write a function as shown below:

<cfscript>
	helloName=function(required string name){
		writeOutput("Hello, my name is " & name)
	}
	helloName("John Doe")
</cfscript>

The traditional syntax of defining the function takes a single parameter and appends itself to the value of the string when returned.

Using arrow functions, you can rewrite the code above as:

<cfscript>
	helloName=(required string name)=>writeOutput("Hello, my name is " & name)
	helloName("Jack Doe")
</cfscript>

Two parameters

Here is the traditional way of writing a function using two parameters.

<cfscript>
	addTwoNum=function(required float a,required float b){
		return a+b
	}
	c=addTwoNum(5.5,7.1)
	writeOutput("The result is: " & c)
</cfscript>

Using arrow function, you can rewrite the same as,

<cfscript>
	addTwoNum=(required float a,required float b)=>a+b
	c=addTwoNum(6.5,17.1)
	writeOutput("The result is: " & c)
</cfscript>

Conditional statements

The following samples show how you’d write a conditional statement conventionally and with arrow functions.

<cfscript>
	mySwitch=function(required boolean state=0){
		if (state==1){
			writeOutput("Switch is ON")
		}
		else if (state==0){
			writeOutput("Switch is OFF")
		}
		else{
			writeOutput("Invalid state")
		}
	}
	mySwitch()
</cfscript>

Rewrite the code above using arrow functions.

<cfscript>
	mySwitch=(required boolean state=0)=>{
		switch(state){
			case 1:
				writeOutput("Switch is ON")
				break
			default:
				writeOutput("Switch is OFF")
		
		}
	}
	mySwitch()
</cfscript>

Array closures

The following samples show how to use array functions in array closures.

Example 1

<cfscript>
	array1 = [() => { return "Hello" }, () => { return "World" }] 
	writedump(array1[1]()) // Returns Hello
	writeDump(array1[2]()) // Returns World
</cfscript>

Example 2

<cfscript>
	array2 = [(arg1) => { return arg1+1 }, (arg1) => { return arg1 }] 
	writeDump(array2[1](3)) // Returns 4
</cfscript>

Example 3

<cfscript>
	array3 = [(String arg1="Hello") => { return arg1 }, (String arg1="World") => { return arg1 }] 
	writeDump(array3[1]()) // Returns Hello
</cfscript>

Example 4

<cfscript>
	array4 = [(numeric arg1) => { return function(numeric arg2) { return arg1 + arg2 } }, (numeric arg1) => { return function(numeric arg2) { return arg1 + arg2 } }]
	writeDump(array4[1](2)(3)) // Returns 5
</cfscript>

Arrow operator for tags

You can also extend Lambdas to ColdFusion tags, for example:

<cfset print=()=>{
    return "Hello World"
}>
<cfoutput>
    #print()#
</cfoutput>

For more information on Arrow functions/Lambdas and other core language updates, see the following:

In future blogs, we'll post more on the other new features in Update 5, including language enhancements.

TOPICS
Advanced techniques , Getting started

Views

2.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 25, 2019 Sep 25, 2019

Copy link to clipboard

Copied

Detailed info. Really helpful.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 19, 2019 Nov 19, 2019

Copy link to clipboard

Copied

LATEST

Unfortunately these break CF Builder. If used in a CFC, they show as an "Invalid Token" and break the Outline View.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation