Changes in Filter Methods

There has been a
recent change in the way callback functions of the various filter functions
work. Earlier, the callback functions used to take only one element at a time.
Following were the syntaxes for the 3 most commonly used filter functions.

ArrayFilter - arrayFilter(array,function(arrayElement){return true|false;});
ListFilter - listFilter(list,function(listElement){return true|false;});
StructFilter - structFilter(struct,function(key, value){return true|false;});

 

As we can see, for
all of these filters, the callback function has access to only one item at a
time. Effectively this means that, all the filters that can be applied have to be independent of all the other elements of the array/list/struct.
This was done keeping in mind that, for filtering an array/list/struct, we can
access the structure element by element and apply the common test on each of
them and thereby return true/false depending on the passing/failure of the element
in the conditions written in the callback function.

 

 But, this limits the functionality of the
filter function to a great extent. There can be a lot of real life scenarios,
where the filtering of one element has clear dependency on one or more of the
other elements of the array/list/struct. For example, suppose we want to filter
out all the elements of a list that are greater than the first element.
Previously, this was not possible.

 

Thus, we brought a
change in the design of the filter functions. Now, the callback functions of
all the three filters have access to the entire array/list/struct apart from
the current element. This enhances the power of filters to a great extent. Much
more sophisticated filters can be written for each of them especially for the
cases which have dependency on other element(s) apart from the current element.
For example, we can now easily write filters such as: Filter out all the
elements of the list which are greater than the next element.

 

The updated syntaxes
of filters are:

ArrayFilter - arrayFilter(array,function(arrayElement, [,index, array]){return true|false;});
ListFilter - listFilter(list,function(listElement, [,index, list, delimiter, includeEmptyFields]){return true|false;});
StructFilter - structFilter(struct,function(key, value, [, struct]){return true|false;});

Edit: This change has gone out in ColdFusion11-Update5. Thanks to @Charlie and @Adam for bringing it up.

3 thoughts on “Changes in Filter Methods

Leave a Reply

Your email address will not be published. Required fields are marked *