Passing functions as arguments

I never realized it before but you can pass functions as arguments in CFML. It works really nicely in Coldfusion 10. Take a look at this example. It is a very primitive markdown parser that replaces bold markup with a HTML equivalent:

 
     private String function _parseMarkdown( content ){
 
            var input = arguments.content;
 
            /* Replacement closure methods */
 
                private String function _replaceBold(required String content){
                    var processed = REReplaceNoCase(arguments.content, "(\*\*|__)(.*?)", "<b>","one");
                        return REReplaceNoCase(processed, "(\*\*|__)", "</b>","one");
                }
 
                /* Add more closures to handle other markdown syntax ..... */
 
            /* Replacement patterns */
 
            var patterns = [
                [ "(\*\*|__)(.*?)(\*\*|__)", _replaceBold ]
                /* Add more patterns to handle other markdown syntax ..... */
            ];
 
            /* Recursive replace */
 
            function _replaceMatch(start,pattern){
 
                var matches =  REFindNoCase(pattern[1], input, arguments.start , true);
 
                if (arrayLen(matches.len) && matches.pos[1] > 0){
 
                    var needle = mid(input, matches.pos[1], matches.len[1]);
                    var fn = pattern[2];
                        input = replaceNoCase(input, needle , fn(needle));
                    _replaceMatch(matches.pos[1],pattern);
 
                }
                return input;
            }
 
            for (pattern in patterns) {
               var processed = _replaceMatch(1,pattern);
            }
 
            return processed;
        }

What we do here:

1. Define a function that will replace the tags using regex. It takes a string and replaces all matches with a replacing pattern.
2. Assign it as an array member to a 2-dimensional array that contains a regex in a first dimension and a function to run if the regex is matched in a second dimension.
3. Parse a markdown string using _replaceMatch() that processes all array members one by one.

I think it is pretty neat.