Fun with anonymous methods – using higher-order functions

Since version 2009, the Delphi language (or better, its Object Pascal dialect) supports anonymous methods. What's an anonymous method? Not surprisingly, an anonymous method is a procedure or a function that does not have an associated name. An anonymous method treats a block of code just like a value so that it can be assigned to a variable, used as a parameter to a method, or returned by a function as its result value. In addition, an anonymous method can refer to variables and bind values to them in the context scope in which the anonymous method is defined. Anonymous methods are similar to closures defined in other languages, such as JavaScript or C#. An anonymous method type is declared as a reference to a function:

type 
  TFuncOfString = reference to function(S: String): String; 

Anonymous methods (or anonymous functions) are convenient for passing as an argument to a higher-order function. What's a higher-order function?

Wikipedia (http://en.wikipedia.org/wiki/Higher-order_function) gives the following explanation:

In mathematics and computer science, a higher-order function (also functional form, functional, or functor) is a function that does at least one of the following:

  • Takes one or more functions as an input
  • Outputs a function

All other functions are first-order functions.