listReduceRight

This function iterates over every element of the list and calls the closure to work on that element. This function will reduce the list to a single value, from the right to the left, and will return the value.
NOTE: Use caution if using the member function. list.reduceRight and list.listReduceRight may behave differently.
NOTE:This function is not available in Lucee 5.x or lower. Potentially use reverse() instead.

listReduceRight(list, callback, initialValue[, delimiter][, includeEmptyFields]) → returns any

Member Function Syntax

list.listReduceRight(callback, initialValue[, delimiter][, includeEmptyFields])

This function requires Adobe ColdFusion 2021 and up.  Not supported on Lucee, etc.

Argument Reference

list string
Required

The input list.

callback any
Required

Closure or a function reference that will be called for each iteration.

Callback parameters:

  • result*any : The result of the reduce operation after the previous iteration.
  • item*any : The value for the current iteration's item.
  • index*numeric : The current index for the iteration.
  • list*string : A reference of the original list.

initialValue any
Required

Initial value which will be used for the reduce operation.

delimiter string
Default: ,

The list delimiter

includeEmptyFields boolean
Default: false

Include empty values?
Values:
  • true
  • false

Links more information about listReduceRight

Examples
Sample code invoking the listReduceRight function

Demonstrate how the function works from right to left.

myList="a,b,c,d";
newList=listReduceRight(myList,function(prev,next,idx,arr){ return prev & next & idx },"");
writedump(newList);

Expected Result: d4c3b2a1

Demonstrate the member function.

myList="a,b,c,d";
newList=myList.listReduceRight(function(prev,next,idx,arr){ return prev & next & idx },"");
writedump(newList);

Expected Result: d4c3b2a1

Demonstrate the behavior when there is an empty element.

myList="a,,,,,b,,,c,,,d";
newList=myList.listReduceRight(function(prev,next,idx){ return prev & next & idx },"");
writedump(newList);

Expected Result: d4c3b2a1

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub