August 14, 2015
Changes in Filter Methods
Comments
(3)
August 14, 2015
Changes in Filter Methods
(3)

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 Comments
2015-08-18 07:35:57
2015-08-18 07:35:57

G’day Charlie (THIS IS ADAM SPEAKING 😉

I tested the fix on U5, and it’s all good.

I also note the article has now been updated to mention this too.

Sorted!

Like
2015-08-17 23:02:44
2015-08-17 23:02:44

Not based on the text in the article no.

I also note this remains the case still.

Like
2015-08-16 16:33:50
2015-08-16 16:33:50

@Charlie, it was in response to this bug I raised: https://bugbase.adobe.com/index.cfm?event=bug&id=3810965

And it was fixed in CF11u5.

Like
Add Comment